Reputation: 570
I have 2 Windows Server Docker containers deployed with Docker Compose. One container runs a reverse proxy server and the other runs a web server. The web server serves a simple index.html page. Both are running Nginx.
When run, everything works as expected. I can access content via the proxy. But if I restart the web server I get a bad gateway error. I then need to restart the proxy to get it working again.
How do I get this working without the need to restart the proxy?
Docker-compose:
version: '3'
services:
web:
build:
context: ./web
dockerfile: ./dockerfile
proxy:
build:
context: ./proxy
dockerfile: ./dockerfile
ports:
- "8089:80"
Proxy Dockerfile:
FROM sixeyed/nginx:windowsservercore
COPY nginx.conf C:/nginx/conf
CMD C:\nginx\nginx.exe
Proxy nginx.config:
events {
}
http {
server {
listen 80;
server_name localhost;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://web:80/;
}
}
}
Web Dockerfile:
FROM sixeyed/nginx:windowsservercore
COPY nginx.conf C:/nginx/conf
COPY index.html C:/nginx/html/index.html
CMD C:\nginx\nginx.exe
Web nginx.config:
events {
}
http {
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
Upvotes: 5
Views: 927
Reputation: 2863
Nginx is caching the resolved DNS IP of web
container.
You can configure Nginx to invalidate DNS cache(use different TTL than the one set on DNS record) by adding resolver 127.0.0.11 valid=1s
to the proxy nginx.config file.
Info on Nginx resolver: http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver
Upvotes: 0
Reputation: 7414
web
reference in the proxy
container’s nginx.conf is referring to the up of the container that is referenced when nginx server was started. When web
restarts the old ip becomes unavailable and a new ip is used, the container web
reference is then updated, however nginx server is still referencing the old ip since configuration changes are only applied on start/restart.
You need to restart the nginx proxy server—not necessarily the container—when your web container is replaced. This can be done by using docker-gen to monitor metadata regarding containers and restart nginx when a change is detected. Luckily nginx-proxy, does this for you. nginx-proxy is well maintained, and an alternative to you having to maintain this business logic yourself.
Nginx Proxy: https://github.com/nginx-proxy/nginx-proxy
Upvotes: 2