Now, let's take a look at how we can stop these containers. For various reasons, we would want to do this. There are a few commands we could use; they are docker kill
, docker stop
, docker pause
, and docker unpause
. Let's cover them briefly as they are fairly straightforward. First, let's look at the difference between docker kill
and docker stop
. The docker kill
command will do just that—kill the container immediately. For a graceful shutdown of the container, you would want to use the docker stop
command. Mostly, when you are testing, you will be using docker kill
. When you're in your production environments, you will want to use docker stop
to ensure you don't corrupt any data you might have in the Docker volumes. The commands are used exactly like the docker logs
command, where you can use the container ID, the random name given to the container, or the one you might specify with the --name= switch
.
Now, let's take a dive into how we can execute some commands, view information on our running containers, and manipulate them in a small sense. We will cover more about container manipulation in the later chapters as well. The first thing we want to take a look at, which will make things a little easier with the upcoming commands, is the docker rename
command. With the docker rename
command, we can change the name that has been randomly generated for the container. When we performed the docker run
command, a random name was assigned to our container; most times, these names are fine. But if you are looking for an easy way to manage the containers, a name can be sometimes easier to remember. For this, you can use the docker rename
command as follows:
$ docker rename <current_container_name> <new_container_name>
Now that we have an easily recognizable and rememberable name, let's take a peek inside our containers with the docker stats
and docker top
commands, taking them in order:
$ docker stats <container_name> CONTAINER CPU % MEM USAGE/LIMIT MEM % NET I/O web1 0.00% 1.016 MB/2.099 GB 0.05% 0 B/0 B
The other command docker top
provides a list of all running processes inside the container. Again, we can use the name of the container to pull the information:
$ docker top <container_name>
We will receive an output similar to the following one based on what processes are running inside the container:
UID PID PPID C STIME TTY TIME CMD root 8057 1380 0 13:02 pts/0 00:00:00 /bin/bash
We can see who is running the process (in this case, the root
user), the command being run (in this case, /bin/bash
), as well as the other information that might be useful.
Lastly, let's cover how we can remove the containers. The same way we looked at removing images earlier with the docker rmi
command, we can use the docker rm
command to remove unwanted containers. This is useful if you want to reuse a name you provided to a container:
$ docker rm <container_name>