cpd1
cpd1

Reputation: 789

Can't connect to web app via nginx in docker container on Windows

I'm able to connect to the postgres instance with no issue but can't figure out why nothing happens when I try to access localhost or http://192.168.72.151/ (Docker Desktop). I've included the relevant files below that I used for configuration. The nginx server seems to have started up correctly but honestly not 100% sure either. I don't see any activity in terminal when trying to connect as I do when I connect to the db.

docker compose file...

version: '3'

services:

  app-server:
    build: ./app-server
    restart: always
    expose:
      - "8000"
    volumes:
      - ./app-server:/home/flask/app/web
    depends_on:
      - redis
      - postgres
    links:
      - redis
      - postgres
    env_file:
      - .env
    deploy:
      mode: replicated
      replicas: 3

  redis:
    image: redis:alpine
    restart: always
    deploy:
      mode: replicated
      replicas: 3

  nginx:
    build: nginx/
    restart: always
    ports:
      - "80"
    deploy:
      mode: replicated
      replicas: 3
    depends_on:
      - app-server

  postgres:
    restart: always
    image: postgres:alpine
    volumes:
      - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
      - ./postgres/data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=appserver
      - POSTGRES_PASSWORD=appserver123
      - POSTGRES_DB=appserver
    ports:
      - "5433:5432"

nginx.conf...

user  nginx;
worker_processes 1;
error_log  /dev/stdout info;
error_log off;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    use epoll;
    multi_accept on;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /dev/stdout main;
    access_log off;
    keepalive_timeout 65;
    keepalive_requests 100000;
    tcp_nopush on;
    tcp_nodelay on;

    server {
        listen 80;
        proxy_pass_header Server;

        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;

            proxy_pass http://app-server:8000/;
        }
    }
}

This is docker ps...

         Name                        Command               State                    Ports
-----------------------------------------------------------------------------------------------------------
app-server_app-server_1   /usr/local/bin/gunicorn -- ...   Up      8000/tcp
app-server_app-server_2   /usr/local/bin/gunicorn -- ...   Up      8000/tcp
app-server_app-server_3   /usr/local/bin/gunicorn -- ...   Up      8000/tcp
app-server_nginx_1        /docker-entrypoint.sh ngin ...   Up      0.0.0.0:51651->80/tcp
app-server_nginx_2        /docker-entrypoint.sh ngin ...   Up      0.0.0.0:51652->80/tcp
app-server_nginx_3        /docker-entrypoint.sh ngin ...   Up      0.0.0.0:51650->80/tcp
app-server_postgres_1     docker-entrypoint.sh postgres    Up      0.0.0.0:5433->5432/tcp,:::5433->5432/tcp
app-server_redis_1        docker-entrypoint.sh redis ...   Up      6379/tcp
app-server_redis_2        docker-entrypoint.sh redis ...   Up      6379/tcp
app-server_redis_3        docker-entrypoint.sh redis ...   Up      6379/tcp

Upvotes: 0

Views: 3365

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191738

First step to verify nginx is working is docker exec into one of the instances, and then curl localhost from there

From the host, you have three nginx containers, so you'd need to connect to one of the ports mentioned before ->80 shown in docker ps since those are the host ports

Otherwise, remove mode: replicated, replicas: 3 from nginx (it is fine to leave it on the app-server, since you are making that be load-balanced... you probably dont need it on redis either) and just use one container port mapping of 80:80, and then http://localhost should work

Upvotes: 2

Related Questions