How to start a shell in a running Docker container

by Liger Learn
Introduction
There are a number of reasons for wanting to open a shell within a running container, including:
- To see the contents of any number of configuration files.
- To make a quick change to a file which is not part of any of the container’s mounted volumes.
- It should be noted that this is most likely an anti-pattern. However, it could also be argued that it is harmless if the change is minor and is made only in development environments for debugging and tweaking.
- If the change you are making is intended to be permanent you should consider capturing it in a Dockerfile or mounting a volume containing the relevant file.
- If you are interested in learning how you can edit files inside containers see the articles how to edit files within docker containers and using Emacs to edit files within Docker containers.
- And last but not least – for curiosity’s sake – you just want to explore what exactly is inside your running container.
How to open a bash shell inside a running container and get an interactive command prompt
There are actually a number of ways in which you can achieve the goal of opening a shell within a running Docker container. The easiest is shown in the source block below:
1 |
docker exec -it <container-id> /bin/bash |
- The -i flag tells docker to keep stdin open (so you can enter commands).
- The -t flag allocates a pseudo-tty.
As stated in the official Docker documentation “the -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin”.
Example walk-through
1. Run your container
1 2 |
$ docker run -d redis 2b659caec54ae77aaec06fa4d4f678120ecad859299eb674f74313426fa9e535 |
- In this case we are running a redis container.
- Since we are running in detached mode -d the container ID should be output. If you do not supply the -d flag you will need to get the ID by running docker ps (shown in the next step).
2. Get the ID of the container you would like to open a shell in
1 2 3 |
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2b659caec54a redis "docker-entrypoint..." 20 seconds ago Up 20 seconds 6379/tcp nostalgic_babbage |
- In case you need to find the container ID again you can run the command docker ps and identify your container’s ID (first column).
3. Run a bash shell inside the container and gain access to a command prompt
1 2 |
$ docker exec -it 2b6 /bin/bash root@2b659caec54a:/data# |
- Using the template covered above (
docker exec -it <container-id> /bin/bash) we enter the right
<container-id> and get access to a command prompt.
- We only need to enter the first few characters of the ID as it is enough for Docker to uniquely identify the container.
- From here we know have access to a root shell and can issue commands.
4. Issue your commands
1 2 3 4 5 |
root@2b659caec54a:/data# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND redis 1 0.1 0.0 33304 7820 ? Ssl 23:47 0:01 redis-server *:6379 root 16 0.0 0.0 20240 1968 ? Ss 23:54 0:00 /bin/bash root 23 0.0 0.0 17492 1148 ? R+ 23:59 0:00 ps aux |
- The ps aux command shows that redis is running with PID 1 as we would expect and /bin/bash is running with PID 16. The ps aux command itself is run with PID 21.
1 2 3 4 |
root@2b659caec54a:/data# vi bash: vi: command not found root@2b659caec54a:/data# nano bash: nano: command not found |
- It is important to note that you may not have access to common utility programs which you are used to having such as
vi and
nano.
- Available programs/commands will usually be limited within the container.
- If you desperately need a program installed you can use your container’s package manage system.
- For example if the container was built using Debian you could use apt-get install.
- However, there is no guarantee that the package manager for your system comes bundled inside your container (it usually does though).
Conclusion
This quick article showed you how you can open a bash shell within a running container to have a peek around the container’s insides. Additionally it illustrated how common programs such as vi and nano may not be installed within the container so you are limited in your ability to issue typical commands that may form part of your workflow.
Recommended Posts

Using Emacs to edit files within Docker containers
March 12, 2017

How to edit files within docker containers
March 11, 2017

Lambda (λ) calculus primer: from the basics to complete programs
September 18, 2023