Do a real SSH into a docker container

Viewed 5831

I am trying to use this guide:

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

to do a plain SSH to a running container, first trying to get ip addr for running container:

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

Based on the above I am trying to:

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

and:

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

and:

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

and:

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

using the password from the guide: screencast but it does not work.

Also verified that it is running:

$ 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

If I SSH to the container using:

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

instead it looks like there is no .ssh folder for the root user:

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

I also tried to run:

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

Not really sure what to make of that though.

Any suggestions?

1 Answers

You need to use your host IP address not docker ip, because -p 32768 is mapped port it's not running inside container. So use ssh root@hostip -p mapped port

Related