How to get bash or ssh into a running container in background mode?

Sample query:

I want to ssh or bash into a running docker container. Please, see example:

$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
665b4a1e17b6  webserver:latest /bin/bash  ...     ...     22/tcp, 80/tcp loving_heisenberg 

Now I want to get something like this (go into the running container):

$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root@665b4a1e17b6:/#

However when I run the line above I get new CONTAINER ID:

$ root@42f1e37bd0e5:/#

I used Vagrant and I’d like to get a similar behaviour as vagrant ssh.

How to get bash or ssh into a running container in background mode?

The answer is Docker’s attach command. So for my example above, the solution will be:

$ sudo docker attach 665b4a1e17b6 #by ID
$ sudo docker attach loving_heisenberg #by Name
$ root@665b4a1e17b6:/#

For Docker version 1.3 or later: Thanks to user WiR3D who suggested another way to get a container’s shell. If we use attach we can use only one instance of the shell. So if we want open a new terminal with a new instance of a container’s shell, we just need to run the following:

$ sudo docker exec -i -t 665b4a1e17b6 /bin/bash #by ID


$ sudo docker exec -i -t loving_heisenberg /bin/bash #by Name
$ root@665b4a1e17b6:/#

Answer #2:

Try this:

sudo docker run -i -t webserver /bin/bash

Answer #3:

From Docker 1.3 onwards:

docker exec -it <containerIdOrName> bash

Basically, if the Docker container was started using the /bin/bash command you can access it using attach. If not, then you need to execute the command to create a Bash instance inside the container using exec.

Also to exit Bash without leaving Bash running in a rogue process:


Yep, it is that simple.

Answer #4:

Although the author of the question specifically said they are interested in a running container, it’s also worth noting that if the container is not running, but you’d like to run it to poke around you can run:

docker run -i -t --entrypoint /bin/bash <imageID>

Answer #5:


Put docker-ssh file in your $PATH with the following contents

#!/bin/bash -xe

# docker container id or name might be given as a parameter

if [[ "$CONTAINER" == "" ]]; then
  # if no id given simply just connect to the first running container
  CONTAINER=$(docker ps | grep -Eo "^[0-9a-z]{8,}\b")

# start an interactive bash inside the container
# note some containers don't have bash, then try: ash (alpine), or simply sh
# the -l at the end stands for login shell that reads profile files (read man)
docker exec -i -t $CONTAINER bash -l

Note: Some container do not contain bash, but ashsh etc. In these cases bash shall be replaced in the above script.


If you have only one running instance, simply run

$> docker-ssh 

Otherwise, provide it with a docker id parameter that you get from docker ps (first col)

$> docker-ssh 50m3r4nd0m1d

Answer #6:

If your container doesn’t have bash installed you could try sh:

docker exec -it CONTAINER /bin/sh

Or look for shells in /bin first:

docker export CONTAINER|tar -t|egrep ^bin/

Answer #7:

I’ve created a containerized SSH server that provides SSH capabilities to any running container. You don’t need to change your container. The only requirement is that the container has bash.

If you have a container with name ‘web-server1’. The following docker run command would start a second container that would provide SSH for the first container.

docker run -ti --name sshd-web-server1 -e CONTAINER=web-server1 -p 2222:22 \
-v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker \

Answer #8:

You can also give the Docker container a routeable IP address with Pipework, and after that SSH into the machine with that new IP address.

This will be more “traditional” (ssh), instead of using an application-specific command like docker attach, and will eventually make it more ‘portable’ across systems and versions.

Answer #9:


install goinside command line tool with:

sudo npm install -g goinside

and go inside a docker container with a proper terminal size with:

goinside docker_container_name

Answer #10:

To bash into a running container, type this:

docker exec -t -i container_name /bin/bash

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →