This blog post explains how to create a container image having an SSH server and how to SSH into it once its created.
Preparing the container
We will be using the interactive way to creating a container image. Before we start we need to select the base image on which OpenSSH Server will be installed. For this post, I am using ubuntu xenial image. All the above commands must be typed in manually.
$ docker pull ubuntu:xenial $ docker run -it ubuntu:xenial $ apt-get update $ apt-get -y install openssh-server $ useradd testuser $ passwd testuser $ service ssh restart $ mkdir /home/testuser
Persisting the changes in an image
Open another terminal window and type in the following commands. The first thing to do is to find the container id:
$ docker ps
Now create the image using the command:
sudo docker commit [CONTAINER_ID] [new_image_name]
$ docker commit 5319c0b73e42 siddharthbarman/ubuntu-xenial-with-ssh:v1
Run a container off the newly created image
We'll now run a new container using the freshly created image. After this we'll SSH into it.
$ docker run -it siddharthbarman/ubuntu-xenial-with-ssh:v1 $ service ssh start #start the ssh server $ hostname -I # will print the IP address of the container
If you have a container which is running non-interactively, you can 'bash' into it using the technique below and then start the SSH server and find out the container's IP:
$ docker exec -it <container-id> bash
Once we have the IP address, we can easily ssh into it:
$ ssh testuser@<container-ip-address>