簡體   English   中英

無法SSH到在MAC上運行的Docker容器

[英]Cannot SSH to Docker Container Running on MAC

我無法訪問SSH或HTTP-alt。 Ubuntu容器在MacOSX上運行。 由於相同的原因,我認為SSH和HTTP-alt都是有問題的。 我正在使用dockerfile和docker-compose進行設置。 因為我是Docker的新手,所以可能會有多余的命令。 我的主機禁用了防火牆。

dockerfile

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

編輯:添加服務輸出

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

可悲的是,這最終成為設備防火牆問題,我在服務器上使用“ nc -l 22”,在客戶端(Linux計算機)上使用“ telnet IP -p 22”進行了故障排除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM