简体   繁体   中英

Cannot SSH to Docker Container Running on MAC

I cannot access SSH or HTTP-alt. The Ubuntu container is running on MacOSX. I assume both SSH and HTTP-alt are problematic for the same reason. I am using dockerfile and docker-compose for the setup. Because I am a novice with docker, there may be redundant commands. My host machine has the firewall disabled.

dockerfile

<-- output omitted for brevity -->

# ports
EXPOSE 22 8080

docker-compose

version: '3'
services:
  base:
    image: cox-nams:1.0
    container_name: cox-nams
    hostname: neteng-docker
    stdin_open: true
    ports:
        - "10000:22" # ssh
        - "10001:8080" # jupyter

<-- output omitted for brevity -->

Initializing Commands

$ docker exec -it cox-nams /bin/bash

Docker output

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
b37789c4660c        ba397d1c07cd        "/bin/sh -c 'service…"   34 minutes ago      Up 34 minutes       0.0.0.0:10000->22/tcp, 0.0.0.0:10001->8080/tcp   cox-nams

Ports within the Container

duser@neteng-docker:~$ netstat -at | grep LISTEN
tcp        0      0 0.0.0.0:http-alt        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.11:46461        0.0.0.0:*               LISTEN
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN

SSH from within the Container

duser@neteng-docker:~$ ssh duser@localhost -p 22
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:la2X7X8gZj7t8DQC7rwHTalMBHYC9oVggfYzATuzkyM.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
duser@localhost's password:
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.14.134-boot2docker x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.
Last login: Fri Aug 30 18:38:54 2019 from 127.0.0.1
duser@neteng-docker:~$

SSH from the Host

$ ssh duser@localhost -p 10000
ssh: connect to host localhost port 10000: Connection refused

Services

root@neteng-docker:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 03:37 ?        00:00:00 /bin/sh -c service ssh restart && bash
root        18     1  0 03:37 ?        00:00:00 /usr/sbin/sshd
root        19     1  0 03:37 ?        00:00:00 bash
root        20     0  0 03:37 pts/0    00:00:00 /bin/bash
root        55    20  0 03:40 pts/0    00:00:00 ps -ef

root@neteng-docker:/# service --status-all
 [ - ]  dbus
 [ ? ]  hwclock.sh
 [ - ]  procps
 [ + ]  ssh

EDIT: Added services output

You can use this Dockerfile

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:THEPASSWORDYOUCREATED' | 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"]

This will expose ssh on port 22 of container. then you can run following command to know which host port is connected to containers 22 port for ssh.

docker port <name of container> 22

This sample application provides solution to your problem. Have a look at it. https://docs.docker.com/engine/examples/running_ssh_service/

可悲的是,这最终成为设备防火墙问题,我在服务器上使用“ nc -l 22”,在客户端(Linux计算机)上使用“ telnet IP -p 22”进行了故障排除。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM