问题
I am running Docker for Mac (Version 1.12.0-rc2-beta16 (Build: 9493)).
I have pulled an image from my local repository, and used 'docker run -d' to create a container. Using 'docker ps' I obtained the 'CONTAINER ID', and then used 'docker inspect <CONTAINER_ID>| grep IPA' to obtain the IP address for the running container.
I now want to connect to the container using SSH with 'ssh root@<IP address>' but the command gives the following error: 'Operation timed out'.
Further investigation shows that I can not ping the <IP address> -> 'Request timeout for icmp_seq 0'
How can I connect to the container using SSH? What is the correct command?
UPDATE: This IS NOT a duplicate (as stated above). The entry that begins "The scenario you described" is the correct solution.
回答1:
The scenario you have described is the approach that would be used on 'normal' Docker.
As Docker on Mac has been created from scratch specifically for the Mac, it has been tweaked to make it easier to use. Therefore, the IP address of the container cannot be used in this way on the Mac.
The documentation Getting Started with Docker for Mac states that:
Previous beta releases used docker as the hostname to build the URL. From this release forward, ports are exposed on the private IP addresses of the VM and forwarded to localhost with no other host name set. See also, Release Notes for Beta 9.
Therefore, the correct way to SSH into a container is to spin it up on Docker for Mac using a port mapping to the SSH port (22). e.g.
docker run -d -p 2022:22 <Image Name>
And the SSH connection is instigated using this command (N.B. it uses 'localhost' on the port specified instead of having to determine and use the container's IP Address):
ssh -p 2022 root@localhost
N.B. It is NOT possible to simply map port 22 to itself i.e. '-p 22:22' as this caused the following error (at least is did for me!):
docker: Error response from daemon: driver failed programming external connectivity on endpoint pensive_wilson (2e832b82fc67d3e48864975c6eb02f6c099e34eee64b29634cfde286c41e00a7): Error starting userland proxy: Failed to bind: EADDRINUSE.
回答2:
To use bash prompt you could use docker exec -ti %container-name-or-id% /bin/bash
. If you want to use ssh and ensure that an ssh daemon is up and running you should expose corresponding ports from the container with -p
parameter like this: docker run -d -p 22:22 my_image
.
来源:https://stackoverflow.com/questions/37965790/how-do-i-ssh-to-a-docker-in-mac-container