I want to make a fancy environment for testing out CI/CD methods, but don’t want a larger cloud bill next month. SO I fired up an ancient linux server old desktop. to begin with I want to do stuff with jenkins ssh slave, so I need an ssh server, this worked out of the box.
# The password is ``screencast``.
Dockerize an SSH service
Estimated reading time: 2 minutes
Build an
eg_sshdimageThe following
Dockerfilesets up an SSHd service in a container that you can use to connect to and inspect other container’s volumes, or to get quick access to a test container.FROM ubuntu:16.04 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 prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config # 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 ENV NOTVISIBLE "in users profile" RUN echo "export VISIBLE=now" >> /etc/profile EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"]Build the image using:
$ docker build -t eg_sshd .Run a
test_sshdcontainerThen run it. You can then use
docker portto find out what host port the container’s port 22 is mapped to:$ docker run -d -P --name test_sshd eg_sshd $ docker port test_sshd 22 0.0.0.0:49154And now you can ssh as
rooton the container’s IP address (you can find it withdocker inspect) or on port49154of the Docker daemon’s host IP address (ip addressorifconfigcan tell you that) orlocalhostif on the Docker daemon host:$ ssh root@192.168.1.2 -p 49154 # The password is ``screencast``. root@f38c87f2a42d:/#Environment variables
Using the
sshddaemon to spawn shells makes it complicated to pass environment variables to the user’s shell via the normal Docker mechanisms, assshdscrubs the environment before it starts the shell.If you’re setting values in the
DockerfileusingENV, you need to push them to a shell initialization file like the/etc/profileexample in theDockerfileabove.If you need to pass
docker run -e ENV=valuevalues, you need to write a short script to do the same before you startsshd -Dand then replace theCMDwith that script.

