I
I
Iceforest2021-03-17 11:52:30
Nginx
Iceforest, 2021-03-17 11:52:30

How to solve nginx issue when building docker-compose?

Wrote the following docker compose file

version: '2'

services:
    nginx_todo:
      build:
        context: ./nginx
      volumes:
        - ${APP_CODE_TODO_PATH_HOST}:${APP_CODE_TODO_PATH_CONTAINER}
        - ${NGINX_TODO_HOST_LOG_PATH}:/var/log/nginx
        - ${NGINX_TODO_SITE_PATH}:/etc/nginx/sites-available/todo.conf
        - ${NGINX_SSL_PATH}:/etc/nginx/ssl
      ports:
        - "${NGINX_TODO_HOST_HTTP_PORT}:80"
        - "${NGINX_TODO_HOST_HTTPS_PORT}:443"
      depends_on:
        - todo
      links:
        - todo 
      networks:
        - frontend
        - backend

    todo:
      build:
        context: ./web
        args:
          - INSTALL_GIT=${INSTALL_GIT}
          - INSTALL_ZIP=${INSTALL_ZIP}
          - INSTALL_NPM=${INSTALL_NPM}
          - INSTALL_CRON=${INSTALL_CRON}
          - WORK_DIR=${APP_CODE_TODO_PATH_CONTAINER}
          - NODE_VERSION=${NODE_VERSION}
      volumes:
        - ./web/cron/crontab_todo:/etc/crontab
        - ${APP_CODE_TODO_PATH_HOST}:${APP_CODE_TODO_PATH_CONTAINER}   
      expose:
        - "9000"
      depends_on:
        - db
        - redis
      networks:
        - backend

    consumers:
      build:
        context: ./consumers
        args:
          - INSTALL_GIT=${INSTALL_GIT}
          - INSTALL_ZIP=${INSTALL_ZIP}
          - INSTALL_NPM=${INSTALL_NPM}
          - INSTALL_CRON=${INSTALL_CRON}
          - INSTALL_SUPERVISOR=${INSTALL_SUPERVISOR}
          - WORK_DIR=${APP_CODE_TODO_PATH_CONTAINER}
      volumes:
          - ./consumers/supervisor/todo.conf:/etc/supervisor/conf.d/todo.conf 
          - ${APP_CODE_TODO_PATH_HOST}:${APP_CODE_TODO_PATH_CONTAINER}   
      expose:
        - "9000"
      depends_on:
        - db
        - redis
      networks:
        - backend

    db:
      build:
        context: ./mysql
        args:
          - MYSQL_VERSION=${MYSQL_VERSION}
      environment:
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
        - TZ=${WORKSPACE_TIMEZONE}
      volumes:
        - ${DATA_PATH_HOST}/mysql:/var/lib/mysql
        - ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
      ports:
        - "${MYSQL_PORT}:3306"
      networks:
        - backend

    redis:
      ##build
      build:
        context: ./redis
      volumes:
        - ${DATA_PATH_HOST}/redis:/data
      ports:
        - "${REDIS_PORT}:6379"
      networks:
        - backend            

networks:
  frontend:
    driver: ${NETWORKS_DRIVER}
  backend:
    driver: ${NETWORKS_DRIVER}

volumes:
  db:
    driver: ${VOLUMES_DRIVER}   
  redis:
    driver: ${VOLUMES_DRIVER}


and there is such a config for nginx(todo.conf)

server {
    
        listen 80;
        listen [::]:80;
    
        # For https
        # listen 443 ssl;
        # listen [::]:443 ssl ipv6only=on;
        # ssl_certificate /etc/nginx/ssl/default.crt;
        # ssl_certificate_key /etc/nginx/ssl/default.key;
    
        #server_name _;
    
        location / {
            try_files $uri @wsgi;
        }
    
        # Django media
        location /media  {
            alias /var/www/todo/media;  # your Django project's media files - amend as required
        }
    
        location /static {
            alias /var/www/todo/static; # your Django project's static files - amend as required
        }
    
        # Finally, send all non-media requests to the Django server.
        location @wsgi {
            uwsgi_pass  todo:9000;
            include     uwsgi_params; # the uwsgi_params file you installed
        }  
    
        location ~ /\.ht {
            deny all;
        }
    
        location /.well-known/acme-challenge/ {
            root /var/www/letsencrypt/;
            log_not_found off;
        }
    
        error_log /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
    }


running docker-compose up starts redis,db and nginx throws this
nginx error: [emerg] host not found in upstream "todo" in /etc/nginx/sites-available/todo.conf:29

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor Makhov, 2021-03-17
@Igorgro

Because upstream must be set in nginx.conf. What is written in docker-compose, nginx does not know

I
Ivan Koryukov, 2021-03-17
@MadridianFox

Try using the upstream directive . It is more tolerant of the absence of the target host at the time of the nginx stratum.

I
IgorOhrimenko, 2021-03-19
@IgorOhrimenko

upstream is a good option, but you can also use this script: https://docs.docker.com/compose/startup-order/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question