Unable to connect to local ssh host within installed Docker container

I am creating a Docker image for an application that requires ssh in localhost (i.e. ssh user @localhost)

I work on an Ubuntu desktop machine and start with the base ubuntu container: 16.04. The following is the contents of my Docker file:

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y \
        openjdk-8-jdk \
        ssh && \
        groupadd -r custom_group && useradd -r -g custom_group -m user1

USER user1

RUN ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N "" && \
        cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Then I create this container with the command:

docker build -t test-container .

And run it using:

docker run -it test-container

The container opens with the following prompt, and the keys are generated correctly to enable ssh in localhost:

user1@0531c0f71e0a:/$ 
user1@0531c0f71e0a:/$ cd ~/.ssh/
user1@0531c0f71e0a:~/.ssh$ ls
authorized_keys  id_rsa  id_rsa.pub

Then ssh to localhost and is welcomed by the error:

user1@0531c0f71e0a:~$ ssh user1@localhost
ssh: connect to host localhost port 22: Cannot assign requested address

Is there something I'm doing wrong or any additional network settings that need to be configured? I just want ssh in localhost in a running container.

+15
3

ssh- :

  • RUN sudo apt-get install -y openssh-server

SSH:

  • RUN sudo/etc/init.d/ssh start

, , Dockerfile ( ...)

 USER root
 CMD [ "sh", "/etc/init.d/ssh", "start"]

,

# init a container from an the image
run -d --name my-ssh-container-name-01 \
    -v /opt/local/dir:/opt/container/dir my-image-01

+12

@user2915097, OP, - ssh , IPv6. IPv4 -4 .

$ docker run -it ubuntu ssh -4 user@hostname
+3

Docker Compose .yml:

network_mode: "host"

, Docker:

--net=host

0

Source: https://habr.com/ru/post/1678224/


All Articles