In the previous part of this series, we familiarized ourselves with different concepts involving Docker. In this part of the series, we will look into some of the basic commands and run our first container.
The first step of course would be to install Docker on your PC. We will be using a Windows Host in this example and hence, would use the Docker Desktop For Windows. You can follow the instructions in the following link to install Docker Desktop For Windows.
Running your first Container
Time to get up and running. Let us run our first container. The Docker community maintains a “hello-world” image, running a minimal hello world app, which is a good place to start our journey.
The first step is to download the image. You can use the
pull command, which downloads an image from the Docker Hub.
> docker pull hello-world
The above command would be to download the image for “hello-world” from the Docker Hub Registry. By default, if no tag is specified,
:latest tag would be used. This can be observed in the log when you execute the above command.
> docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a Status: Downloaded newer image for hello-world:latest docker.io/library/hello-world:latest
Docker Tags are named references for Docker images. They make it easier for developers to reference a particular variant of the image. Note that I used the word variant and not version. This was intensional. The tags don’t refer to a particular version of the image. An image with the same tag can be updated with a newer version which might have a different feature set. Also, multiple tags can be pointed to the same image.
To download a particular variant of the image, you can use the
pull along with the
docker pull hello-world:linux
To download all the variants(tags) of the image, you can use the
-a flag along with the
pull command. For example,
> docker pull -a hello-world
Similarly, by default, the images are pulled from the Docker Hub Register. But you could pull images from your own register by specifying the register url.
> docker pull imageName registryUrl
> docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 4 months ago 13.3kB hello-world linux feb5d9fea6a5 4 months ago 13.3kB
Now that we have downloaded our images, it is time to run it. For running the container, you can use the
docker run command.
> docker run hello-world:latest
docker run commands create a writable layer over the image and start the container. The above command would run the “hello-world” with tag “latest”. If you observe the Container List in the Docker Desktop, you will notice that the Container runs under a random name.
This is because we haven’t supplied a name for the container. You can specify the name using the
--name flag followed by the container name of your choice. For example
> docker run --name helloWorldContainer hello-world:latest
The above command would run the
hello-world image as a container with the name
helloWorldContainer. If you want to run the container as a background server, you can use the
-d flag along with the
docker run command.
If you specify an image that doesn’t exist in the host machine, docker would also execute the
docker pull command to download it from the docker hub, before executing the
If you would like to see the already running (or ran) containers, you can use the
docker ps command.
> docker ps
docker ps commands list all the containers that are currently running. In this above case, the
helloWorldContainer would have already exited. To see all the containers that are in the system (running and stopped), you can use the
> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 095a0963c2f7 hello-world:latest "/hello" 8 minutes ago Exited (0) 8 minutes ago helloWorldContainer 57f6d3987440 hello-world:latest "/hello" 17 minutes ago Exited (0) 17 minutes ago brave_mcclintock
You can start a stopped container using the
docker start [containerName] command.
docker start helloWorldContainer
Running Sql Server as Container
We have, so far, familiarized ourselves with common commands. Let us now use our newly acquired knowledge to run a Microsoft Sql Server container. We will use the mcr.microsoft.com/mssql/server image with 2019-latest tag for our exercise.
docker run --name nt-user-sqlserver -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=YourPassword" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
We have already known some of the flags, but there are some new ones introduced here. Let us look at the one by one
|–name||Name of the container|
|-e||Set environment variables|
|-p||Expose/Publish a containers port to host|
|-d||Run in background|
It is important to expose the port to the host (using the -p). Otherwise, your Sql Client will not able to connect to the container. Now you can use your favorite Sql Client tool to connect to the database.
In this part of the tutorial, we learned how to download, and run your favorite container from Docker Hub. We also familiarized ourselves with some of the important flags that can be used with these commands. We also learned how to expose a container port to the host such that the host machine could interact with the container.
So far we have worked on a single container. But in a multi-container environment, how do the different containers interact with each other? We will into the details in the next blog.