How to deploy a Docker container Via SSH access

This tutorial is about How to deploy a Docker container Via SSH access. We will try our best so that you understand this guide. I hope you like this blog, How to deploy a Docker container Via SSH access. If your answer is yes, please do share after reading this.

So lets keep reading for intertesting info:

Check How to deploy a Docker container Via SSH access

The main reason you want to do this is for testing purposes. Maybe you’re testing infrastructure automation or deployment with something like Ansible, which requires SSH access to the target machine. SSH is always the default mechanism for remote shell access to a running Unix or Linux operating system from a terminal client to execute commands.

While SSH is familiar, Docker offers simpler, more user-friendly methods that don’t require you to run a container with an SSH server. In this post, we will introduce two methods for shell access to a Docker container using OpenSSH and the docker exec command. In this article, we will show you how to set up a Docker container with SSH access.

How to deploy a Docker container via SSH access

Starting a container and SSH into Docker Containers with docker run

The docker run command is a Docker command that runs the command the first time a new container arrives. Using docker run, you can start an interactive SSH session to a container by following the steps below.

To SSH into Docker containers with docker run:

  • Open a terminal on your local machine.
  • Run the docker run command by providing:
    • The name of the container to run (ubuntu_container_ssh)
    • The i flag indicates that you would like to open an interactive SSH session to the container. The i flag does not close the SSH session even if the container is not connected.
    • The t flag allocates a pseudo-TTY that can be used to run commands interactively.
    • The base image to create the container from (ubuntu).
  • Then execute any command, such as touch command. The touch command will create a new folder called myfolder in the tmp directory.
  • Now you can run any command you want!
  • Finally, when you’re done running the commands, type exit to log out.

SSH into running Docker containers with docker exec

In the previous section, you learned how to run SSH commands when starting a new Docker container. But what if you need to SSH into Docker containers that are already running? Run the docker exec command.

The docker exec command creates a Bash shell inside a running container and is a great way to send SSH commands to a container. To SSH into a running Docker container with docker exec:

  • Open a terminal on your local machine.
  • Next, run the docker run command to start the container. Be sure to specify the -d flag to run the container in the background to keep it alive until you remove it. The following command starts a container called nginx-testing.
    • sudo docker run –name nginx-testing -d nginx
  • Now run the docker ps command to verify that the container is running. The docker ps command will list all running containers running on the Docker host.
  • Finally, run docker exec, as shown below, to SSH into the running container named nginx-testing. In the following code snippet:
    • The docker exec command is run (/bin/bash) to get a Bash shell into the container.
    • The -it flag allows you to run a container in interactive mode, that is, you can execute commands inside the container while it is still running.
    • nginx-testing is the container name.
    • sudo docker exec -it nginx-testing /bin/bash

Setting up an OpenSSH server and connecting to a Dockerfile

For now, the tutorial assumes that you are connected to a container that already has some SSH server installed. But what if it isn’t? Perhaps the image you’re using doesn’t have OpenSSH installed yet and you need to configure it first?

With a Dockerfile, you can set up all the tasks required not only to SSH into Docker containers, but also to set up an OpenSSH server from scratch.

  • Optionally create a directory to store the Dockerfile. This tutorial will use the ~/DockerFileContainerTest directory.
  • Open your favorite text editor, copy and paste the following Dockerfile inside and save the file as Dockerfile inside the ~/DockerFileContainerTest directory. This Dockerfile contains all the commands and settings to create a new Docker image on top of any base image and configure OpenSSH.
    • The DockerFile below contains several steps/instructions that will build the container:
    • FROM: Defines the ubuntu:16.04 base image to use.
    • RUN – Run commands on a new layer on top of the base image.
    • CMD: CMD allows you to execute commands. There are two ways commands are executed, either through exec or using shell formats.
    • EXPOSE – Informs Docker that the container is listening on the specified network ports at runtime. The container will be exposed in canister 22.
  • Next, run the docker build command to create the Docker image. The t flag labels the sshd_container image y. allows Docker to select all the necessary files from the current working directory.
  • Now, run the dockable images command to inspect the created image. Note the REPOSITORY attribute. This attribute is the tag created with the -t flag in the previous step.
  • Run docker run to create and run the container from the image and tell Docker to run the image in the background (-d),
  • The following command tells Docker to create and run the container called test_sshd_container in the background (-d), using the newly created sshd_tagged_image that you created in step 3, and to publish all ports defined in the Dockerfile as random ports.
  • After the successful execution of the docker run command, you will see the container ID is generated below.
  • Run docker port to verify SSH connectivity between the Docker host and the container. The list of docker port commands are the port mappings or a specific mapping for the container.
  • You should see the output of 22/TCP → 0.0.0.0:32769, indicating that container port 22 is mapped to external port 32769.
  • Next, find the IP address of the container. To do this, run the docker watch command. The docker inspect command queries Docker information and returns the results in a JSON array using a format parameter.
  • You’ll see that the format parameter argument below uses the range attribute to find the container’s IP address by checking NetworkSettings→Networks→ IPAddress.
  • Finally, now that you have the IP address to SSH to, try SSHing to the container, and it should work!

Final words: How to deploy a Docker container Via SSH access

I hope you understand this article, How to deploy a Docker container Via SSH access. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.

James Hogan
James Hogan
James Hogan is a notable content writer recognized for his contributions to Bollyinside, where he excels in crafting informative comparison-based articles on topics like laptops, phones, and software. When he's not writing, James enjoys immersing himself in football matches and exploring the digital realm. His curiosity about the ever-evolving tech landscape drives his continuous quest for knowledge, ensuring his content remains fresh and relevant.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related Articles

Hubspot Service Hub review 2024: a comprehensive platform

When it comes to customer support operations, HubSpot Service Hub is an all-encompassing customer service platform that is meant to...
Read more
When players on Windows 11 or 10 try to log in to Steam, they may get the error code E87....
Users of Windows 11 or 10 may find it frustrating to deal with the error number 147-0 in Microsoft Office....
The Microsoft Store is an important part of the Windows operating system because it gives users a single place to...
It can be hard to find the right balance between usefulness, durability, and cost when it comes to kitchen storage....
Both AirDroid and Vysor are well-known tools that help Android users control their devices and mirror them. One of the...