How to reach docker container `localhost` from Mac?

I have posted the following workaround to my original Stackoverflow post, and the master copy of my answer will be maintained there. Below is the copy/paste to the original reponse.


Here’s a working solution. The basic idea is to use SSH tunneling to do the port forwarding.

High Level Idea

  • You first need to build a docker image to support SSH access, because
  1. ubuntu image doesn’t have a sshd out of box, and also
  2. you will need to know the password of root of your running container.
  • Then you will spin up your container as what you would normally do except that you are doing that based on the new image you created.
  • You create a SSH tunneling session from your MacBook, then you run your client on MacBook as you would normally do.

For reference, the command for SSH tunneling can be found here, the process of creating a sshd docker image is explained here, and how to ssh into docker container is explained here.
(Docker forum doesn’t allow me to post more than two URLs, so please see the link in my original stackoverflow answer)

Steps

  1. Create a Docker file Dockerfile

     #Use whatever image you are using on Docker Linux , say "FROM ubuntu:16.04"
     FROM nitincypher/docker-ubuntu-python-pip
    
     RUN apt-get update && apt-get install -y openssh-server
     RUN mkdir /var/run/sshd
     RUN echo 'root:screencast' | chpasswd
     RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
     
     # SSH login fix. Otherwise user is kicked off after login
     RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
    
     ENV NOTVISIBLE "in users profile"
     RUN echo "export VISIBLE=now" >> /etc/profile
    
     EXPOSE 22
     CMD ["/usr/sbin/sshd", "-D"]
    
  2. Create a Docker Image from the Dockerfile

     [me@MacBook App]$ docker build -t my_ssh_python .
    
  3. Spin up your server container

     [me@MacBook App]$ docker run -d -P -v `pwd`:/App --name myserver my_ssh_python
    
  4. Start your server inside the container

     [me@MacBook App]$ docker exec myserver /App/server.py
    
  5. Create a SSH tunnel

     [me@MacBook App]$ ssh root@`hostname` -p `docker port myserver 22 | awk -F ":" '{print $2}'` -L 8000:localhost:8000 -N
     #Password is "screencast" as you built in Dockerfile
    

    Note that

    a. You have to use the IP address of your MacBook instead of your docker container’s IP address.

    b. You will use the port where the default container ssh port 22 is mapped to on host

    c. In tunneling -L 8000:localhost:8000, you are saying forward anything from your MacBook 8000 (the first 8000) to Docker container’s localhost at port 8000

  6. Now you can use you client locally

     [me@MacBook App]$ ./client.py 
     Client received data: Hello, World!
    

    And on server side, you can see

     Server Connection address: ('127.0.0.1', 55396)
     Server received data: Hello, World!
1 Like