2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
|
|
|
title = "Dockerizing an SSH service"
|
|
|
|
description = "Installing and running an SSHd service on Docker"
|
|
|
|
keywords = ["docker, example, package installation, networking"]
|
|
|
|
[menu.main]
|
|
|
|
parent = "smn_apps_servs"
|
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
# Dockerizing an SSH daemon service
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-09-11 23:30:20 -04:00
|
|
|
## Build an `eg_sshd` image
|
|
|
|
|
2014-05-21 17:05:19 -04:00
|
|
|
The following `Dockerfile` sets up an SSHd service in a container that you
|
2014-04-23 16:48:28 -04:00
|
|
|
can use to connect to and inspect other container's volumes, or to get
|
2014-04-15 20:53:12 -04:00
|
|
|
quick access to a test container.
|
|
|
|
|
|
|
|
# sshd
|
|
|
|
#
|
2014-09-11 23:30:20 -04:00
|
|
|
# VERSION 0.0.2
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-09-11 23:30:20 -04:00
|
|
|
FROM ubuntu:14.04
|
|
|
|
MAINTAINER Sven Dowideit <SvenDowideit@docker.com>
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-07-07 14:06:34 -04:00
|
|
|
RUN apt-get update && apt-get install -y openssh-server
|
2014-05-21 17:05:19 -04:00
|
|
|
RUN mkdir /var/run/sshd
|
2014-09-03 17:44:57 -04:00
|
|
|
RUN echo 'root:screencast' | chpasswd
|
|
|
|
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-09-21 23:32:34 -04:00
|
|
|
# SSH login fix. Otherwise user is kicked off after login
|
|
|
|
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
|
|
|
|
|
2014-09-11 23:30:20 -04:00
|
|
|
ENV NOTVISIBLE "in users profile"
|
|
|
|
RUN echo "export VISIBLE=now" >> /etc/profile
|
|
|
|
|
2014-04-15 20:53:12 -04:00
|
|
|
EXPOSE 22
|
2014-09-03 17:44:57 -04:00
|
|
|
CMD ["/usr/sbin/sshd", "-D"]
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Build the image using:
|
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker build -t eg_sshd .
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-09-11 23:30:20 -04:00
|
|
|
## Run a `test_sshd` container
|
|
|
|
|
2014-05-21 17:05:19 -04:00
|
|
|
Then run it. You can then use `docker port` to find out what host port
|
|
|
|
the container's port 22 is mapped to:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker run -d -P --name test_sshd eg_sshd
|
|
|
|
$ docker port test_sshd 22
|
2014-04-15 20:53:12 -04:00
|
|
|
0.0.0.0:49154
|
|
|
|
|
2014-09-03 17:44:57 -04:00
|
|
|
And now you can ssh as `root` on the container's IP address (you can find it
|
|
|
|
with `docker inspect`) or on port `49154` of the Docker daemon's host IP address
|
2014-10-11 16:30:36 -04:00
|
|
|
(`ip address` or `ifconfig` can tell you that) or `localhost` if on the
|
|
|
|
Docker daemon host:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
$ ssh root@192.168.1.2 -p 49154
|
|
|
|
# The password is ``screencast``.
|
|
|
|
$$
|
|
|
|
|
2014-09-11 23:30:20 -04:00
|
|
|
## Environment variables
|
|
|
|
|
|
|
|
Using the `sshd` daemon to spawn shells makes it complicated to pass environment
|
2014-10-11 16:30:36 -04:00
|
|
|
variables to the user's shell via the normal Docker mechanisms, as `sshd` scrubs
|
2014-09-11 23:30:20 -04:00
|
|
|
the environment before it starts the shell.
|
|
|
|
|
2014-10-11 16:30:36 -04:00
|
|
|
If you're setting values in the `Dockerfile` using `ENV`, you'll need to push them
|
|
|
|
to a shell initialization file like the `/etc/profile` example in the `Dockerfile`
|
2014-09-11 23:30:20 -04:00
|
|
|
above.
|
|
|
|
|
|
|
|
If you need to pass`docker run -e ENV=value` values, you will need to write a
|
2014-10-11 16:30:36 -04:00
|
|
|
short script to do the same before you start `sshd -D` and then replace the
|
2014-09-11 23:30:20 -04:00
|
|
|
`CMD` with that script.
|
|
|
|
|
|
|
|
## Clean up
|
|
|
|
|
2014-04-15 20:53:12 -04:00
|
|
|
Finally, clean up after your test by stopping and removing the
|
|
|
|
container, and then removing the image.
|
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker stop test_sshd
|
|
|
|
$ docker rm test_sshd
|
|
|
|
$ docker rmi eg_sshd
|
2014-05-21 17:05:19 -04:00
|
|
|
|