Reputation: 39649
I have tried various options e.g. expose
, bridge
, networks
options of docker-compose
but can't get it to work with the nginx connection to upstream gunicorn running in separate container, I am receiving 502 Bad Gateway
error from nginx. I am not sure as to what i am missing exactly. Below is my docker-compose
file:
version: "3"
services:
web:
build: .
container_name: web
command: bash -c "/start_web.sh"
restart: always
depends_on:
- worker
ports:
- "80:80"
- "443:443"
worker:
build: .
container_name: worker
command: bash -c "/start_worker.sh"
restart: always
ports:
- "8000:8000"
nginx conf:
upstream worker {
server 127.0.0.1:8000;
}
server {
listen 80 default_server;
location / {
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Url-Scheme $scheme;
proxy_redirect off;
# Mitigate httpoxy attack
proxy_set_header Proxy "";
proxy_pass http://worker;
}
}
Gunicorn config:
import multiprocessing
import os
bind = '127.0.0.1:8000'
default_workers = multiprocessing.cpu_count() * 2 + 1
workers = os.getenv('GUNICORN_WORKERS', os.getenv('WEB_CONCURRENCY', default_workers))
worker_class = 'tornado'
# This is to fix issues with compressor package: broken offline manifest for
# custom domain. It randomly breaks, I think because of global variable inside.
preload_app = True
timeout = 200
graceful_timeout = 60
max_requests = 250
max_requests_jitter = max_requests
accesslog = '/tmp/gunicorn_access.log'
errorlog = '/tmp/gunicorn_error.log'
Circus ini files:
web.ini
[watcher:nginx]
cmd = /usr/sbin/nginx
stop_signal = QUIT
worker.ini
[watcher:gunicorn]
cmd = /usr/local/bin/gunicorn test:app -c /etc/gunicorn/app.py
working_dir = /opt/app
copy_env = True
uid = www-data
The whole code is available on github as well at repository docker_test for your ease to test it.
Upvotes: 1
Views: 1897
Reputation: 39237
Gunicon config:
bind = '127.0.0.1:8000'
This will bind to loopback
interface (localhost only), change it to 0.0.0.0
to bind to every available interface in the container. This will make it reachable from nginx.
Nginx config:
upstream worker {
server 127.0.0.1:8000;
}
You need to change loopback
ip to DNSname/IP
of worker container. I recommend creating an user-defined network, Then put all containers that are related in that network and call them by DNS names. You wont have internal DNS in default bridge network so following nginx config wont work.
upstream worker {
server worker:8000;
}
Upvotes: 3