Running Docker as SSH Daemon

2 minute read

Please follow this tutorial to install docker.

Step-by-step guide for running docker as ssh daemon

Dockerizing a SSH daemon service is two step process:

  1. Build a docker image
  2. Run the docker image in a container

Step1 (Build)

First we need to build a docker image. For this copy the code below and paste it to your Dockerfile.

# sshd
# VERSION               0.0.2

FROM ubuntu:14.04
MAINTAINER Sven Dowideit <>

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed '[email protected]\s*required\s*[email protected] optional [email protected]' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

CMD ["/usr/sbin/sshd", "-D"]

Now enter the following command to build the image:

$ sudo service docker restart
$ sudo docker build -t eg_sshd . #please don't forget the dot in this command

Step2 (Run)

Now all we need to run this image in a container. Let’s run our image eg_sshd in a container test_sshd.

$ sudo docker run -d -P --name test_sshd eg_sshd
# -d runs container in background and print container ID
# -P Publish all exposed ports to random ports
# --name Assign a name to the container

Please note that eg_sshd here is name for your image and test_sshd is name for your container, you can name them anything you like. We all know that default port for ssh service is 22 so let’s find out what host port the container’s port 22 is mapped to:

$ sudo docker port test_sshd 22

Now we have port number we want to ssh to but still don’t know the ip. Enter the following command in your terminal to reveal ip of your docker daemon.

$ ifconfig
docker0   Link encap:Ethernet  HWaddr 00:00:00:00:00:00  
          inet addr:  Bcast:  Mask:

For me the ip address was Enter the following code to ssh into your docker container.

$ ssh [email protected] -p 49154
# The password is `screencast`.

Congrats! you have now sshed into your docker image. Finally you can clean up after your test by stopping and removing the container, and then removing the image.

$ sudo docker stop test_sshd
$ sudo docker rm test_sshd
$ sudo docker rmi eg_sshd

This tutorial was made with reference of docker ssh example. You can also visit flux7’s docker tutorial for more knowledge. Please feel free to comment for suggestions.




Leave a Comment