I have a container which runs its web app on localhost:4000 i want to access it through nginx which is in another container. I have created a docker network and ran the following to bring them under same network mynetwork
docker run --name myapp -p 8080:4000 -d --network mynewnetwork ubuntunet:1.9 --- myapp
docker run --name nginx -p 80:80 -d --network mynewnetwork nginx --- nginx
with the following nginx.conf
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream docker-myapp {
server myapp:4000;
}
server {
listen 80;
location / {
proxy_pass http://docker-myapp;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
For some reason this is not working showing 502 bad gateway when i access docker host ip, when we create a docker network it opens all ports between the containers that resides in the same network correct? if yes why my app is not running.
Should i change hosts file in myapp container to have docker ip with localhost instead of 127.0.0.1? or i am missing something in this. Any help on this would be appreciated.