Getting Started with Containerization
上QQ阅读APP看书,第一时间看更新

Starting, stopping, and removing containers

You have successfully run a container in the previous section. Now we want to investigate in detail what exactly happened and why. Let's look again at the command we used:

$ docker container run alpine echo "Hello World" 

This command contains multiple parts. First and foremost, we have the word docker. This is the name of the Docker command-line interface (CLI), which we are using to interact with the Docker engine that is responsible to run containers. Next, we have the word container, which indicates the context we are working with. As we want to run a container, our context is the word container. Next is the actual command we want to execute in the given context, which is run.

Let me recap—so far, we have docker container run, which means, Hey Docker, we want to run a container....

Now we also need to tell Docker which container to run. In this case, this is the so-called alpine container. Finally, we need to define what kind of process or task shall be executed inside the container when it is running. In our case, this is the last part of the command, echo "Hello World".

Maybe the following figure can help you to get a better approach to the whole thing:

Anatomy of the docker container run expression

Now that we have understood the various parts of a command to run a container, let's try to run another container with a different process running inside it. Type the following command into your Terminal:

$ docker container run centos ping -c 5 127.0.0.1

You should see output in your Terminal window similar to the following:

Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
85432449fd0f: Pull complete
Digest: sha256:3b1a65e9a05...
Status: Downloaded newer image for centos:latest
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.019 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.029 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.030 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.029 ms

--- 127.0.0.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4103ms
rtt min/avg/max/mdev = 0.021/0.027/0.029/0.003 ms

What changed is that, this time, the container image we're using is centos and the process we're executing inside the centos container is ping -c 5 127.0.0.1, which pings the loopback address for five times until it stops.

Let's analyze the output in detail:

  • The first line is as follows:
Unable to find image 'centos:latest' locally

This tells us that Docker didn't find an image named centos:latest in the local cache of the system. So, Docker knows that it has to pull the image from some registry where container images are stored. By default, your Docker environment is configured such as that images are pulled from the Docker Hub at docker.io. This is expressed by the second line, as follows:

    latest: Pulling from library/centos 
  • The next three lines of output are as follows:
    85432449fd0f: Pull complete
    Digest: sha256:3b1a65e9a05...
    Status: Downloaded newer image for centos:latest

This tells us that Docker has successfully pulled the image centos:latest from the Docker Hub.

All the subsequent lines of the output are generated by the process we ran inside the container, which is the ping tool in this case. If you have been attentive so far, then you might have noticed the keyword latest occurring a few times. Each image has a version (also called a tag), and if we don't specify a version explicitly, then Docker automatically assumes it as latest.

If we run the preceding container again on our system, the first five lines of the output will be missing since, this time, Docker will find the container image cached locally and thus won't have to download it first. Try it out and verify what I just told.