Inspecting your container
There comes a time in the life of anyone working with containers when you will need to jump into a running container and see what is going on. Fortunately, Docker has just the tool for you in the form of docker exec
. The exec
subcommand takes two arguments, the name of the container, and the command to run:
$ docker exec -it nginx bash
I slipped an option in there that is important if you are starting an interactive process. The -it
option tells Docker that you have an interactive process and that you want a new TTY. This is essential if you want to start a shell:
$ sudo docker exec -it nginx bash root@fd8533fa2eda:/# ps ax PID TTY STAT TIME COMMAND 1 ? Ss 0:00 nginx: master process nginx -g daemon off; 6 ? S 0:00 nginx: worker process 13 ? Ss 0:00 bash 18 ? R+ 0:00 ps ax root@fd8533fa2eda:/# exit
In the preceding example, I connected to the container and ran ps ax
to see every process that the container knew about. Getting a shell in the container can be invaluable when debugging. You can verify that files were added correctly or that internal scripts are properly handling environment variables passed in through docker
.
It's also possible to run non-interactive programs. Let's use the same ps
example as earlier:
$ sudo docker exec nginx ps ax PID TTY STAT TIME COMMAND 1 ? Ss 0:00 nginx: master process nginx -g daemon off; 6 ? S 0:00 nginx: worker process 19 ? Rs 0:00 ps ax
As you might expect, there's not much to see here, but it should give you an idea of what is possible. I often use them when debugging and I do not need a full shell.