Search code examples
macosvirtualboxdockerfig

Access Docker from external machine in network


Is it possible to access an docker service from an external device? I built the service via fig and exposed the port 3000. I use fig with docker-osx, so docker is running inside a virtualbox.

Now I need to access the service provided from an external device (i.e. a mobile phone or tablet).

At the moment I could only access the service with localdocker:3000 from the machine hosting the VirtualBox-Environment.


Solution

  • You'll have to tell your local machine to listen for incoming connections on that port and then forward those requests on to your docker container.

    Nginx is pretty good at this, and a simple config like this:

    /etc/nginx/sites-enabled/your-file.conf

    server {                                                                   
        listen 3000;                                                              
    
        server_name YOUR_IP_ADDRESS;                                              
    
        proxy_redirect off;                                                       
        proxy_buffering off;                                                      
        proxy_set_header Host $host;                                              
        proxy_set_header X-Real-IP $remote_addr;                                  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              
    
        location / {                                                              
            proxy_pass http://127.0.0.1:3000;                                            
        }                                                                         
    } 
    

    Would work fine if your phone / tablet hits http://YOUR_IP_ADDRESS:3000/