SSH into a Docker Container

Hi there! Here we will see how to enable SSH into a Docker Container.

Firstly let’s see what Docker and SSH are

What is Docker?

Docker is a containerisation tool that helps run an application in an isolated environment.

Some Important terms that you will get while reading/listening to anything related to Docker

  • Docker File: Contains all the required commands to build an image.
  • Images: A Template used to “build” a “container”. We can say similar to a snapshot we take in a VM.
  • Containers: A Running instance of a docker image. Similarly, we can say running the snapshot taken in VM.
  • Deamon: Docker’d’ listens to events and manages images, containers, networks, and volumes.
  • And Many More….  For more terms, you can visit

What is SSH?

SSH Stands for Secure Shell or Secure Socket Shell, SSH is a Network Communication Protocol that provides a method to log in and perform operations on remote machines.


  1. Docker for Desktop.

You can find the steps to install Docker for Desktop from the below link:

Let’s dive into how to enable SSH into a Docker Container(Note: I’ll be using Ubuntu OS for this setup you can use any, just make sure you do the same operations as mentioned.)

Step 1). Generate Public and Private Keys for Authentication

For this, we’ll be using the ssh-keygen command

After hitting this command, you will be asked for some inputs from your end.

I’ve given the path of the current folder and the name of the file as the id.

Once the command completes executing, you will see 2 files id and

 id is a private key file used to log in from the host machine and is the public key file used to authenticate the host to the remote machine.

Step 2). Create a docker file(touch Dockerfile).

From UbuntuWe’ll be using Ubuntu as the base image.
RUN apt-get updatefetches the latest version of the package list from your distro’s software repository
RUN apt-get install -y openssh-serverInstall OpenSSH Server
RUN mkdir /var/run/sshdDefault folder for SSH to put logs
RUN sed -i ‘s/PermitRootLogin prohibit-password/PermitRootLogin yes/’ /etc/ssh/sshd_configEditing sshd_config file
Allowing the root user to log in with SSH.
RUN mkdir /root/.sshDefault SSH directory where the public key file will be stored.
WORKDIR /root/.sshMarking /root/.ssh as a Work Directory
COPY .Copy i.e. public key file to the image in /root/.ssh file
RUN cp authorized_keysCreating authorized_keys file and copying the content of public files in authorized_keys file. By default, SSH uses authorized_keys to authenticate clients.
RUN rm id.pubDeleting a public key file from an image as it is of no use in it.
RUN touch known_hostsCreating known_hosts file.
CMD [“/usr/sbin/sshd”, “-D”, “-e”]Starting SSH Server.

Step 3). Build Docker Image

Command: docker build -t ssh:1.0 .

  • -t Option is used to tag the image, As per the above command our Image will be tagged as ssh:1.0
  • dot(.) is a file path Where your Dockerfile is available. In my case, I’m in the same folder where the file is Available.

Step 4). Run Docker Image.

Command: docker run -d  -p 4242:22 ssh:1.0

  • -d Option is used to run the image in a detached Mode.
  • -p Option is used to expose Ports on the host Machine. Here in our command, The service running on port 22 inside the container will be exposed to the host machine at port 4242.
  • The Last Option is the image name i.e. ssh:1.0.

Step 5). Try to log in to the container with SSH.

Command: ssh -i <private_key_file_path> user_name@host_machine_ip -p 4242.

  • private_key_file_path will be a path to your Private key file.
  • user_name will be root.
  • host_machine_ip will be IP of your(host) machine.(For Linux you can use ifconfig command to get the IP of your machine)


In this blog, I have explained how to enable SSH in a docker container.

Hope You enjoyed this article😃!

Leave A Comment

Your email address will not be published. Required fields are marked *