6

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.

Pantharian
  • 61
  • 1
  • 2
  • 5
  • The image you're using probably isn't configured for SSH. While some questions about Docker are loosely programming-related, this one is not. This is a network / sys admin question, and is no different than "How do I enable SSH in Linux?" – Jonathon Reinhart Jun 22 '16 at 10:51
  • I'm voting to close this question as off-topic because it is not about programming. – Jonathon Reinhart Jun 22 '16 at 10:51
  • see https://stackoverflow.com/questions/39739560/how-to-access-the-vm-created-by-dockers-hyperkit for an answer specifically for 'Docker for Mac" – thoutbeckers Aug 26 '19 at 10:58

2 Answers2

10

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.

G07cha
  • 4,009
  • 2
  • 22
  • 39
s00150515
  • 101
  • 2
  • 2
    It is possible, but you're right, in most cases 22 port is already used by SSH daemon on the host machine. Error "EADDRINUSE" means that this address already in use in case if you meet this error once more. – G07cha Jun 22 '16 at 14:30
  • To be clearer: if you required this to use port 22 on the host, you would have to shut down the host's own instance of SSHd. – Scott Prive May 16 '19 at 14:31
8

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.

G07cha
  • 4,009
  • 2
  • 22
  • 39