Learn Docker:Fundamentals of Docker 19.x
上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) tool, 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.

alpine is a minimal Docker image based on Alpine Linux with a complete package index and is only 5 MB in size.

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 screenshot can help you to get a better idea of 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
8ba884070f61: Pull complete
Digest: sha256:b5e66c4651870a1ad435cd75922fe2cb943c9e973a9673822d1414824a1d0475
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.104 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.081 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.055 ms
--- 127.0.0.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4127ms
rtt min/avg/max/mdev = 0.050/0.069/0.104/0.022 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 five times until it stops.

centos is the official Docker image for CentOS Linux, which is a community-supported distribution derived from sources freely provided to the public by Red Hat  for Red Hat Enterprise Linux (RHEL).

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 so that images are pulled from 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:

8ba884070f61: Pull complete
Digest: sha256:b5e66c4651870a1ad435cd75922fe2cb943c9e973a9673822d1414824a1d0475
Status: Downloaded newer image for centos:latest

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

All of 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 latest keyword occurring a few times. Each image has a version (also called tag), and if we don't specify a version explicitly, then Docker automatically assumes it is 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 hence won't have to download it first. Try it out and verify what I just told you.