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
Connecting to a running Docker container via SSH on Linux
Connecting to a running Docker container via SSH on Linux
Software Requirements and Linux Command Line Conventions
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


SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


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.

  1. 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
    
  2. Verify your container is running, as well as the name of your container, with this command.
    $ docker ps
    
  3. Viewing the currently running Docker containers on our system
    Viewing the currently running Docker containers on our system
  4. We can now use the following command to determine the IP address of our NGINX container.
    $ docker inspect -f "{{ .NetworkSettings.IPAddress }}" nginx
    


  5. Here we determine the IP address of the Docker container we want to SSH into
    Here we determine the IP address of the Docker container we want to SSH into
  6. 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.
  7. 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
    
  8. 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
    
  9. Next, configure the container so we can login to the root account via SSH.
    # echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
    
  10. Don't forget to change the root password so you can login.
    # passwd
    
  11. Finally, start the SSH service on the container.
    # /etc/init.d/ssh start
    
  12. 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
    


  13. We can now successfully SSH into the Docker container
    We can now successfully SSH into the Docker container

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.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.