2014-05-21 21:05:19 +00:00
|
|
|
page_title: Dockerizing an SSH service
|
|
|
|
page_description: Installing and running an SSHd service on Docker
|
2014-04-16 00:53:12 +00:00
|
|
|
page_keywords: docker, example, package installation, networking
|
|
|
|
|
2014-05-21 21:05:19 +00:00
|
|
|
# Dockerizing an SSH Daemon Service
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-09-12 03:30:20 +00:00
|
|
|
## Build an `eg_sshd` image
|
|
|
|
|
2014-05-21 21:05:19 +00:00
|
|
|
The following `Dockerfile` sets up an SSHd service in a container that you
|
2014-04-23 20:48:28 +00:00
|
|
|
can use to connect to and inspect other container's volumes, or to get
|
2014-04-16 00:53:12 +00:00
|
|
|
quick access to a test container.
|
|
|
|
|
|
|
|
# sshd
|
|
|
|
#
|
2014-09-12 03:30:20 +00:00
|
|
|
# VERSION 0.0.2
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-09-12 03:30:20 +00:00
|
|
|
FROM ubuntu:14.04
|
|
|
|
MAINTAINER Sven Dowideit <SvenDowideit@docker.com>
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-07-07 18:06:34 +00:00
|
|
|
RUN apt-get update && apt-get install -y openssh-server
|
2014-05-21 21:05:19 +00:00
|
|
|
RUN mkdir /var/run/sshd
|
2014-09-03 21:44:57 +00:00
|
|
|
RUN echo 'root:screencast' | chpasswd
|
|
|
|
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-09-22 03:32:34 +00: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-12 03:30:20 +00:00
|
|
|
ENV NOTVISIBLE "in users profile"
|
|
|
|
RUN echo "export VISIBLE=now" >> /etc/profile
|
|
|
|
|
2014-04-16 00:53:12 +00:00
|
|
|
EXPOSE 22
|
2014-09-03 21:44:57 +00:00
|
|
|
CMD ["/usr/sbin/sshd", "-D"]
|
2014-04-16 00:53:12 +00:00
|
|
|
|
|
|
|
Build the image using:
|
|
|
|
|
2015-03-26 18:12:37 +00:00
|
|
|
$ docker build -t eg_sshd .
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-09-12 03:30:20 +00:00
|
|
|
## Run a `test_sshd` container
|
|
|
|
|
2014-05-21 21:05:19 +00: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-16 00:53:12 +00:00
|
|
|
|
2015-03-26 18:12:37 +00:00
|
|
|
$ docker run -d -P --name test_sshd eg_sshd
|
|
|
|
$ docker port test_sshd 22
|
2014-04-16 00:53:12 +00:00
|
|
|
0.0.0.0:49154
|
|
|
|
|
2014-09-03 21:44:57 +00: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 20:30:36 +00:00
|
|
|
(`ip address` or `ifconfig` can tell you that) or `localhost` if on the
|
|
|
|
Docker daemon host:
|
2014-04-16 00:53:12 +00:00
|
|
|
|
|
|
|
$ ssh root@192.168.1.2 -p 49154
|
|
|
|
# The password is ``screencast``.
|
|
|
|
$$
|
|
|
|
|
2014-09-12 03:30:20 +00:00
|
|
|
## Environment variables
|
|
|
|
|
|
|
|
Using the `sshd` daemon to spawn shells makes it complicated to pass environment
|
2014-10-11 20:30:36 +00:00
|
|
|
variables to the user's shell via the normal Docker mechanisms, as `sshd` scrubs
|
2014-09-12 03:30:20 +00:00
|
|
|
the environment before it starts the shell.
|
|
|
|
|
2014-10-11 20:30:36 +00: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-12 03:30:20 +00:00
|
|
|
above.
|
|
|
|
|
|
|
|
If you need to pass`docker run -e ENV=value` values, you will need to write a
|
2014-10-11 20:30:36 +00:00
|
|
|
short script to do the same before you start `sshd -D` and then replace the
|
2014-09-12 03:30:20 +00:00
|
|
|
`CMD` with that script.
|
|
|
|
|
|
|
|
## Clean up
|
|
|
|
|
2014-04-16 00:53:12 +00:00
|
|
|
Finally, clean up after your test by stopping and removing the
|
|
|
|
container, and then removing the image.
|
|
|
|
|
2015-03-26 18:12:37 +00:00
|
|
|
$ docker stop test_sshd
|
|
|
|
$ docker rm test_sshd
|
|
|
|
$ docker rmi eg_sshd
|
2014-05-21 21:05:19 +00:00
|
|
|
|