做一个真正的SSH到docker容器

时间:2017-11-27 19:00:34

标签: docker ssh

我正在尝试使用本指南:

https://docs.docker.com/engine/examples/running_ssh_service/#run-a-test_sshd-container

对正在运行的容器执行普通SSH,首先尝试使用ip addr运行容器:

$ sudo docker inspect 4b928aca09ae

        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "74ce28dd93a6fa6158ebc3f4ec73f1dba74ddbfc183f4ebf9cca77ced2571174",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "22/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "32768"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/74ce28dd93a6",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "5927380d426ce9d5684dc17982c7f01af38d850218aa476d6ed8f457c8a39165",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.8",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:08",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9c02e185366bb205bded7cab97766cb0e91315a4317affedc98f299831d5ceae",
                    "EndpointID": "5927380d426ce9d5684dc17982c7f01af38d850218aa476d6ed8f457c8a39165",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.8",
                    "IPPrefixLen": 16,

基于以上所述我试图:

$ ssh root@172.17.0.1 -p 32768
root@172.17.0.1's password: 
Permission denied, please try again.
root@172.17.0.1's password: 

$ ssh root@172.17.0.8 -p 32768
ssh: connect to host 172.17.0.8 port 32768: Connection refused

$ ssh root@172.17.0.8
root@172.17.0.8's password: 
Permission denied, please try again.
root@172.17.0.8's password:

$ ssh root@localhost -p 32768
root@localhost's password: 
Permission denied, please try again.
root@localhost's password: 

使用指南中的密码:screencast但不起作用。

还确认它正在运行:

$ sudo docker ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                                            NAMES
4b928aca09ae        eg_sshd                    "/usr/sbin/sshd -D"      3 days ago          Up 33 minutes       0.0.0.0:32768->22/tcp                            test_sshd

如果我使用以下方式SSH到容器:

$ sudo docker exec -i -t 4b928aca09ae /bin/bash

而是看起来根用户没有.ssh文件夹:

root@4b928aca09ae:~# pwd
/root
root@4b928aca09ae:~# ls -la
total 20
drwx------  2 root root 4096 Nov 30 22:01 .
drwxr-xr-x 49 root root 4096 Nov 30 22:01 ..
-rw-------  1 root root   46 Nov 30 22:01 .bash_history
-rw-r--r--  1 root root 3106 Oct 22  2015 .bashrc
-rw-r--r--  1 root root  148 Aug 17  2015 .profile

我也尝试过:

root@4b928aca09ae:~# ps -A | grep sshd
    1 ?        00:00:00 sshd

虽然不太确定如何制作。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

你需要使用主机IP地址而不是docker ip,因为-p 32768是映射端口,它没有在容器内运行。所以使用ssh root @ hostip -p映射端口