Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx upstream doesn't work with docker deploy stack

I'm trying to deploy a stack with docker.

Here is how my stack works:

  • nginx-proxy (redirect user requests to the good container)
  • website (simple nginx serving a website)
  • api (django application, launch with gunicorn)
  • nginx-api (serving static files and uploaded files and redirect to the API container if it is an endpoint)

This is my docker-compose.yml:

version: '3.2'

services:
    website:
        container_name: nyl2pronos-website
        image: nyl2pronos-website
        restart: always
        build:
          context: nyl2pronos_webapp
          dockerfile: Dockerfile
        volumes:
            - ./logs/nginx-website:/var/log/nginx
        expose:
            - "80"
        deploy:
            replicas: 10
            update_config:
                parallelism: 5
                delay: 10s

    api:
        container_name: nyl2pronos-api
        build:
            context: nyl2pronos_api
            dockerfile: Dockerfile
        image: nyl2pronos-api
        restart: always
        ports:
            - 8001:80
        expose:
            - "80"      
        depends_on:
            - db
            - memcached
        environment:
            - DJANGO_PRODUCTION=1
        volumes:
            - ./data/api/uploads:/code/uploads
            - ./data/api/static:/code/static

    nginx-api:
        image: nginx:latest
        container_name: nyl2pronos-nginx-api
        restart: always
        expose:
            - "80"
        volumes:          
            - ./data/api/uploads:/uploads
            - ./data/api/static:/static
            - ./nyl2pronos_api/config:/etc/nginx/conf.d
            - ./logs/nginx-api:/var/log/nginx
        depends_on:
            - api

    nginx-proxy:
        image: nginx:latest
        container_name: nyl2pronos-proxy
        restart: always
        ports:
            - 80:80
            - 443:443
        volumes: 
            - ./proxy:/etc/nginx/conf.d
            - /etc/letsencrypt:/etc/letsencrypt
            - ./logs/nginx-proxy:/var/log/nginx
        deploy:
            placement:
                constraints: [node.role == manager]
        depends_on:
            - nginx-api
            - website

When I use docker-compose up everything works fine. But when I try to deploy with docker stack deploy --compose-file=docker-compose.yml prod. My nginx config files can't find the different upstreams.

This is the error provided by my service nginx-api:

2019/03/23 17:32:41 [emerg] 1#1: host not found in upstream "api" in /etc/nginx/conf.d/nginx.conf:2

See below my nginx.conf:

upstream docker-api {
    server api;
}

server {
    listen 80;
    server_name xxxxxxxxxxxxxx;

    location /static {
        autoindex on;
        alias /static/;
    }

    location /uploads {
        autoindex on;
        alias /uploads/;
    }

    location / {
        proxy_pass         http://docker-api;
        proxy_redirect     off;
        proxy_set_header   Host $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-Host $server_name;
    }
}

If you see something wrong in my configuration or something I can do better, let me know!

like image 688
Jérémy Octeau Avatar asked Oct 28 '22 17:10

Jérémy Octeau


1 Answers

this is happening because nginx-api service is up before the api service.

but i added the depends_on option?

you are right, and this option should work for a docker-compose up case. but unfortunately not on docker stack deploy, or, as the docs put it:

The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.

ok, so what can i do now?

nothing. its actually not a bug-

docker swarm nodes (your stack services) are supposed to recover automatically on error. (thats why you define the restart: always option). so it should work for you anyway.

if you are using the compose file only for deploying the stack and not on a docker-compose up - you may remove the depends_on option completely, it means nothing to docker stack.

like image 89
Efrat Levitan Avatar answered Nov 09 '22 09:11

Efrat Levitan