1

I'm trying to create docker container with nginx, frontend and backend. Frontend is react application and backend is a django application. Here is the docker-compose file

version: '3'

services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.prod
    volumes:
      - frontend-data:/usr/share/nginx/html

  backend:
    build:
      context: .
      dockerfile: Dockerfile.prod
    environment:
      - DEBUG=0
      - ALLOWED_HOSTS=nginx,localhost,127.0.0.1
      - SECRET_KEY=django-Super-secret-key
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - static_data:/vol/web
    depends_on:
      - db
    networks:
      - app-networks
    

  db:
    image: postgres:latest
    restart: always
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - db-data:/var/lib/postgresql/data

  nginx:
    build: 
      context: ./nginx
      dockerfile: Dockerfile.prod
    ports:
      - "80:80"
    volumes:
      - frontend-data:/usr/share/nginx/html/frontend
      - static_data:/vol/static
    depends_on:
      - frontend
      - backend
    networks:
      - app-networks


networks:
  app-networks:
    driver: bridge

volumes:
  db-data:
  frontend-data:
  static_data:

in frontend Dockerfile I have moved the build files to volume and mounted it in nginx. I'm facing an error

[emerg] 1#1: host not found in upstream "backend:8000" in /etc/nginx/nginx.conf:11
nginx: [emerg] host not found in upstream "backend:8000" in /etc/nginx/nginx.conf:11

Where my nginx.conf file is

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;
    large_client_header_buffers 4 32k;

    upstream backend_servers {
        server backend:8000;
    }


    server {
        listen 80;
        server_name mysite.com;

        location / {
            return 301 https://$host$request_uri;
        }
    }

    server {
        listen 443 ssl;
        server_name mysite.com;

        ssl_certificate /etc/ssl/certs/my-site.crt;
        ssl_certificate_key /etc/ssl/private/my-site.key;

        # Static files
    location /static/ {
        alias /vol/static/;
    }


    location / {
        root /usr/share/nginx/html/frontend;
        try_files $uri $uri/ /index.html;
    }

    # Reverse proxy requests to the uWSGI server
    location /api {
        rewrite ^/api(.*) $1 break;
        uwsgi_pass backend_servers;
        include /etc/nginx/uwsgi_params;
        proxy_redirect     off;
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        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-Proto $scheme;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_buffer_size           128k;
        proxy_buffers               4 256k;
        proxy_busy_buffers_size     256k;
    }

    # Proxy pass requests for /admin to the Django admin server
    location /admin {
        uwsgi_pass backend_servers;
        include /etc/nginx/uwsgi_params;
        proxy_redirect     off;
        proxy_redirect     off;
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        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-Proto $scheme;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_buffer_size           128k;
        proxy_buffers               4 256k;
        proxy_busy_buffers_size     256k;
    }

    client_max_body_size 3M;

    }
}

the nginx.conf file is at /etc/nginx/nginx.conf

swamper
  • 117
  • 1
  • 10

1 Answers1

0

I suspect the problem is that the nginx container is starting too fast, before the backend service is ready to serve.
The depends_on parameter only creates a dependency graph, but does not wait for services to be ready.

You should take a look at this: https://docs.docker.com/compose/startup-order/.
An example here: https://stackoverflow.com/a/64921431/12530613

Auktis
  • 368
  • 1
  • 13
  • 1
    "host not found" implies failed resolution of the name `backup` to me. The port wouldn't be relevant yet at resolution time. – erik258 Apr 22 '23 at 15:58
  • @Auktis I tried adding this to my nginx but still got the same error healthcheck: test: ["CMD", "ping", "-c" ,"1", "backend"] interval: 1s timeout: 3s retries: 30 – swamper Apr 22 '23 at 16:28
  • @erik258 is there something I'm missing? – swamper Apr 22 '23 at 16:32
  • @swamper You should add the `healthcheck` on `backend` and the `condition: service_healthy` on the depends_on in nginx. – Auktis Apr 22 '23 at 16:37
  • @Auktis Thanks for that but it says the container is unhealthy – swamper Apr 22 '23 at 16:50
  • @Auktis So, take this scenario the nginx container fails to start and backend container is already up and running if I tried to run the image still it is throwing the error – swamper Apr 22 '23 at 17:02
  • @swamper If you start the nginx service manually after the backend is running and still get the error, then the problem is elsewhere. – Auktis Apr 22 '23 at 17:29
  • @Auktis Is there any way to debug it? – swamper Apr 22 '23 at 17:34
  • @Auktis Noticed a thing when upstream is in nginx.conf the error occurs but If I does the same in default.conf its working fine – swamper Apr 22 '23 at 18:05