How To SSH Into Running Docker Container?

Docker is used to creating containers and run them isolated from the host. Docker containers are very similar to virtual machines where they provide similar services to VMs like SSH, HTTP, Telnet, etc. In this tutorial, we examine how to SSH into a running Docker Container in different ways like using the docker exec , docker attach and ssh command.

Run Command Using “docker exec”

The docker exec command is used to run commands in a docker container. Different commands can be run by using the “docker exec” as well as the bash can be also started which is very same as making SSH into the docker container. First, the container should be already running. A container can be started by using the “docker run” command by providing the image name. In the following example, we start a container from the ubuntu image.

$ sudo docker run --name myubuntu -d ubuntu

Then we should list the running container name with the “docker ps” command.

$ sudo docker ps

The syntax of the “docker exec” command to run bash shell is like below. The -it is used to create an interactive terminal for the /bin/bash command inside the docker container. “ubuntu” is the docker container name.

$ sudo docker exec -it ubuntu /bin/bash

Run Command Using “docker attach”

Another way to SSH into the running docker container to get a shell is using the “docker attach” command. The docker attach command directly attaches the running container shell into the host system shell by binding input, output, and error streams and launches bash shell .

In the following example, we attach the running container bash shell named “myubuntu”.

$ sudo docker attach myubuntu

Connect with SSH Command

The last method to make SSH into a running container is installing and running the SSH service inside the container and then getting its IP address in order to connect it by using the SSH command. This method is the most complex method but provides a real SSH connection which can be also used via other host systems to connect to the running container.

Install and Run SSH for Ubuntu, Debian Containers

$ sudo apt-get install ssh
$ sudo systemctl ssh start
$ sudo systemctl ssh enable
$ service ssh status

Install and Run SSH for CentOS, RHEL, Fedora Containers

$ yum –y install openssh-server openssh-clients
$ service sshd start
$ service sshd enable
$ service sshd status

Get the IP Address of the Container

Now we should get the IP address of the container by using the docker inspect command. In the following example set the CONTAINER_NAME with your container name like “myubuntu” etc.

$ sudo docker inspect -f "{{ .NetworkSettings.IPAddress }}" CONTAINER_NAME

SSH Into Container

This is the last step where we use the ssh command in order to connect to the container by using its IP address. In the following example, we connect the container with the IP address “” where the username is “root”. The user name can change in some situations. You can take a look at the container image documentation for different user names.

$ ssh [email protected]

Leave a Comment