openssh-server doesn't start in Docker container
When building a Dockerfile you would create an image. But you can't create an image with an already running ssh daemon or any running service else. First if you create a running container out of the image you can start services inside. E.g. by appending the start instruction to the docker run command:
sudo docker run -d mysshserver service ssh start
You can define a default command for your docker image with CMD. Here is an example Dockerfile:
FROM ubuntu:14.04.1MAINTAINER Thomas SteinbachEXPOSE 22RUN apt-get install -y openssh-serverCMD service ssh start && while true; do sleep 3000; done
You can build an run this image with the following two commands:
sudo docker build -t sshtest .sudo docker run -d -P --name ssht sshtest
Now you can connect to this container via ssh. Note that in the example Dockerfile no user and no login was created. This image is just for example and you can start an ssh connection to it, but not login.
In my opinion there is a better approach:
Dockerfile
FROM ubuntu:14.04.1EXPOSE 22COPY docker-entrypoint.sh /docker-entrypoint.shRUN chmod +x /docker-entrypoint.shRUN apt-get install -y openssh-serverENTRYPOINT ["sh", "/docker-entrypoint.sh"]# THIS PART WILL BE REPLACED IF YOU PASS SOME OTHER COMMAND TO docker RUNCMD while true; do echo "default arg" && sleep 1; done
docker-entrypoint.sh
#!/bin/bashservice ssh restartexec "$@"
Build commanddocker build -t sshtest .
The benefit of this approach is that your ssh daemon will always start when you use docker run
, but you can also specify optional arguments e.g.:
docker run sshtest
will print default arg
every 1 secondwhether docker run sshtest sh -c 'while true; do echo "passed arg" && sleep 3; done'
will print passed arg
every 3 seconds
I had the same problem.Luckily I could solve it by checking kenorb answer and adapting it to my Dockerfile:https://stackoverflow.com/a/61738823/4058295
It's worth a try :)