The list container command

The indication of a running container can be shown using the following command:

# Usage: docker container ls [OPTIONS]
docker container ls

This is the list containers command, and without any additional parameters, it will list the currently-running containers. What do I mean by currently running? A container is a special process running on the system, and like other processes on the system, a container can stop or exit. However, unlike other types of processes on your system, the default behavior for a container is to leave behind its read/write layer when it stops. This is because you can restart the container if desired, keeping the state data it had when it exited. As an example, imagine you run a container that is an OS, such as Ubuntu, and in that container you install wget. After the container exits, you can restart it, and it will still have wget installed. Remember that each running container has its own read/write layer, so, if you run one Ubuntu container and install wget, then you run another Ubuntu container, it will not have wget. The read/write layers are not shared between containers. However, if you restart a container that had the wget installed, it will still be installed.

So, the difference between a running container and a stopped one is that the process is either running or it has exited, leaving behind its own read/write layer. There is a parameter to the list containers command that allows you to list all of the containers, both those running and those that have exited. As you may have guessed, it is the --all parameter, and it looks like this:

# short form of the parameter is -a
docker container ls -a
# long form is --all
docker container ls --all

# old syntax
docker ps -a

Now, let's go back to one of my favorite optional run command parameters, the --rm parameter:

# there is no short form of the --rm parameter
docker container run --rm hello-world

This parameter instructs Docker to remove the container's read/write layer automatically when the container exits. When you run a docker container without the --rm parameter, the container data is left behind when the container exits so that the container can be restarted again later. If, however, you include the --rm parameter when you run a container, all of the container's read/write data is removed at the time the container exits. This parameter provides an easy clean up on exit function that you will often find very helpful. Let's see this with a quick example using the run and container ls commands we just discussed:

First, we confirmed we had the hello-world image in our local cache. Next, we listed all of the containers on our system, both running and exited. Note the distinction between images and containers. If you are familiar with VMware, the analogy would be somewhat like a template and a VM. Next, we ran the hello-world container using the --rm parameter. The hello-world container prints its message and then immediately exits (we redirected the output to /dev/null to keep the example output short). Next, we listed the containers again, as we saw that the hello-world container's read/write data was automatically removed when the container exited. After that, we ran the hello-world container again, but this time did not use the --rm parameter. When we listed the containers this time, we saw the indication of the (exited) container. Often you will run a container, knowing that you will never need to restart it later, and using the --rm parameter to automatically clean it up is very handy. But what if you don't use the --rm parameter? Are you stuck with an ever-growing list of containers? Of course not. Docker has a command for that. It is the container rm command.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset