How to SSH into a Docker Container in 3 Easy Methods

Try this guide with our instant dedicated server for as low as 40 Euros

SSH into a docker container

Docker is often the preferred method of deploying applications on multiple server configurations. Developers can write their applications and then package the code and dependencies into one convenient container that they can deploy on any compatible platform. 

Given the popularity of Docker as the primary application deployment method, the ability to interact with live containers is a critical requirement. Whether you’re debugging or just performing routine tasks, you need a secure way of accessing the container. 

That’s where SSH, or Secure Shell, comes in handy. 

Let’s dive into how you can SSH into a Docker container and execute the commands to manage your applications effectively. We’ll discuss three methods of setting up SSH access to the Docker containers, and then sign off with some troubleshooting tips. 

Let’s start with the prerequisites of the process.

Table of Contents

  1. The Prerequisites
  2. How to SSH into a Docker Container for Executing Commands
  3. Troubleshooting Errors and Best Practices
  4. Conclusion
  5. FAQs

The Prerequisites

Setting up SSH into a Docker container requires:

  • Docker running on a Linux system
  • Active container on the Docker
  • Access to the terminal or command-line
  • A user account with administrator or sudo privileges

How to SSH into a Docker Container for Executing Commands

Let’s discuss the three common methods of setting up SSH access into a Docker container. These methods will help you securely execute commands inside an active container.

Method #1: Run Commands with docker exec

The docker exec command is used to execute commands in active containers. You can use this command to directly connect to the container. 

The basic syntax of the docker exec command is: 

# docker exec [options] [container] [command]

Here’s how to use this command:

If you don’t have a Docker image, grab one first. For instance, you can get the NGINX image with the following command:

Pull nginx image

Next, deploy this image into a container:

#sudo docker run –name nginx-image -d nginx

name nginx image

After setting up a container, it’s always a good idea to verify container activity with the following command:

#sudo docker ps

sudo docker ps

Now that you have an active image, you can use docker exec to run commands:

#sudo docker exec -it nginx-test /bin/bash

it nginx image

By using docker exec with the -i and -t flags, your terminal becomes the command line inside the container. The -i flag lets you send commands to the container, while -t sets up a text interface that feels like a regular terminal session.

Method #2: Use docker attach to Connect Your Terminal to the Container

The docker attach creates a direct line from your terminal to the container’s command line. Note that this method is not flexible and we do not recommend it for running additional commands because it attaches to the main process of the container.

Use the following command syntax to run the docker attach command: 

#sudo docker attach <name_of_your_container>

Once connected, you can start typing commands in the terminal as if directly working within the container. 

For instance, the following command will connect to the NGINX container we mentioned in the previous method and allow you to run commands directly within the container:

#sudo docker attach nginx-image

start nginx image

Method #3: Deploy a Docker Container With SSH Access

The previous two methods work with existing containers and provide a way to connect the terminal with the container’s command line. These methods might not work depending on the container and platform configurations. 

A better approach is to create a container with built-in SSH access to simplify the process of running commands securely. 

Let’s run through the process of creating an SSH-enabled container and then execute commands. Note that for this demonstration, we’ll use the Ubuntu image.

Step #1: Create a Dockerfile 

Start by creating a Dockerfile to set up an SSH service within your container. A Dockerfile is a file that lists all the steps you need to follow on the command line to create a Docker image. You can make your own Dockerfile and edit it with a text editor. 

The following example shows how to create a Dockerfile that you can use to create an Ubuntu Docker image that allows SSH access within the container:

FROM ubuntu:latest

RUN apt-get update && apt-get install -y openssh-server

RUN mkdir /var/run/sshd

RUN echo ‘root:YOUR_PASSWORD’ | chpasswd

RUN sed -i ‘s/#PermitRootLogin prohibit-password/PermitRootLogin yes/’ /etc/ssh/sshd_config

# SSH login fix. Otherwise, the user is kicked off after login.

RUN sed ‘s@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g’ -i /etc/pam.d/sshd

EXPOSE 22

CMD [“/usr/sbin/sshd”, “-D”]

create docker file

Remember to replace YOUR_PASSWORD with a strong password. 

Step #2: Build the Image 

Now that you have a Dockerfile, the next step is to build a Docker image with this file. For this, run the following command:

#docker build -t my_sshd.

build image

The command will take a while because the Dockerfile includes several steps such as installing an OpenSSH server, setting up the root password, and allowing root SSH login.

Step #3: Create and Run the Container 

Now that you have an image, create a new container from the Docket image you created in the last step. For this, run the following command:

#docker run -d -p 2222:22 –name my_ssh_container my_sshd

create and run container

This command starts the Docker container in a mode where it runs in the background (-d) and connects port 2222 on your server to port 22 on the Docker container, which is the usual port for SSH.

Step #4: SSH into the Container 

Now you can SSH into the container using:

#ssh root@localhost -p 2222

SSH in Container

Enter the password you set in the Dockerfile when prompted, and you will be logged into the Ubuntu Docker container through SSH.

log in docker container

After successfully establishing an SSH connection to your Docker container, you can interact with it as you would in a standard Linux terminal environment. For instance, you can use the following command to list the contents of a directory within the container:

# ls -la

Troubleshooting Errors and Best Practices

If you encounter any difficulties accessing the container through SSH, consider the following troubleshooting steps:

  1. Verify that the SSH service is up and running within the container.
  2. Double-check that the ports are correctly mapped, ensuring the container’s SSH port is appropriately exposed and connected to the appropriate server’s port.
  3. Review the firewall settings to make sure it does not block the SSH ports.

Additionally, when using SSH to access containers, it’s important to adhere to these best practices:

  1. Avoid installing unnecessary packages within your container to maintain a minimal and efficient image.
  2. Create lightweight and efficient Docker images by removing any redundant components.
  3. Expose only the ports required for the services running within the container to enhance security.

Conclusion

We’ve described three methods to SSH into an active Docker container. The fastest way is to use the docker exec approach. However, if you are setting up a container, we recommend adding SSH access to the container’s Docker file to give the container SSH access from the start. 

Connecting to Docker isn’t just about getting inside your containers—it’s about doing so with security and efficiency. 

And, as you continue to manage and optimize your Docker setups, consider a hosting partner that values your need for robustness and reliability. RedSwitches offers a range of hosting solutions to support your Docker deployments, ensuring that your containers are running on top-notch infrastructure. 

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

Happy Dockering!

FAQs

Q. What is SSH into a Docker Container?

SSH (Secure Shell) into a Docker Container is a method that allows you to securely connect to a running Docker container via a command line interface.

Q. How can I SSH into a Docker Container?

To SSH into a Docker Container, you can use the `docker exec` command and specify the container ID or name along with the SSH command. For example: `docker exec -it ssh user@`.

Q. Can I SSH into a Docker Container using the Docker run command?

No, you cannot SSH into a Docker Container using the Docker run command. The Docker run command is used to start a container, whereas SSH requires an established connection to a running container.

Q. Do I need an SSH server installed inside the container to SSH into it?

Yes, in order to SSH into a Docker Container, you need to have an SSH server installed inside the container. You can use a base image that already has an SSH server installed or create a custom Docker image with an SSH server.

Q. How can I start a Docker Container with SSH capabilities?

You can start a Docker Container with SSH capabilities by creating a new Docker image that includes an SSH server. You can add the necessary configurations and startup scripts in the Dockerfile to ensure that SSH is running inside the container.

Q. Can I run commands in a Docker Container via SSH?

Yes, once you have successfully SSHed into a Docker Container, you can run any command inside the container, just like you would on a traditional SSH connection.

Q. How can I find the IP address of a Docker Container to SSH into it?

You can find the IP address of a Docker Container by using the following command: `docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ `.

Q. Can I SSH into multiple Docker Containers simultaneously?

Yes, you can SSH into multiple Docker Containers simultaneously by opening separate SSH connections to each container’s IP address and specifying the appropriate container ID or name.

Q. Can I SSH into a running Docker Container?

Yes, you can SSH into a running Docker Container. As long as the container has an SSH server installed and running, you can establish an SSH connection to it.

Q. Do I need to expose the SSH port of a Docker Container to SSH into it?

No, you do not need to expose the SSH port of a Docker Container to SSH into it. SSH connections are established internally within the Docker network and do not require port forwarding or exposing.

Try this guide with our instant dedicated server for as low as 40 Euros