Open In App

How To Use SSH Keys Inside Docker Container?

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

SSH key is a credential used for remote access of servers without using any type of username and password and Docker is a containerization tool used to encapsulate the application with its dependencies into compact units called docker containers. In this guide, I will first discuss what is SSH key and how it works. Then I will discuss docker. After that, I will walk you through the different steps to use SSH keys inside a docker container.

What is an SSH Key?

SSH Key is an access credential similar to a username and password. These keys are mainly used in any automating process. SSH keys have two parts: public key and private key. Here the private key is placed on the host machine while the public key is placed on the server. Whenever the host machine tries to access the server, here first the server recognizes someone is trying to access the server. So in response server sends an encrypted message with the public key to the host machine and then the host machine will decrypt it with its private key correctly to get access to the server. This whole process is automatic without any manual user intervention. After this host machine can successfully access the server. These SSH keys enhance security by using strong cryptographic algorithms. These are widely used in the automated process. SSH key also reduces the overhead of password management for managing access to multiple servers. In summary, we can say SSH keys are important tools for secure remote access to a server.

What is Docker?

Docker is a containerization tool that basically encapsulates the application and its dependencies into compact units called Docker containers. These Docker containers contain the code, runtime, libraries, and other essential tools that is needed to run the application. Here users first write a simple Dockerfile mentioning the base image, work directory, tools to install, and commands to run the Docker container. Then this Dockerfile is built to create a Docker image. These docker images are lightweight and portable. Docker images can be run in any operating system but the only condition is to install docker on that system. This allows the developers to run their applications without facing any issues in any operating system or any specific hardware requirements. Multiple docker containers can be run on a single host machine which results in maximizing resource utilization and reducing infrastructure costs. In summary, we can say Docker has become a very important tool for developers and organizations for software deployment and delivery pipelines.

Pre-requisites

Before moving to next next section make sure that you have installed Docker on your system. If Docker is not installed then follow these detailed geeks for geeks articles to install Docker on your system.

Steps To Use SSH Keys Inside Docker Container

Step 1: Run an Ubuntu docker container using the below command.

 docker run -it -d -p 2200:22 --name ssh-access-server ubuntu:latest

ubuntu

Step 2 : Go inside the docker container .

docker exec -it ssh-access-server bash

inside-container

Step 3 : Use the commands below to install openssh-server and nano on the Ubuntu docker container .

apt update
apt install openssh-server -y
apt install nano -y

openssh

Step 4 : Edit the sshd_config file . Here make the PermitRootLogin to yes .

nano /etc/ssh/sshd_config

nano

Then search for PermitRootLogin and make it yes .

permit

Step 5 : Then start the SSH service on Ubuntu docker container .

service ssh start

service-ssh-start

You can use this command below to check the status of SSH .

service ssh status

status

Step 6 : Now change the password of the Ubuntu docker container .

passwd

passswd

Step 7 : Now open another terminal on your host and write this command to access the Ubuntu docker container through SSH . Then you have to give the password you have set for Ubuntu docker container .

ssh root@localhost -p 2200

ssh access

Now you have successfully accessed the Ubuntu docker container through SSH .

Conclusion

Here in this article you have first learned about what is SSH keys and its use . Then you have learned about what is docker . After this you have created an Ubuntu docker container and installed openssh-server on this container . Then you have made some changes in the SSH configuration file . Finally you have accessed the Ubuntu docker container through SSH on a different terminal .

How To Use SSH Keys Inside Docker Container – FAQ’s

How to generate SSH key inside a docker container ?

First install OpenSSH on the docker container and then you can generate SSH key using commands like ‘ssh-keygen’

Why private keys are not directly included in the Docker image ?

Private keys are not used directly in Docker image because it is not safe to expose such important credential in a docker image . Attackers can gain the information if they get access of the docker image .

In which location SSH config file can be found on Ubuntu machine?

You can find the SSH config file in the /etc/ssh/ path on the Ubuntu machine .

What are different parts of SSH key ?

SSH key consists of two asymmetric keys that is public and private keys .

What are the advantages a docker container used for testing environment provides ?

This allows the users to experiment SSH configurations without affecting the production system .



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads