Creating a Docker Ubuntu 13.10 Image With OpenSSH

Posted by {"display_name"=>"greg", "login"=>"greg", "email"=>"greg@udon.org", "url"=>""} on December 31, 2013 · 3 mins read

I'm trying out docker, a lightweight application container. Not unlike a lightweight and portable virtual machine that can easily be moved around or recreated from a development system to production or AWS server. I'm going to create an Ubuntu 13.10 image with openssh as my first test.
I used these instructions to install docker on an existing Ubuntu 13.10 system.

I chose the stackbrew/ubuntu:13.10 image as a base for my first docker image. This image includes Ubuntu 13.10 but I'd like to add sshd for remote shell access. The downloaded docker images are stored in /var/lib/docker/graph/<id>/layer.

For the most part, I followed these instructions on the docker site except that I used the stackbrew Ubuntu 13.10 image as the base.

Changes for 13.10

After installing openssh and attempting to connect to the docker image, my password was accepted but I was immediately disconnected from Ubuntu. I had to make two changes to get past this problem.

1. Edit the /etc/pam.d/sshd and change the pam_loginuid line 'required' to 'optional'

session      optional       pam_loginuid.so

2. Create the file /etc/default/local with the single line:

LANG="en_US.UTF-8"

To run an instance of the image as a daemon, execute:

docker run -d -p 22 stephens/sshd

To run the image in the foreground and enter the shell, execute:

docker run -i -t -p 22 stephens/sshd /bin/bash

To connect to the image, first find the port that was assigned to the ssh port and then connect with the ssh client:

docker ps
ssh root@localhost -p <port>

Download My Image

I've posted the stephens/sshd image to the docker site and  you can download it with the command:

docker pull stephens/sshd

The root password is password.

Dockerfile

Here's the Dockerfile to create this image:

FROM stackbrew/ubuntu:13.10
MAINTAINER Greg Stephens < greg [at] udon {dot} org>
ENV MYPASSWORD password
# make sure the package repository is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu saucy main universe" > /etc/apt/sources.list
RUN apt-get update && apt-get -y upgrade
# set root password
RUN echo "root:$MYPASSWORD" | chpasswd
# Install Supervisor to start multiple processes
RUN apt-get -y install python-pip python-dev supervisor
RUN mkdir -p /var/log/supervisor
RUN /bin/echo -e "[supervisord]\nnodaemon=true\n" >/etc/supervisord.conf
# Install OpenSSH & set values to work with Ubuntu 13.10
RUN apt-get -y install openssh-server
RUN mkdir /var/run/sshd
RUN /usr/sbin/sshd
RUN sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
# Expose 22 to random ports
EXPOSE 22
# Setup supervisor to start openssh
RUN /bin/echo -e "[program:sshd]\ndirectory=/usr/local\ncommand=/usr/sbin/sshd -D\nautostart=true\nautorestart=true\nredirect_stderr=true\n" >>/etc/supervisord.conf
ENTRYPOINT ["/usr/bin/supervisord","-n"]