How do I connect to a running Docker container via SSH and run commands?

This tutorial describes the SSH Docker container command running tutorial

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

How Do I Run SSH Commands in a Docker Container? This guide will show you three ways to SSH into a Docker container and run commands.

Prerequisite

  • A Linux system running Docker
  • The preconfigured container is loaded and running
  • Access the terminal window/Command Prompt (Ctrl+Alt+T or Ctrl+Alt+F2)
  • A user account with sudo permissions

Method 1: Run the docker exec command in a Docker container

How does SSH connect to a Docker container and run commands? The docker exec command runs the specified command in a container that has already been run. You can use it to SSH to connect to Docker containers by creating a bash shell (a shell where you can type commands).

The basic syntax for docker exec to run commands in a container is:

docker exec [options] [container] [command]

How Do I Run SSH Commands in a Docker Container? If you haven’t already, pull a Docker image first. For example, you can load Nginx:

sudo docker pull nginx

Then, run the image:

sudo docker run ––name nginx–test –d nginx

List all running containers for verification:

sudo docker ps

You should now see that your nginx-test image is loaded.

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

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

You are now logged in to the nginx-test container. As a result, any commands you enter will be executed in that container. The –i option specifies the interaction and –t enables the terminal input interface.

Method 2: Use the docker attach command to connect to the running container

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

sudo docker attach container_Name

How Do I Run SSH Commands in a Docker Container? In the following example, the system will connect to the nginx-test container:

sudo docker attach nginx-test

Once the command is executed, you’ll work in the container. Any command you run will affect your virtual Docker environment.

Method 3: Use SSH to connect to a Docker container

How does SSH connect to a Docker container and run commands? You can use SSH (Secure Shell) to connect to Docker containers. Generally, SSH is used to connect to a server remotely over a network. The technology works the same way when connected to a virtual Docker container on a system.

Important: We don’t recommend this method because it will inflate the image beyond the normal range. You need to have an image that has SSL configured for it to work.

Step 1: Enable SSH on your system

SSH Docker Container Run Command Tutorial – First install and enable the SSH service:

To enable SSH on Ubuntu 18.04:

sudo apt-get install ssh

sudo systemctl ssh start

sudo systemctl ssh enable

service ssh status

To enable SSH on CentOS 7:

yum –y install openssh-server openssh-clients

service sshd start

service sshd enable

service sshd status

Step 2: Obtain the IP address of the container

How Do I Run SSH Commands in a Docker Container? Use the docker inspect command to get the IP address of the container and filter the results.

For a modern Docker engine, use the following command:

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

For older Docker engines, run:

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

The IP address will be displayed as shown in the image above.

Note: The target docker container must be running to get its IP address. If you need to start an existing docker container, run sudo docker start container_name.

Step 3: SSH into the Docker container

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

ping –c 3 172.17.0.2

To connect to a mirror using the SSH tool:

ssh root@172.17.0.2

You should be prompted to enter the root user password for the container. If it says Connection denied, you may not have configured a container for SSH. If the prompt changes, you’re now SSH connected and can run the command in the container.

SSH Docker Container Running Command Tutorial Conclusion

How does SSH connect to a Docker container and run commands? Docker containers are lightweight and transitive, so traditional SSH connections are not recommended. The recommended way to run commands in a Docker container is docker exec or docker attach.

If you want to configure multiple remote VMs, you can use the docker-machine ssh command to connect to the VMs via Docker. For most users, the first two command methods are recommended.