Let’s See How to SSH into a Running Docker Container and It’s Illustration:

kubernetes
Reading Time: 3 minutes
How to Run Docker Containers - Explained with Examples

Hello Readers… I hope you all are doing fine. So in this blog we are going to learn another and interesting topic of docker. Stay tuned.

Introduction:

Docker is a utility that you can use to create a container to run your application. A Docker container is a fully contained virtual machine.

I am sharing with you all the three ways to SSH into a Running Docker container with it’s illustration:

Requirements:

  • Linux system with Docker
  • Preconfigured container to be loaded and run
  • Access Terminal Window / Command Prompt

Method 1: Use docker exec to Run Commands in a Docker Container :

The docker exec command executes the command specified in a container that is already running. You can use this to send SSH to a Docker container by creating a bash shell.

The basic syntax for using docker exec to run a command in containers is:

docker exec [options] [container] [command]

So; Start by pulling a Docker image if you haven’t already. For example, you can load Nginx:

sudo docker pull nginx

Then, run the image:

 docker run ––name nginx–test –d nginx


List all running containers to verify:

 docker ps -a

To get access and run commands in that Docker container, type the following:

 docker exec –it nginx-test /bin/bash 

You are now signed in to the nginxtest container. Therefore, all the commands you enter will be executed in this container. The –i option specifies interactive and –t enables the interface for terminal input.

Method 2: Use the docker attach Command to Connect to a Running Container:

The docker attach command links local inputs, outputs, and error streams to the container. By default, it starts in a bash shell. To connect to a running container, type:

sudo docker attach container_Name 

let’s take one example of nginx:

 docker attach nginx-test 

When the command is executed, it works in the container. Every command you run affects your Docker virtual environment.

Method 3: Use SSH to Connect to a Docker Container :

Step 1: Enable SSH on System :

Start by installing and enabling the SSH service:

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

Step 2: Get IP Address of Container :

There are one command to get ip address of container i.e

sudo docker inspect -f "{{ .NetworkSettings.IPAddress }}" container_name

Let’s take one example:

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nginx-test

Here; we get a ip address of container;

Step 3: SSH into Docker Container :

Ping the IP address to make sure it’s available:

ping –c 3 172.17.0.2

Use the SSH tool to connect to the image:

ssh root@172.17.0.2

If the connection is denied, the container may not be provisioned for SSH. If the prompt changes, you can connect via SSH and execute commands in the container.

Conclusion:

So; In this blog we know how to play running docker container with ssh commands. Docker containers are lightweight and transitional, so a traditional SSH connection isn’t recommended.Most recommended method to run commands in a Docker container is either docker exec or docker attach.

Written by 

Mayuri Dhote is a Software Consultant at Knoldus Software. She has completed her MCA from VIT University. She has very dedicated towards her work. She has always ready to learn new things. Her practice area is Devops. When not working, you will find her writing poems and poetry.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading