How to start a shell in a running Docker container

How to start a shell in a running Docker container


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:

  • 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

  • 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

  • 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

  • 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

  • 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.

  • 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).


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.


Leave a Reply

Your email address will not be published. Required fields are marked *