【问题标题】:Cannot SSH to Docker Container Running on MAC无法通过 SSH 连接到在 MAC 上运行的 Docker 容器
【发布时间】:2019-09-11 11:53:55
【问题描述】:

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

码头文件

<-- output omitted for brevity -->

# ports
EXPOSE 22 8080

码头工人撰写

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

编辑:添加服务输出

【问题讨论】:

  • 容器运行时为什么不使用docker exec -it cox-nams bash
  • EXPOSE'ing 端口没问题,但容器内实际运行的是哪些服务?
  • @Stefano 因为我希望其他人可以通过网络直接访问此容器。
  • @masseyb 已将服务信息添加到帖子
  • 你能在你的主机上试试ssh duser@0.0.0.0 -p 10000吗?

标签: macos docker ssh


【解决方案1】:

你可以使用这个 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/

【讨论】:

  • 虽然是正确的,但用于构建 openssh-server 图像的 docker 示例是 meh - 如果您允许使用密码(!)进行 root 登录,那么您做错了什么恕我直言。这是我有限的能力openssh-server 构建了你需要的一切(老派)ProxyCommandnc)和使用公钥身份验证的 2 因素身份验证 - 我在生产中使用它的变体。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多