How to SSH into Docker Containers [Step

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

SSH is a network protocol that allows users to establish encrypted communications between two computers. Docker Containers are an application containerization platform and this article will show you how to SSH into them.

The “docker ssh into image” is a command that allows users to SSH into a Docker container. The “Step” section will show the steps necessary for using this command.

How to SSH into Docker Containers [Step

SSH is a wonderful choice for troubleshooting or getting a fast look inside a Docker container. SSH enables you to connect to a running container and see its contents fast. However, connecting to a Docker container requires some setup, and there are a few possibilities.

In this article, you’ll learn how to use the docker run command and a Dockerfile to SSH into Docker containers.

Let’s get this party started!

Prerequisites

If you want to follow along with the steps, make sure you have the following items:

  • A Linux server. Ubuntu 18.04.5 LTS is used in this lesson.
  • On the Linux host, Docker is installed. Docker v19.03.8 is used in this lesson.

How to Install and Use Docker on Ubuntu is a related article (In the Real World)

SSH into Docker Containers and start a Container with docker run

The docker run command is a Docker command that executes a command when a new container is launched for the first time. Follow the instructions below to start an interactive SSH connection to a container using docker run.

Make sure you have a Docker image downloaded and ready before you begin this part. This tutorial makes use of the most recent Ubuntu Docker image from Docker Hub.

To SSH into Docker containers using docker run, follow these steps:

1. On your local computer, open a terminal.

2. Run the docker run command with the following parameters:

  • (ubuntu container ssh) is the name of the container to execute.
  • The I parameter indicates that you want to start a live SSH session with the container. Even if the container is not connected, the I flag does not end the SSH connection.
  • The t option creates a virtual terminal that may be used to execute commands interactively.
  • The picture that will be used to make the container (ubuntu).

# Create the ubuntu container ssh container and start a Bash session. sudo docker run —name ubuntu container ssh -i -t ubuntu container ssh -i -t ubuntu container ssh -i -t ubuntu

Start a Bash session in the container called ubuntu container ssh.Start a Bash session in the container called ubuntu container ssh.

You’re now SSHed to the container and can execute any commands you want.

3. After that, use any command, such as touch. As seen below, the touch command will create a new folder called myfolder in the tmp directory.

In a Docker container, creating a new folder in the tmp directory.In a Docker container, creating a new folder in the tmp directory.

You may now execute any command you want!

Finally, enter quit to end the session when you’ve finished performing commands.

Docker exec allows you to SSH into running Docker containers.

You learnt how to perform SSH commands while launching a new Docker container in the previous section. But what if you need to SSH into an already operating Docker container? The docker exec command is executed.

The docker exec command generates a Bash shell within a running container, which is a convenient method to deliver SSH instructions to it.

How to Set Up an Apache Docker Container is a related article.

Make sure you have a Docker image downloaded and ready before you begin this part. This tutorial makes use of the most recent NGINX Docker image from Docker Hub.

With docker exec, you may SSH into a running Docker container:

1. On your local computer, open a terminal.

2. To start the container, use the docker run command. Make careful to use the -d parameter to keep the container running in the background until you delete it. The command below launches the nginx-testing container.

docker run nginx-testing —name nginx-testing -d nginx

Using the docker run command, create and launch a new container.Using the docker run command, create and launch a new container.

3. Verify that the container is operating by using the docker ps command. The docker ps command displays a list of all currently running Docker containers on the Docker host.

a list of all currently active containersa list of all currently active containers

4. Finally, use docker exec to SSH into the running container nginx-testing, as seen below. The following is a sample of code:

  • To obtain a Bash shell in the container, execute the docker exec command (/bin/bash).
  • The -it switch enables you to run a container in interactive mode, which means you may run commands within it while it’s running.
  • The container’s name is nginx-testing.

/bin/bash sudo docker exec -it nginx-testing

ssh into a container that is already runningssh into a container that is already running

Using a Dockerfile to set up an OpenSSH server and connect to it

Until now, the tutorial has assumed you’re connecting to a container with an SSH server already installed. What if it doesn’t work out? Perhaps OpenSSH isn’t already installed on the image you’re using, and you need to configure it first?

How to Run Startup Commands in Docker Containers is a related article.

You can specify all of the operations required to not just SSH into Docker containers but also to build up an OpenSSH server from scratch using a Dockerfile.

Assuming your local terminal is still open:

1. Create a directory to store the Dockerfile if desired. The /DockerFileContainerTest directory will be used in this tutorial.

2. Open your preferred text editor, put the Dockerfile below into it, and save it as Dockerfile under the /DockerFileContainerTest directory. This Dockerfile provides all of the tools and settings needed to generate a new Docker image and set up OpenSSH on any base image.

The DockerFile that follows provides the following steps/instructions for building the container:

  • FROM – Defines the base image to use, which is ubuntu:16.04.
  • RUN — Runs commands on top of the base image in a new layer.
  • CMD — You may use CMD to execute commands. Commands may be performed in one of two ways: through exec or via shell formats.

Associated:Executive vs. Shell Form

  • EXPOSE — Notifies Docker that the container is listening on the specified network ports during execution. On pot 22, the container will be seen.

# Instruction for Dockerfile to create a new image on top of the base image (ubuntu) FROM ubuntu:16.04 RUN apt-get update && apt-get install -y openssh-server RUN mkdir /var/run/sshd RUN echo ‘root:mypassword’ | chpasswd RUN sed -i ‘s/PermitRootLogin prohibit-password/PermitRootLogin yes/’ /etc/ssh/sshd_config RUN sed ‘[email protected]s*requireds*[email protected] optional [email protected]’ -i /etc/pam.d/sshd EXPOSE 22 CMD [“/usr/sbin/sshd”, “-D”]

3. Create the Docker image using the docker build command. The picture sshd container and is tagged with the t flag. Docker may now choose all essential files from the current working directory.

sudo docker build -t sshd tagged image # Creating the Docker Image

Creating a Docker image based on an Ubuntu imageCreating a Docker image based on an Ubuntu image

4. Run the docker images command to view the image you just built. The REPOSITORY property should be noted. The tag produced with the -t option in the preceding step is this attribute.

Execute the Docker images command.Execute the Docker images command.

5. Run docker operate to construct and run the container from the image in the background, instructing Docker to do so (-d),

The following command tells Docker to construct and run the test sshd container container in the background (-d), using the sshd tagged image freshly produced image you prepared in step 3, and to publish all ports specified in the Dockerfile as random ports.

# Using the freshly created image to launch the container docker run -d -P —name test sshd container sshd tagged image

Following the successful execution of the Docker run command, the container ID is produced as shown below.

Using the freshly produced image, start the container.Using the freshly produced image, start the container.

6. Verify SSH connection between the Docker host and the container by running docker port. The docker port command lists the container’s port mappings or a single mapping.

docker port test sshd container sudo docker

22/TCP 0.0.0.0:32769 should be shown, indicating that the container’s port 22 is mapped to the external port 32769.

The docker port command is being used.The docker port command is being used.

7. Next, look for the container’s IP (internet protocol) address. Run the docker inspect command to do so. The docker inspect command searches Docker information and uses a format option to output the results in a JSON array.

By checking in NetworkSettingsNetworks IPAddress, the argument for the format parameter below utilizes the range attribute to get the container’s IP (internet protocol) address.

argument for the format parameterargument for the format parameter

inspect —format=’range.NetworkSettings.Networks.IPAddressend’ docker inspect test sshd container

IP (internet protocol) addressIP (internet protocol) address

8. Finally, now that you have the IP (internet protocol) address to SSH to, try to SSH to the container, and it should work!

Attempt to SSH into the containerAttempt to SSH into the container

Conclusion

You should now be able to SSH into a Docker container using a variety of methods. You should be able to debug and manage your containers using one of these methods.

How do you intend to SSH into your container now that you have this newfound knowledge?

Docker Containers use a virtual machine to run the application. You can SSH into Docker containers with the “docker shell into container” command.

Related Tags

  • docker-compose ssh into container
  • ssh into docker container windows
  • docker connect to container
  • bash into docker container
  • ssh into docker container mac

Table of Content