Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I SSH to a Docker in Mac container [duplicate]

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.

like image 927
Pantharian Avatar asked Jun 22 '16 10:06

Pantharian


1 Answers

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.

like image 196
s00150515 Avatar answered Sep 20 '22 08:09

s00150515