Running SSH Server in a Docker container

Sathvika Kolisetty
3 min readJul 6


In today’s technological landscape, Docker containers have revolutionized the way we deploy and manage applications. Docker enables us to encapsulate applications and their dependencies into lightweight, isolated containers that can run consistently across different environments. One common use case is running SSH within a Docker container, which allows secure remote access to the container.

Running SSH in a Docker container allows you to execute commands, transfer files, and interact with the container from a remote machine, providing a flexible and convenient way to manage containerized applications.

The process of running SSH in a Docker container on Amazon Linux within the AWS ecosystem. This blog will cover creating a Dockerfile, building the Docker image, running the container, and establishing an SSH connection to the container. Additionally, It also a best practice to ensure security and optimize performance when using SSH within Docker containers on AWS.

Step 1: Installing Docker on AWS Amazon Linux

First, connecting to Amazon Linux instance using SSH. To install Docker on Amazon Linux, execute the following command. Once Docker is installed, start the Docker service.

Installation of Docker on AWS Instance

Step 2: Create a Dockerfile

To begin, we need to define a Dockerfile that describes the steps to build our Docker image. The Dockerfile specifies the base image, installs the necessary packages, configures the SSH server, and starts the server as the container’s main process.

FROM ubuntu:latest
RUN apt-get update && apt-get install -y openssh-server
# Configure SSH
RUN mkdir /var/run/sshd
RUN echo 'root:redhat' | chpasswd
#password for user login
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# Start SSH server
CMD ["/usr/sbin/sshd", "-D"]

Step 3: Build the Docker Image

Save the Dockerfile in a directory . Navigate to the directory containing the Dockerfile and execute the following command to build the Docker image

docker build -t ssh_on_docker .
Building Docker Image

Step 4: Run the Docker Container

Once the Docker image is built, now we need to run the container. Execute the following command. This command runs the container in the background and it ports to host on port no 3000 to the container's port 22, and uses the ssh_on_docker:latest image.

docker run -dit -p 3000:22 --name ssh_docker ssh_on_docker:latest
Running Docker Container on port 3000

Step 5: Connect to the SSH Server

With the Docker container running, now we can connect to the SSH server inside it. In your host machine and execute the following command. This command establishes an SSH connection to the container’s SSH server using the root user and the specified port 3000 and IP address if the container is running on a remote machine. Enter the password specified in the Dockerfile.

ssh -1 root -p 3000 ipOfHostMachine
SSH Connection

Step 6: Push Image to Docker Hub

Running SSH within a Docker container provides a way to access and manage the container remotely. By following the steps one can quickly set up an SSH server inside a Docker container, allowing secure access to the container’s environment.