Dial D for Docker: Part 2 - Images and Containers
In the last segment on Dial D for Docker, we had a small introduction about docker and we saw about the installations and command structures. In this part, lets look in depth into docker images and containers.
What are docker images and containers
If you are a programmer, I can provide you a straight forward analogy. An image is comparable to a class while a container to an object. You can instantiate n number of objects from your class similarly you can run n number of containers from your image.
If you are a dev-ops personnel or a non programmer, you can associate an image to a blueprint or a recipe and the container as the buildings built from the blueprint or the delicious pizza prepared from the recipe. Once you have your blueprint or recipe you can built as many buildings as you want from your blueprint or you can cook pizza for an entire party from the recipe. Likewise, you can run a number of containers from an image.
Breaking down the Nginx installation
We installed and ran nginx in the last part of the blog using the command docker container run --publish 80:80 --detach nginx
. This command downloaded an image, invoked a container from the image, ran the image in the background while associating port 80 of the machine with the port 80 of the container.
Now lets do it an other way. As we discussed, containers are invoked from an image. So in order to run an nginx container, we require the Nginx image. So pull one from the dockerhub(more about dockerhub later) using the command
docker image pull nginx
Once it is downloaded, you can check the downloaded docker-image using the command
docker image ls
As you can see in the console(Using default tag: latest), by default docker downloads the latest version of the image. If you want to download an older version of the image, you can add the appropriate tag to the command as such
docker image pull nginx:1.12.1
Now you can see that there exists two nginx images, one with the tag - latest and another with the tag - 1.12.1
Now that we have downloaded our image, we can run the container using our same old command
docker container run --publish 80:80 -- detach nginx
The difference in both the cases while running this command is that first time it downloaded the nginx from dockerhub but this time it used the already downloaded image.
Options: publish and detach
In our execution we saw 2 options passed to the run command, publish and detach. Publish binds the port within the container to the port of machine running container. Lets see what happens if we run without the publish option
As you can see from the images, though the container is created and its running, the ports are not bound and hence we are not able to access the nginx. In the pics below, you can notice the newly invoked container’s port 80 is bound to port 7777 of my machine.
Detach option on the other hand helps us to run the container in the background.
Commands: ls and ls -a
Docker has used unix nomenclature for listing the containers and images. So use the following command to list all the containers that are running on the maching
docker container ls
You can add the option `a` to the same command to also list the containers were created but are not currently running.
Providing names to the containers
If you had noticed in the above pictures, each container is given a random name (youthful_jones
and blissful_benz
in our case). If a container is created without a name, docker creates a name by concatenating a random adjective with surname of famous scientists.
You can find the entire list of adjectives and the Scientist’s names here.
We can also add our own names to the containers by using the option name
.
Commands: start, stop and restart
As the name suggests you can start, stop and restart a container using the respective commands.
Commands: inspect, stats & top
Docker inspect
command provides an detailed inspection report about the container
stats
command provides a live stats about the container. This includes CPU usage, Memory usage, IO, process ID
top
command lists the processes running in the container.
With this let us end this part of Dial D for Docker. Play around with the commands. Pull and run various kinds of containers(Node.js, MongoDB, MySql, Ubuntu, etc).
In the next segment, lets see how to interact within the docker container from the CLI.
This article is a part of docker series Dial D for Docker. You can find the previous and next articles in the following links