3

I am having a weird problem.

I am not able to ssh to docker container having ip address 172.17.0.61.

I am getting following error:

$ ssh 172.17.0.61
ssh: connect to host 172.17.0.61 port 22: Connection refused

My Dockerfile does contain openssh-server installation step:

RUN apt-get -y install curl runit openssh-server

And also step to start ssh:

RUN service ssh start

What could be the issue?

When I enter into container using nsenter and start ssh service then I am able to ssh. But while creating container ssh-server doesn't seems to start.

What should I do?

Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60

3 Answers3

11

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.1
MAINTAINER Thomas Steinbach
EXPOSE 22
RUN apt-get install -y openssh-server
CMD 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.

Thomas Steinbach
  • 1,019
  • 1
  • 9
  • 19
3

In my opinion there is a better approach:

Dockerfile

FROM ubuntu:14.04.1
EXPOSE 22
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
RUN apt-get install -y openssh-server

ENTRYPOINT ["sh", "/docker-entrypoint.sh"]

# THIS PART WILL BE REPLACED IF YOU PASS SOME OTHER COMMAND TO docker RUN
CMD while true; do echo "default arg" && sleep 1; done

docker-entrypoint.sh

#!/bin/bash
service ssh restart
exec "$@"

Build command docker 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 second whether docker run sshtest sh -c 'while true; do echo "passed arg" && sleep 3; done' will print passed arg every 3 seconds

Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
-1

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 :)

ThatsMe
  • 1
  • 1
  • See [answer]. Answers should be more than just a link—if what's at that link is changed or deleted, there's no other information in your post. Show _how_ you went about adapting that answer to your situation, and how exactly your situation relates to the question here – camille Aug 08 '22 at 15:51