One of the great things about Docker is that it isn't really virtualization; as mentioned in the previous chapter, it is a great way of isolating processes rather than running an entire operating system.
This can get confusing when running tools such as top
or ps
. To get an idea just how confusing this can get, lets launch several containers using docker-compose
and see for ourselves:
$ cd /monitoring_docker/Chapter01/02-multiple $ docker-compose up -d Creating 02multiple_web_1... $ docker-compose scale web=5 Creating 02multiple_web_2... Creating 02multiple_web_3... Creating 02multiple_web_4... Creating 02multiple_web_5... Starting 02multiple_web_2... Starting 02multiple_web_3... Starting 02multiple_web_4... Starting 02multiple_web_5...
Now, we have five web servers that have all been launched from the same image using the same configuration. One of the first things I do when logging into a server to troubleshoot a problem is run ps -aux
; this will show all the running processes. As you can see, when running the command, there are a lot processes listed.
Even just trying to look at the processes for NGINX is confusing, as there is nothing to differentiate the processes from one container to another, as shown in the following output:
So, how can you know which container owns which processes?
This command lists all the processes that are running within a container; think of it as a way of filtering the output of the ps aux
command we ran on the host machine:
As docker top
is an implementation of the standard ps
command, any flags you would normally pass to ps
should work as follows:
$ docker top 02multiple_web_3 –aux $ docker top 02multiple_web_3 -faux
Another way to view what is going on within a container is to enter it. To enable you to do this, Docker introduced the docker exec
command. This allows you to spawn an additional process within an already running container and then attach to the process; so, if we wanted to look at what is currently running on 02multiple_web_3
, we should use the following command spawn a bash shell within an already running container:
docker exec -t -i 02multiple_web_3 bash
Once you have an active shell on the container, you will notice that your prompt has changed to the container's ID. Your session is now isolated to the container's environment, meaning that you will only be able to interact with the processes belonging to the container you entered.
From here, you can run the ps aux
or top
command as you would do on the host machine, and only see the processes associated with the container you are interested in:
To leave the container, type in exit
, you should see your prompt change back in your host machine.
Finally, you can stop and remove the containers by running docker-compose stop
and docker-compose kill
.