Home Tech How To Do SSH Into Docker Container

How To Do SSH Into Docker Container [Step-By-Step Guide]

In this article, we will be discussing integrating ssh into docker containers. We will also be answering many questions you mind have about ssh and docker containers.

The Secure Shell Protocol (ssh), often known as the Secure Shell, is a cryptographic network protocol that allows users to use network services safely across an unprotected environment.

Its most noteworthy applications are command-line execution and remote login, both of which are described below. SSH applications are built on a client-server architecture, which connects an SSH client instance to an SSH server instance.

Even though SSH is among the most often used tools in a sysadmin’s toolkit, it is not frequently seen in conjunction with Docker. So let’s talk about how to SSH into a working container and what you should consider before doing so.

Using SSH in Conjunction With Docker Containers; Do I Recommend It?

The first question that comes up in integrating ssh into docker containers is, is it recommended? Setting up an SSH server within a Docker container is widely considered to be a terrible practice, and you should avoid doing so. However, it’s almost always preferable to utilize the docker exec instruction when getting a shell inside a container.

Docker novices may be tempted to utilize SSH to update files within a container, which is not recommended.

Containers, on the other hand, are intended to be disposable and should be viewed as immutable after formation, except for persistent data stored within volumes. When you make changes to the source code, you should make a new image and relaunch the container.

Aside from the time-consuming configuration process, including adding multiple dependency packages, deploying SSH in a Docker image introduces another possible attack vector.

Managing several autonomous SSH processes on a network of interconnected active containers requires you to remember the right connection for each carrier on a system with many active containers.

As an alternative to installing SSH on every container, run it only once on the physical machine hosting Docker.

Then, access your host via SSH and execute docker exec -it my-container bash to enter individual containers using the docker command.

Although docker exec is the primary way, there are several situations in which SSH may be helpful. For example, you may use it as a stopgap method to interact with legacy deployment platforms while developing a more permanent solution.

Some integrated development environments (IDEs) and build systems may also take advantage of it to enable live reload capabilities while creating.

How to Configure SSH into Docker Container

Let’s discuss how to set up an ssh server on Windows using the Docker desktop application.

If you don’t already have docker desktop for Windows or the windows subsystem for Linux installed, you can go ahead and start the installation process now.

The first thing you’ll need to do is install Docker on your computer or laptop. Once the docker desktop is installed, everything is running and configured to be accessed through an actual Ubuntu 20.04 Linux subsystem setup; you should be ready to start working on your projects.

If you don’t have Ubuntu already downloaded, issue the command.

Now, enter the command docker images to see if you have the latest ubuntu image.

docker images

ssh into docker containerHow to Start the Docker Container

To begin, you must run a docker container. When the container exits, remove it from your system to ensure it is no longer present. Then, using the terminal, start up the docker container in interactive mode to see what is going on.

You have the opportunity to accomplish something unique here. First, assign the localhost to port 8022 from the localhost to the actual docker container by using the localhost to port 8022 commands. Then, for convenience, give the Docker container a name.

Run the latest version of Ubuntu and run bash on startup.

Here is the command you should enter:

docker run -- rm -it -p 8022:8022 --name sshd ubuntu: latest bash

After you’ve entered everything, press enter, you should receive a prompt, and then you should be ready to go.

Clearing the screen is the first thing you should do before proceeding with the process.

How to Install the Necessary Software Packages in SSH Into Docker Container

To install the software package, the very first thing you should do is enter the app-get update.

app-get update

If you’re not on a container, you might need to use sudo. So you’ll put in sudo, and then you’ll put your root password in, or you can sudo su first, and then you can issue all the commands as root.

But on the docker container, we’re going to be utilizing root.

When the update is done, you can clear the screen. Then, you can proceed to do an app get now on a non-docker system. Then you would need to put in a -y so you’re not prompted to say yes to install things every time.

Go ahead and install the open ssh server, and you would install vim so that you have an editor to use.

You can also install nano here if you want. But you would be installing ssh as well. You could choose to install the uncomplicated firewall as well.
With that done, you hit enter.

apt-get -y install openssh-server vim ssh ufw

Expect to wait for a little as it takes a while to get done. You would return to the setup during the installation because it would ask you a couple of questions.

It would ask for a geographic area, so go on and choose your geographic location. In this example, we put America.

How To Do SSH Into Docker Container [Step-By-Step Guide]

We also choose New York, so we select 105 for the time zone.

How To Do SSH Into Docker Container [Step-By-Step Guide]This completes the installation successfully. So go ahead and clear the screen. The next thing that we are going to do is update our ssh’s configuration not to utilize the commonly known port 22 that ssh runs on.

How to Set up SSH Port Access

The next step in integrating ssh into docker container would be setting up SSH port access.

We will put it on an upper-level port above 1024 for security purposes. So the way you do that is you’re going to use vi you can use nano which we installed initially. Then you’re going to go to the ssh update ssh d underscore config file.

vi /etc/ssh/sshd_config

Come down to this port here, open it up and port as shown in the figure below.

How To Do SSH Into Docker Container [Step-By-Step Guide]

Then input 8022 instead. You can make use of any port above 1024. Just ensure that it does not conflict with other services you have already configured.

How To Do SSH Into Docker Container [Step-By-Step Guide]

Go ahead and save the file and clear the screen.

The next thing you’ll want to do is open up port 8022; we had ported from our local system to the container when we started up this container.

Setting up Firewall Services

To set up firewall services when integrating ssh into docker container, go ahead and open that up this firewall; here, we will do an uncomplicated firewall.

You are going to key in, allow 8022.

allow 8022

You will see that the rule has been updated, and we’re good to go. So the next thing you should do is to clear the screen.

How to Start Up the SSH Service

To start up the ssh service when working with ssh into docker container,

You are going to do a system or enable it. You’re going to do a system ‘ctl enable ssh.’ Once that’s complete, you’ll do a system ctl start ssh. You could get an error saying you need to use the init system.

So instead, you’ll do a slash etsy inet.d ssh start, and there, you’ll have the ssh started.

How to Create Private and Public Keys

Now let us create the ssh keys operating with ssh into docker container. The public key takes place on the server, and the private key to put on our clients.

We do this by typing in ssh keygen, and we’re going to add a bit size of 4096 to keep it nice and secure.

ssh-keygen - 4096

Now it will ask you what you want to call the file. In this example, let’s keep it at default as id_rsa; it will ask for a passphrase we will put in a passphrase. So let’s just put a nice simple one and confirm it, and that’s it, we are done.

How To Do SSH Into Docker Container [Step-By-Step Guide]

Please use a complex passphrase for proper security. We chose a simple passphrase for the purpose of the example.

We now have an rs8 key that is 4096 bits, nice and secure. So let’s now go ahead and display with a cat from the home directory ssh, with that rsa key that we just created.

cat~/ .ssh/id_rsa

We would grab this entire file

How To Do SSH Into Docker Container [Step-By-Step Guide]

and copy it to the clipboard dragging it over the desktop. You are going to right-click and do a new text file.

Let’s call it private key. When we open it up, we will paste in that private key.

We would make sure that we have the end comment down here with the dashes and the beginning comment at the top with the dashes. We’re all good there. Now we are going to save the file.

You can close this out and move the clipboard away. Clear the screen to make it nice and clean.

How to Add Public Key to the Authorized Keys File

The next question we will answer in our integrating ssh into docker containers guide is how to add a public key to the authorized key files.

What we want to do now from our local directory ssh; you would cut out rsa.hub file and pipe it into authorized keys.

cat ~/ .ssh/_rsa.pub > ~/ .ssh/authorized_keys

Note: I did not have an authorized key file since I was on a brand new installation. However, if you are not on a new system, do not use the single ‘>’; instead, use the double angle bracket ‘>>’ to append the to the authorized key file.

cat ~/ .ssh/_rsa.pub >> ~/ .ssh/authorized_keys

How to Install Putty

The next thing we need to do because we’re on a windows system is download putty. Since we’re on windows, we’re going to have to do a little manipulation to our private key to get this to work and be able to access our ubuntu server that’s running the ssh daemon.

So, we begin by opening up Google and typing in putty download for Windows. It would be best if you chose it and then downloaded putty.

How To Do SSH Into Docker Container [Step-By-Step Guide]

The next step is for you to grab it from here, and the best bet is, first of all, since we’re running ubuntu 2004, you need to be at the latest version. So let’s download the 64-bit MSI file in our case.

Run it through a quick check for viruses and malware, and you will be ready to install it. Then go ahead and grab the MSI file, go through the install process, and install it.

How to Create Windows for .PPK File

The last question we would be answering in this integrating ssh into docker containers guide is how do we create Windows for our .ppk file.

Under your start menu, you should find all the different putty elements. You can put them on your desktop just for ease.

The first one you want to open up is the puttygen double click on it. We would then open up and load our private key file, so if we go and look for that private key file, we’ll see that there’s nothing in there. So you need first to set this to all files to find it.

How To Do SSH Into Docker Container [Step-By-Step Guide]

Grab the private key and open it up.

How To Do SSH Into Docker Container [Step-By-Step Guide]

It’s going to ask you for that password. Then, type in the passphrase you created. It would have all of your information.

How To Do SSH Into Docker Container [Step-By-Step Guide]

You should save that private key. You can give it the same name that we gave the private key, but instead, there will be no extension.

Final Thoughts

This has been a guide on integrating ssh into docker container. We answered many frequently asked questions and gave you elaborate explanations.

This guide is simple to understand as we have written out the codes and the diagrams you would need.