Hello Docker 4: Exploration

Ocean Blue
2 min readDec 7, 2020

If you have followed through the guide in Hello Docker 3, you should have a long running container now.

There are 2 common operations you want to perform when you have a running container.

Exploring the inside of a running container

What if you want to see what the environment is like inside the container? Because multiple processes can run inside the same container, you can always run an additional process in it to see what’s inside. You can even run a shell, provided that the shell’s binary executable is available in the image.

Running a shell inside an existing container

The Python image on which you’ve based your image contains the bash shell, so you can run the shell inside the container like this:

$ docker exec -it web-container bash

This will run bash inside the existing web-container. The bash process will have the same Linux namespaces as the main container process. This allows you to explore the container from within and see how Python and your app see the system when running inside the container. The -it option is shorthand for two options:

  • -i, which makes sure STDIN is kept open. You need this for entering commands into the shell.
  • -t, which allocates a pseudo terminal (TTY).

You need both if you want the use the shell like you’re used to. (If you leave out the first one, you can’t type any commands, and if you leave out the second one, the command prompt won’t be displayed and some commands will complain about the TERM variable not being set.)

Entering a running container like this is useful when debugging an app running in a container. When something’s wrong, the first thing you’ll want to explore is the actual state of the system your application sees. Keep in mind that an application will not only see its own unique filesystem, but also processes, users, hostname, and network interfaces.

--

--