无法SSH到在MAC上运行的Docker容器

时间:2019-08-30 18:48:45

标签: macos docker ssh

我无法访问SSH或HTTP-alt。 Ubuntu容器在MacOSX上运行。由于相同的原因,我认为SSH和HTTP-alt都是有问题的。我正在使用dockerfile和docker-compose进行设置。因为我是Docker的新手,所以可能会有多余的命令。我的主机禁用了防火墙。

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

初始化命令

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

Docker输出

$ 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

容器中的端口

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

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

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

服务

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

编辑:添加了服务输出

2 个答案:

答案 0 :(得分:5)

您可以使用此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"]

这将在容器的端口22上暴露ssh。那么您可以运行以下命令来了解哪个主机端口已连接到ssh的容器22端口。

docker port <name of container> 22

此示例应用程序为您的问题提供了解决方案。看看它。 https://docs.docker.com/engine/examples/running_ssh_service/

答案 1 :(得分:0)

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

相关问题