Accessing host machine as localhost from a Docker container that's also inside a boot2docker vm

Paul Lam

Suppose I have a server running on port 8000 on OSX. How can my Docker container access it via localhost:8000? I can't change the hostname too as the app in the container is not in my control.

I've read this previous discussion on using --net="host" for a container to access the host machine's network. However, I'm on OSX and Docker runs inside a VM so localhost from the Docker container with --net="host" goes to the VM and not my real machine.

Then I tried port forwarding workaround like so: VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000"; to no avail.

Any suggestion would be greatly appreciated.

lajarre

Thanks to palimpestor's answer I figured it out:

Instead of --net="host", use --add-host="localhost:10.0.2.2"

Indeed:

  • 10.0.2.2 is the default gateway defined for the guest network interface in NAT mode (reference).
    Read: it's your host, seen from boot2docker.
  • --add-host... is adding localhost 10.0.2.2 in /etc/hosts (reference)

Note: you need to have set up a NAT adapter in your boot2docker VM VirtualBox settings (I did it through the GUI, don't know the CLI).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to connect to 'real' localhost from inside my VM boot2docker container?

From Dev

Accessing apache2 residing inside Docker container from Host machine web browser

From Dev

TCP bridge between 2 interfaces for specific port (accessing host's localhost from inside a VM)

From Dev

How can I redirect traffic to an Host machine port from a VM inside in a CentOS Docker container?

From Dev

Accessing a Docker MySQL server from the host machine, without needing the container's IP address

From Java

From inside of a Docker container, how do I connect to the localhost of the machine?

From Dev

Domain resolves on host machine but not inside docker container

From Dev

Docker access container logs from the host machine

From Dev

Cannot ping docker container from the host machine

From Dev

Accessing web application running inside a docker container in host browser?

From Dev

Accessing VirtualBox localhost from host machine

From Dev

Directory created inside docker container not reflecting on host machine

From Dev

How to connect with JMX from host to Docker container in Docker machine?

From Dev

What's the best way to share files from Windows to Boot2docker VM?

From Dev

Error accessing host port from docker container on ubuntu

From Dev

Linux+Docker - How to run host's apps from inside Docker container?

From Dev

Accessing Docker container from another machine in an internal network

From Java

How to get the IP address of the docker host from inside a docker container

From Dev

Host unreachable inside Docker container

From Dev

Host unreachable inside Docker container

From Dev

Resolve docker-compose container names from host machine

From Dev

Run script from host machine on Docker container with exec

From Java

Connect from laravel app in docker container to Postgresql in host machine?

From Dev

How to access mysql running in docker container from host machine?

From Dev

Load data from host machine to docker hive container

From Dev

Load data from host machine to docker hive container

From Dev

docker(Mac/Windows): how to access host machine service from the container?

From Dev

Mounting a directory from the host in boot2docker for Windows

From Dev

403 Forbidden when accessing host OS's localhost on guest VM

Related Related

  1. 1

    How to connect to 'real' localhost from inside my VM boot2docker container?

  2. 2

    Accessing apache2 residing inside Docker container from Host machine web browser

  3. 3

    TCP bridge between 2 interfaces for specific port (accessing host's localhost from inside a VM)

  4. 4

    How can I redirect traffic to an Host machine port from a VM inside in a CentOS Docker container?

  5. 5

    Accessing a Docker MySQL server from the host machine, without needing the container's IP address

  6. 6

    From inside of a Docker container, how do I connect to the localhost of the machine?

  7. 7

    Domain resolves on host machine but not inside docker container

  8. 8

    Docker access container logs from the host machine

  9. 9

    Cannot ping docker container from the host machine

  10. 10

    Accessing web application running inside a docker container in host browser?

  11. 11

    Accessing VirtualBox localhost from host machine

  12. 12

    Directory created inside docker container not reflecting on host machine

  13. 13

    How to connect with JMX from host to Docker container in Docker machine?

  14. 14

    What's the best way to share files from Windows to Boot2docker VM?

  15. 15

    Error accessing host port from docker container on ubuntu

  16. 16

    Linux+Docker - How to run host's apps from inside Docker container?

  17. 17

    Accessing Docker container from another machine in an internal network

  18. 18

    How to get the IP address of the docker host from inside a docker container

  19. 19

    Host unreachable inside Docker container

  20. 20

    Host unreachable inside Docker container

  21. 21

    Resolve docker-compose container names from host machine

  22. 22

    Run script from host machine on Docker container with exec

  23. 23

    Connect from laravel app in docker container to Postgresql in host machine?

  24. 24

    How to access mysql running in docker container from host machine?

  25. 25

    Load data from host machine to docker hive container

  26. 26

    Load data from host machine to docker hive container

  27. 27

    docker(Mac/Windows): how to access host machine service from the container?

  28. 28

    Mounting a directory from the host in boot2docker for Windows

  29. 29

    403 Forbidden when accessing host OS's localhost on guest VM

HotTag

Archive