How to SSH into a Docker Container and Run Commands

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Introduction

Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers.

Docker is a utility that lets you create a container for running applications. A Docker container is a fully-contained virtual machine.

This guide will show you three methods to SSH into a Docker container and run commands.

Method 1:

  • Use docker exec to Run Commands in a Docker Container

The docker exec command runs a specified command within an already running container.

You can use it to SSH into a Docker container by creating a bash shell.

Syntax for using docker exec

docker exec (options) (container) (The_command)
  • Pull a Docker image if you haven’t. you can load apache for example:
sudo docker pull httpd
  • Then run the container with
sudo docker run --name apache-test -d httpd
  • List all running containers to check if the container listed or not
sudo docker ps
  • To access it and run commands in that container, run
sudo docker exec -it apache-test /bin/bash

The –i option specifies interactive

The –t enables a terminal typing interface.

Now, any commands you enter will run in that container.

Method 2:

  • Use the docker attach Command to Connect to a Running Container

The docker attach command links a local input, output, and error stream to a container.

  • To connect to a running container, run
sudo docker attach (The_container_Name)

Now, we will connect to the apache-test container with the following command line.

sudo docker attach apache-test

Method 3:

  • We will use SSH to Connect to a Docker Container

You can connect to a Docker container using SSH (Secure Shell).

  • Install, start and enable ssh

On CentOS:

dnf install -y openssh-server openssh-clients && systemctl start sshd && systemctl enable sshd 

On Ubuntu:

sudo apt-get install ssh && sudo systemctl start sshd && sudo systemctl enable sshd 
  • Get IP Address of Docker Container

Get the container’s IP address by using the docker inspect command and filtering out the results.

For modern Docker engines, run

sudo docker inspect -f "{{ .NetworkSettings.IPAddress }}" (The_container_name)

For older Docker engines, run

[root@unixcop ~]# docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' (The_container_name)
172.17.0.1
[root@unixcop ~]# 
  • SSH Into Docker Container by using the SSH command to connect to the container
ssh [email protected]

The system should ask for a password of the root user for that container

Now, you can run commands in the container.

Conclusion

So, That’s it

In this tutorial, we shew you how to SSH into a Running Docker Container and Run Commands via three methods.

Thanks

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
MQ-Jr
MQ-Jr
unixcop Admin

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook