简体   繁体   中英

Docker - proxy container port on host

I have an Ubuntu VM running on my windows machine (I'm using Vagrant with VirtualBox for that). I am running two docker containers in the VM, one a DB the other a web server. I want to proxy the web container through the host so that I can browse the web container from the windows machine.

Does docker help with this or do I need something like HAProxy on the VM?

There are differents ways to achieve this.

Lets firt assume you have the following container running on your Docker host:

docker run -d -p 80:80 tutum/hello-world

The -p option tells Docker to open port 80 on the Docker host and forward traffic to port 80 of the Docker container.

By assigning static IP to your Vagrant box

In your Vagrantfile , you can assign a fixed IP to your Vagrant box by adding:

config.vm.network "private_network", ip: "176.16.0.3"

Then from Windows, open http://176.16.0.3/

By forwarding a port in your Vagrant box

If you don't want to assign a fixed IP address to your Vagrant box, you can instead forward port 80 from the Ubuntu box to port 80 of the Vagrant host (the Windows machine).

In your Vagrantfile , put

config.vm.network "forwarded_port", guest: 80, host: 80

Now, from the Windows machine, you can reach the webserver at http://localhost/ .

other considerations

Note that in your Docker container, your webserver MUST accept connections from the outside. In other words you need to bind to the special 0.0.0.0 network interface instead of just localhost or 127.0.0.1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM