How to SSH into a Docker Container

Docker has become an important tool for developers and system administrators. One common task is accessing the shell of a running Docker container, often referred to as SSHing into a container. It gives you direct access to its environment to manage and optimize your containerized applications. In addition, this process allows you to troubleshoot issues, run commands, inspect files, install additional software, and monitor the container’s performance. 

In this article, we will walk you through the possible methods of SSHing into a Docker container.

How to SSH into a Docker Container?

SSHing into a Docker container involves using the Secure Shell (SSH) protocol to access and interact with a running container. It provides a powerful way to interact with your Docker environment.

Prerequisites

To SSH into a Docker Container, make sure you meet the following pre-requisites:

  • You must have Docker installed on your Ubuntu machine.
  • A running Docker container is required before you SSH into a Docker container.
  • You must have a basic knowledge of the command-line interface (CLI).

Method 1: Using docker exec

The most simple way to access a Docker container’s shell is by using the docker exec command. Moreover, this method lets us ssh docker container from host, which allows accessing the container’s shell from the host machine. This method does not require SSH and is recommended for most use cases.

Step 1: Find Your Container ID

First, you need to identify the container you want to SSH into. Now, list all running containers and note the ID of the one you want to access such as 4d279546e295:

docker ps
finding your container id

Step 2: Access the Container

Docker does not include SSH by default, but you can use the docker exec command to get a shell inside the container. Let’s open the shell inside the container:

docker exec -it 4d279546e295 /bin/bash
accessing the container

Replace the container ID with the actual ID of your container.

Alternatively, you can SSH into the container using its IP address. To do so, use the ip a command to find out the IP address of the machine:

ssh [email protected]
ssh into container

Method 2: Using docker attach

Another method to SSH into the Docker Container is possible via the docker attach command. This command attaches your terminal to a running container’s main process. This allows you to interact with the container’s console in real-time.

To SSH into a Docker Container, follow the below steps:

Step 1: Create Attach Container

To enable “docker attach” on a container, you need to start it in detached and interactive mode. It is possible through the -dit option along with the docker run command.

Let’s create a new container named “attach-test” based on the “ubuntu” image. Then, configure it for docker attach:

docker run --name attach-test -dit ubuntu
create and configure container

Step 2: Attach to Container

To connect to the container, use the docker attach command by specifying the container name. In our case, the attach-test is already running container:

docker attach attach-test

This opens a terminal session directly inside the container, allowing you to execute commands and monitor its activity:

attaching to container

This method attaches to the container’s main process, which might not be a shell. If the main process exits, the container will stop.

Method 3: Using Docker Compose

If you are using Docker Compose, you can configure your docker-compose.yml file to include an SSH server. Let’s SSH into a Docker Container via the configuration file:

Step 1: Edit docker-compose.yml

First, access the docker-compose.yml file in the nano editor: 

sudo nano docker-compose.yml

After that, add the SSH server installation and start commands to your service definition:

services:
myservice:
image: nginx
command: /bin/bash -c "apt-get update && apt-get install -y openssh-server && service ssh start && /bin/bash"
ports:
- "2222:22"
edit docker-compose file

Step 2: Deploy the Service

Now, start your services with Docker Compose:

sudo docker-compose up -d
deploying the service

Step 3: SSH into the Container

Finally, use the mapped port such as 22 to SSH into the container:

ssh username@localhost -p 22
ssh into container

Method 4: Using docker run

The docker run command is the primary tool for creating and starting new Docker containers. To immediately access a container’s shell once it’s created, you can use the -it flags.

To create a container named run-test based on the Nginx image and directly access its Bash shell:

docker run --name run-test -it nginx /bin/bash
using docker run

That is all from how to SSH Docker Container with possible methods.

Conclusion

While Docker doesn’t support SSH by default, there are several methods to access a container’s shell. The docker exec command is the most straightforward and recommended approach. However, if you need to use SSH specifically, you can run an SSH daemon inside the container or use Docker Compose. In this article, we discussed different methods to SSH into a Docker Container.

Learn how to SSH into your Docker containers effortlessly with UltaHost’s Self-Managed SSH VPS Hosting. With full root access and SSH keys, you gain complete control over your container environment. Enjoy unmatched flexibility and seamless performance scaling to manage your Docker operations with ease!

FAQ

How can I effectively SSH into a Docker container?
Can I use the docker attach command to SSH into a container?
Is it possible to run an SSH daemon inside a Docker container?
How do I install an SSH server inside a Docker container?
How do I start the SSH server inside a Docker container?
How do I expose the SSH port when running a Docker container?
Can I use Docker Compose to run an SSH server inside a container?

Related Post

How to Add SSH Key to Visual Studio Code

Adding an SSH key to Visual Studio Code (VS Code) is a ...

How to Share Data Between Docker Containers

Docker is a containerization platform that enables us t...

How to Push and Pull a Docker Image from Dock

Docker is an open-source platform that enables develope...

How to Check and Manage Logs on Docker Compo

Managing logs in Docker Compose is essential for develo...

How to Install PuTTY in Windows

PuTTY is a free and open-source terminal emulator that ...

How to Install Docker on Debian

Docker is a powerful containerization platform that all...

Leave a Comment