After installing Docker on Fedora, AlmaLinux, Manjaro, or some other distro, it’s time to install more containers. Once you have a Docker container up and running on a Linux system, one of the things you’ll likely need to do is run commands inside the container. This allows you to use the container similarly to how you would a physical machine, except that Docker has done most of the setup legwork for us already.
There are already two commands available that allow us to run commands on a Docker container. The first one is docker exec
, and the second command, which allows us to attach to a running container, is docker attach
. These commands usually suffice, but you may find yourself in a scenario where you’d prefer to use SSH to connect to the Docker container and manage it.
Not all Docker containers are provisioned to run SSH. Normally, Docker containers are very lightweight and only programmed to do one thing. However, some Docker containers will allow SSH, and this can make management of the container much easier. In this guide, we’ll see how to connect to a Docker container via SSH from the host system on Linux command line.
In this tutorial you will learn:
- How to connect to a running Docker container via SSH
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | Docker |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Connect to Docker container via SSH
For this example, we’ve already installed Docker and an NGINX image by using the docker pull nginx
command. Follow the step by step instructions below to see how we determine the container’s IP address, and then use the SSH command to connect to the running container.
- First, the Docker container needs to be actively running. Make sure that you’ve started it already by using the following command. Note once again that we are using an NGINX image, but you can substitute the name of your own image in this command and future commands.
$ docker run --name nginx -d nginx
- Verify your container is running, as well as the name of your container, with this command.
$ docker ps
- We can now use the following command to determine the IP address of our NGINX container.
$ docker inspect -f "{{ .NetworkSettings.IPAddress }}" nginx
- In our case, the NGINX container has an IP address of
172.17.0.2
. We can now attempt to connect to this IP address via SSH and the root account.$ ssh root@172.17.0.2
At this point, you’ll either connect successfully to the running container, or get a “connection refused” error. In the case of an error, proceed with the following steps to setup an SSH server on the container.
- If SSH is not working, we need to make sure the software is installed on the container. Connect to the container with the following command.
$ docker exec -it nginx /bin/bash
- Then, install SSH on it. We’re using
apt
package manager in this example, but you may need to adapt the command if your container is using a different package manager.# apt update && apt -y install openssh-server
- Next, configure the container so we can login to the root account via SSH.
# echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
- Don’t forget to change the root password so you can login.
# passwd
- Finally, start the SSH service on the container.
# /etc/init.d/ssh start
- Now you will be able to SSH from the host machine by specifying the IP address you determined earlier, and using the root password you just configured the password for.
$ ssh root@172.17.0.2
That’s all there is to it. Now you are able to connect to your running container via SSH to run commands and manage it. Of course, the method of using docker exec
command should continue working as well, and can accomplish much the same thing.
Closing Thoughts
In this guide, we saw how to connect to a running Docker container from the host system via SSH. Docker already gives us multiple ways to connect to a container, but having the ability to SSH into containers can be nice for some situations. The tricky part is that most containers don’t already have SSH installed, since they are normally configured with the bare minimum requirements. This requires a few extra steps on our part, but as you’ve seen here, it’s not that hard to do.