Docker: proxy_pass to another container - nginx: host not found in upstream
I figured out how to fix the problem. Got some help to fix the docker-compose.yml
, so it looks like this:
docker-compose-yml:
version: "3"
services:
web:
image: user/repo:web
deploy:
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "8000:80"
networks:
main:
aliases:
- web
nginx:
image: user/repo:nginx
ports:
- 80:80
links:
- web:web
depends_on:
- web
networks:
main:
aliases:
- nginx
networks:
main:
After this the nginx container actually ran, but it was still not capable of connecting to the web-container. Found out I was able to use both curl web
and curl web -p 8000
to get the page from web, from inside the nginx container. Then I changed the upstream in my nginx.conf
from this
upstream docker-web {
server web:8000;
}
to this:
upstream docker-web {
server web;
}
Hello your problem is with the port when you linked web:web you need to specify the port in which the container runs the service for exemple
ports:
- "8000:80"
means inside the container i can access on port 80 but in the host machine i can acess via 8000. here's the doc about linking two services in docker-compose
so if you want to access web in proxy nginx don't use 8000 but use :
upstream docker-web {
server web:80;
}
instead, this should work :)
or (those are both the same)
upstream docker-web {
server web;
}
It's simple. Just change the old configuration:
upstream docker-web {
server web:8000;
}
To this new one:
upstream docker-web {
server web:80;
}
The reason is, between containers can't communicate with published port. It just can communicate with the target port. 8000:80 (8000 is published port and 80 is target port).
Let say you have 8000:443, you must create the configuration with "server web:443" inside Nginx listen to 443 port number. May this help.