DevOps

How to SSH into a Docker Container and Run Commands?

Introduction

Docker containers are the best way to run software or applications neatly. Whether it is about initial configuration or important modification, an interaction with the docker container is an essential step. Such interactions can be done in a variety of ways, and we have chosen the SSH approach for this article. This insightful article is all about establishing connections in a container through the docker exec command. Our focus is on the three major methods to build a connection to the docker container via SSH. Let’s jump right into the first method.

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

The use of the docker exec command in the container is a process where the user gets to access an already running container. Access is used to perform certain actions, such as inquiring about the status of software installation. First of all, we will ensure whether the container that we intend to target is running or not. 

Use docker exec to Run Commands in a Docker Container

In the above syntax, the command “docker run” will start a new docker container. The term “-d” will let the container run in the background while the user instructs the system through the terminal window. Moving on, the term “–name my_container” will be replaced by the user’s chosen name of the container. The last term, “my_image” is replaced with the required name. Here, the docker image is a template that helps run the application inside the docker.

Now, we will use the docker exec command when the container is ready to run. 

Use docker exec to Run Commands in a Docker Container

We will replace the term “my_container” with “smart techie” as the name of the container. The term “-it” is an option that will instruct the system to create a pseudo-terminal that can establish interaction with the docker container. The reason behind the creation of a pseudo-terminal is to offer isolation to the docker so that the interaction is not held through the ongoing terminal, which might confuse and cause harm to the process. Now, the command to interact with the docker is placed as “inspect the configuration of the docker container, “Smart techie.” Let’s look at the output below.

Use docker exec to Run Commands in a Docker Container

Also Read: How to Kill a Process in Linux Command – Complete Guide

Method 2: Use the Docker Attach Command to Connect to a Running Container

This method is suitable for a container that is already running. We will straightaway get to the terminal and use the below command to attach the running docker to the terminal. Here, the name of the container is “smart techie.” When the user is done with the modification process, he simply needs to press Ctrl+P to detach from the container.

Use the Docker Attach Command to Connect to a Running Container

Method 3: Use SSH to Connect to a Docker Container

This is another way to establish a docker SSH connection for the remote system. The most basic step of this process is to enable SSH on the system. A Linux-based system has pre-installed SSH, while a Windows-based system needs a quick installation to use it. Let’s go through each point in detail.

Set up SSH 

This step is important when SSH is not installed on the system. We will start by checking the status of the SSH on Ubuntu and then CentOS, respectively, through the below command. 

For Ubuntu:

Use SSH to Connect to a Docker Container

In the above command, “dpkg -1” is used to invoke the Debian package management system. If SSH is not already installed, then the following command will help with installation.

Use SSH to Connect to a Docker Container

For CentOS:

Use SSH to Connect to a Docker Container

In this command, “rpm” is used for RPM package manager, while the term “-q” is used for checking the status of the package. If SSH is not already present on the CentOS then the following command will ensure the installation of SH on CentOS.

Use SSH to Connect to a Docker Container

Now, the next step will lead to the search of the IP address of the container.

Track the IP address of the container

This step is essential as it connects the right docker container via SSH. It is just like being equipped with the right address to visit their home. Look at the below command to get the correct IP address.

Use SSH to Connect to a Docker Container

The user will have to replace “my_container” with the required container name. The command “docker inspect” is used in the syntax to instruct the system to look out for the IP address of the given container. 

SSH Into Docker Container

The last step of this process is related to the docker ssh connection. In the below syntax, the user has to replace the right IP address with “container_ip.”

Use SSH to Connect to a Docker Container

The process has ended, but the user needs to keep in mind that the IP address provided must match the container, and SSH should be installed beforehand.

Also Read: How to Install PIP on Mac? (Step By Step Guide)

Conclusion

We have tried our best to give our readers insights into three methods for establishing a stable and effective interaction with Docker containers via SSH. The first method, using the “docker exec command,” allows users to access running containers and perform actions within them. The second method involves using the “docker attach” command to connect to a running container. Finally, the third method demonstrates how to connect to a docker container via SSH, which involves setting up SSH if not already installed, finding the container’s IP address, and then initiating an SSH connection. These methods offer flexibility for managing and interacting with Docker containers, catering to various use cases and preferences.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *