Cannot connect to the backend server running port 8000 using nginx, django and docker

I've spent two days trying to figure this out. I'm running docker containers that host a django+react website from a docker compose file.

I can access the website on port 80 (IP: http://52.90.163.11:80), but I can't seem to access the django admin panel on port 8000 (IP should be http://52.90.163.11:8000 but it doesn't work). I'm using AWS to host my website.

I simply want the backend container accessible via port 8000.

I have two docker compose files. One I build on my local machine. After building on local machine, I push the images to dockerhub. The second dockerfile resides on the AWS server, and uses images from the first build.

Here is my docker-compose file on my local machine to create the images.

version: '3'

services:
  backend:
    build:
      context: ./backend/src
    command: gunicorn djreact.wsgi --bind 0.0.0.0:8000
    ports:
      - 8000:8000
    depends_on:
      - pgdb
  pgdb:
    image: postgres
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
    volumes:
      - pgdata:/var/lib/postgresql/data
  frontend:
    build:
      context: ./frontend/gui 
    volumes:
      - react_build:/frontend/build
  nginx:
    image: nginx:latest
    ports:
      - 80:8080
    volumes:
      - ./nginx/nginx_setup.conf:/etc/nginx/conf.d/default.conf:ro
      - react_build:/var/www/react
    depends_on:
      - backend
      - frontend

volumes:
  react_build:
  pgdata:

Here is my dockerfile on my AWS server. It uses the images created on my local machine.

version: '3'

services:
  backend:
    image: ansariuminhaj/mynacode:mynacode-backend
    command: gunicorn djreact.wsgi --bind 0.0.0.0:8000
    ports:
      - 8000:8000
    depends_on:
      - pgdb
  pgdb:
    image: ansariuminhaj/mynacode:postgres 
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
    volumes:
      - pgdata:/var/lib/postgresql/data
  frontend:
    image: ansariuminhaj/mynacode:mynacode-frontend 
    volumes:
      - react_build:/frontend/build
  nginx:
    image: ansariuminhaj/mynacode:nginx
    ports:
      - 80:8080
    volumes:
      - ./nginx/nginx_setup.conf:/etc/nginx/conf.d/default.conf:ro
      - react_build:/var/www/react
    depends_on:
      - backend
      - frontend

volumes:
  react_build:
  pgdata:

Here is my nginx conf file:

upstream api {
      server backend:8000;
}

server {
    listen 8080;

    server_name 52.90.163.11;

    location / {
            proxy_read_timeout 300s;
            proxy_connect_timeout 75s;
        root /var/www/react;
        try_files $uri /index.html;
    }

    location /api/ {
            proxy_read_timeout 300s;
            proxy_connect_timeout 75s;
        proxy_pass http://api;
        proxy_set_header Host $http_host;
    }

}

docker ps shows this on my AWS server.

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

9852c9738331 ansariuminhaj/mynacode:nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 80/tcp, 0.0.0.0:80->8080/tcp, :::80->8080/tcp mynacode_nginx_1

b9709cbe37a1 ansariuminhaj/mynacode:mynacode-backend "gunicorn djreact.ws…" 9 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp mynacode_backend_1

5eebbbcae314 ansariuminhaj/mynacode:postgres "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 5432/tcp mynacode_pgdb_1

I've tried creating a new AWS instance, docker system prune and recreate images/volume, but the problem still persists.

so I found the solution! The docker files are correct. I had to log onto the Amazon Instance and enable port 8000 so that it accepts requests on that port!

Back to Top