How to access a server on localhost with a dginx nginx container?

I am trying to use a docked version of nginx as a proxy for my node application (ExpressJS). Without any configuration for nginx and publishing port 80 for the container, I can see the default nginx landing page. Therefore, I know that a lot works.

Now I can connect the site-support directory containing the configuration for proxy_pass localhost:3000 . I have a node application running locally (not in any Docker container) and I can access it through port 3000 (i.e. localhost:3000 ). However, I would suggest that with the launch of the nginx container mapped to port 80 and proxying my localhost: 3000, I could see my very simple (hello world) application. Instead, I get 502.

Do I need to transfer something to the docker? Perhaps this is a nginx configuration error? Here is my nginx configuration:

 server { listen 0.0.0.0:80; server_name localhost; location / { proxy_pass http://localhost:3000; } } 

I tried using this question , but it didn't seem to help. That is, if I am not doing something completely wrong.

+12
docker nginx
source share
4 answers

You can get your current IP address as shown here :

 ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}' 

Then you can use the --add-host flag with docker run :

 docker run --add-host localnode:$(ifconfig en0 | grep inet | grep -v inet6 | awk '{print \$2}') ... 

In proxypass use localnode instead of localhost .

+9
source share

If you use docker-for-mac 18.03 or later, it automatically creates a special DNS record host.docker.internal that dynamically binds to the inet ip host. You can then use the dns name for proxy services running on the host machine from inside the container as a replacement for localhost .

those. nginx configuration file:

 server { listen 0.0.0.0:80; server_name localhost; location / { proxy_pass http://host.docker.internal:3000; } } 
+3
source share

Yes. Docker must know about your car. You can set an alias using the --add-host switch. In the * nix field to create an alias for the name "localbox" it will be:

 docker run my_repo/my_image --add-host=localbox:<host_name>` 

In boot2docker, this will be:

 docker run my_repo/my_image --add-host=localbox:192.168.59.3` 

where you should replace "192.168.59.3" with what boot2docker ip returns.

Then you should always access your host machine through the local alias block, so just change the nginx configuration to:

 location / { proxy_pass http://localbox:3000; } 
+2
source share

And finally, if you use Nginx as a reverse proxy for multiple services, you can connect all of this with docker-compose. Make sure that the "80:80" ports are open only in the Nginx service. Other services you can set only the service port without reference to the core network, for example like this:

 web: ..... expose: - 8080 nginx: ..... port: - "80:80" 

and then use the Nginx proxy_pass configuration http: // service-name: port You don’t need part of the upstream application at all

-one
source share

All Articles