What value should I use for Nginx proxy_pass running in Docker?

June 18, 2018 123 views
Docker Nginx Ubuntu 16.04

I have a fresh Droplet on Ubuntu 16.04 running Docker and Docker Compose with two containers: nginx and my homepage.

My homepage container is running on port 3000. I can curl localhost:3000 and see the app.

I have this setup on my home server and on AWS and the proxy_pass value is the local/private IP address of my server. For example on my home server it's http://192.168.1.100:3000/, on AWS it's the Private IP of the EC2 instance.

I have tried several values on the Droplet for proxy_pass such as http://127.0.0.1:3000, localhost, but none of those worked. I lastly tried enabling private networking and tried using the listed private IP address. Still nothing...

What should I be using for the value here?

1 Answer
joshfierstein June 20, 2018
Accepted Answer

My apologies if this is against the rules to post an answer to your own question, but many times in the past I'd wished others had done the same.

For those that weren't aware, whenever you use docker-compose up, it automatically creates a default docker network if you don't specify any in the docker-compose.yaml. Docker containers in the same network automatically get a DNS entry created for the container name. That means you can access the other containers in the network using something like http://mycontainername:8080.

For the Nginx container running the reverse proxy, the proxy_pass value ended up just being http://myapp:3000.

Definitely seems like a no-brainer. I know I had seen posts online regarding using a name url like that but was ignoring all them because I didn't realize the docker network was doing that behind the scenes and din't put it together that using the container name would work in this way.

Hopefully this helps someone!

Have another answer? Share your knowledge.