Card 05/ 06

WalkthroughDifficulty: Intermediate1 min

Opening a Shell Inside a Running Container

Sooner or later you need to stand inside a running container and look around: is the config file where you think, is the port really open, does the directory you mounted contain what you expected.

Getting a prompt inside a container that is already running

Step 1 of 3

Run a command in it

bash
$ docker exec web ls /usr/share/nginx/html
50x.html
index.html

exec starts a second process inside a container that is already running. The first one carries on untouched.

Ask for a shell instead

bash
$ docker exec -it web sh
# whoami
root
# exit

-i keeps the input stream open and -t gives you a terminal. Without both, the shell starts and immediately has nothing to talk to.

Know what you changed

Anything you edit in there lives in the container's writable layer and disappears with the container. It is the right place to look and the wrong place to fix.

The command people reach for by mistake is docker run, which starts a new container from the image and shows you a machine where none of the problem exists.