Visual Studio Code for IT pros: Refresh your Docker container skills

Last year, I created two articles here at TechGenix about containers. The first one was “Getting started with containers” and the second one was “Managing containers using Docker client.” In today’s article, we will refresh your memory by working with a couple of images, running them, executing some commands, and then use Visual Studio Code (VS Code) to manage Docker.

Getting a new image and exploring the Docker command line

From a command prompt or PowerShell session, we will use the Docker utility to manage our Docker environment on our workstations. The Docker utility is comprised of two areas: Management commands and commands. The management commands are the second string, and they have several commands underneath related to the management command. The first command that we are going to do is to search in Docker Hub for images. You should have an idea of what you are looking for when creating your containers. If you don’t have that yet, don’t worry, follow along, and you will have more ideas on your own by the end of this article.

We are going to start with Alpine Linux, which is a small image that means fast deployment that we are going to use as the base for this article. We can search for it using the docker search alpine command. The results will be all the images that we have in the Docker Hub. Please pay attention to the official column, which helps when deciding the images you want to use as the base for your new containers/applications.

Visual Studio Code

To download the image locally, we are going to use docker pull alpine. By default, images have tags that are related to the version. Since we are not specifying one it will download the latest provided by the owner of the image.

After downloading the image, we can use the Docker image list to show us all available images, as depicted in the image below. You are reading correctly —the entire image is less than 6MB.

Visual Studio Code

Let’s download the nginx image from the Docker Hub. Just type docker pull nginx and wait for the download process to complete.

Running our first container

Now that we have a couple of local images, we can spin up our first container running the alpine image. We are going to run the command below, and we will have access to the console of the container where we have root access to the console of the Alpine Linux. If we type exit within the container, the container will be automatically stopped, and we can always create a new container.

docker container run --name <NewContainerName> -it <Image-Name> <Command inside of the container>

The most common use of a container is to start them in detached mode. The container will be running until the service is alive. However, we cannot “see” or interact with the container from the command line.

There are a couple of different switches compared to the previous attempt. The first is that we replaced the -it for -d, which means detached mode. The second change was to expose the container port 80 (after all it is an nginx image and by default has the webserver running) to the 9090 port on the host. The results can be seen in a web browser running on the host accessing the initial page of the nginx running on the container that we have just created.

docker container run --name nginx01 -d -p 9090:80 nginx

Visual Studio Code

Getting in and out of an attached container

When we execute a container, we define a service/application to start the container. When such an application ends, the container is stopped automatically. In the previous section, we created a container using -it, which means interactive session.

Sometimes, we want to keep the container running in the background, and it is especially useful when we are running a container that is using a shell session. To leave the container, we can type Ctrl + Q or Ctrl + P to exit the session.

Back to the command prompt, we can list the existing containers, and we will notice that our container is still running, which is a good sign. To return to the container we can type docker attach <ContainerName-or-ContainerId>. The entire sequence of commands is described below, and we can see it in action in the image below.

ls /etc
docker container ls
docker attach <ContainerName-or-ContainerID

Visual Studio Code

Cleaning unused containers

Every time that we spin a new container, the name will be retained and cannot be reused. After a series of tests, we may end up with tons of stopped containers that we are not planning to use again. Using the commands listed below, we can list all containers and remove all the stopped ones.

Docker container list -a
Docker container prune
Docker container list -a

Using the right tool goes a long way: Visual Studio Code to the rescue!

We have been using the docker command line, and we covered some basic tasks that we should know when working with Docker. However, there are several tools to help manage containers, and one of my favorites is Visual Studio Code. I wrote an article about Visual Studio Code here at TechGenix. Please check it out here.

Visual Studio has a Docker extension. Click on Extensions icon and type docker, select the extension from Microsoft. Click on install and a new docker icon will be listed on the left menu.

The image below provides a glimpse of some of the integration between Docker and Visual Studio Code. Here is a simple explanation of the features available using the Visual Studio Code:

  • A list of containers in the current host (Item 1).
  • All existent images (Item 2).
  • We can check registry (Item 3) where we can pull and push images.
  • All existing networks (Item 4).
  • All volumes (Item 5).
  • Commands that can be executed (Item 6) at any time.

Besides viewing the objects, we can manage them, including deletion.

Visual Studio Code

If we want to have a sneak peek inside of a container using the VS Code, right-click on the desired container and click Attach Shell. At the bottom right corner, you will get access to the container. Pretty cool, eh?

Visual Studio Code and Docker: Wrapping up

Ready to spin your containers on your own? I hope you are excited as I am with containers and how an IT pro can use them.

One of the main advantages of containers is portability. In our journey, we are learning how to manage them from the very basics. Our next stop will be creating our images, and from that point on, we can go to more scalable and orchestrated solutions. Yes, I’m talking about Kubernetes, more specifically how to use AKS (Azure Kubernetes Services) to deliver modern applications to your users and customers. Stay tuned!

Featured image: Docker

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top