Home > Software design >  How to deploy fastapi app to EC2 with docker-compose.yaml and dockerized nginx server
How to deploy fastapi app to EC2 with docker-compose.yaml and dockerized nginx server

Time:01-11

Trying to switch to using docker-compose to deploy the application on EC2. At the same time, I want nginx to be deployed in docker image as well.

Dockerfile

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9

WORKDIR /app

COPY ./requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir -r /app/requirements.txt

COPY . .

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

docker-compose.yaml

version: '3.9'

services:
  web:
    env_file: .env
    build: .
    command: sh -c "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port 8000"
    volumes:
      - .:/app
    ports:
      - 8000:8000
    depends_on:
      - db
      - redis
  db:
    image: postgres:11
    volumes:
      - postgres_data:/var/lib/postgresql/data
    expose:
      - 5432
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASS}
      - POSTGRES_DB=${DB_NAME}
  redis:
    image: redis:6-alpine
    volumes:
      - redis_data:/data
  nginx:
    image: nginx:latest
    ports:
      - "8080:8080"
    volumes:
      - ./nginx_config.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - web

volumes:
  postgres_data:
  redis_data:

nginx_config.conf

server {
    listen 8080;
    server_name 3.70.228.88;

    location /static/ {
    root /app;
    try_files $uri $uri/ =404;
    }

    location / {
    proxy_pass http://web:8000;
    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;
    }
}

Runing sudo docker-compose up. No any errors, looks good

ubuntu@ip-172-31-24-232:~/wplay$ sudo docker-compose up
Starting wplay_redis_1 ... done
Starting wplay_db_1    ... done
Starting wplay_web_1   ... done
Starting wplay_nginx_1 ... done
Attaching to wplay_redis_1, wplay_db_1, wplay_web_1, wplay_nginx_1
db_1     | 
db_1     | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1     | 
db_1     | 2023-01-10 14:18:04.378 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1     | 2023-01-10 14:18:04.378 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1     | 2023-01-10 14:18:04.384 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1     | 2023-01-10 14:18:04.417 UTC [24] LOG:  database system was shut down at 2023-01-07 10:52:36 UTC
db_1     | 2023-01-10 14:18:04.451 UTC [1] LOG:  database system is ready to accept connections
redis_1  | 1:C 10 Jan 2023 14:18:04.034 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 10 Jan 2023 14:18:04.034 # Redis version=6.2.8, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 10 Jan 2023 14:18:04.034 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1  | 1:M 10 Jan 2023 14:18:04.034 * monotonic clock: POSIX clock_gettime
redis_1  | 1:M 10 Jan 2023 14:18:04.037 * Running mode=standalone, port=6379.
redis_1  | 1:M 10 Jan 2023 14:18:04.038 # Server initialized
redis_1  | 1:M 10 Jan 2023 14:18:04.038 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1  | 1:M 10 Jan 2023 14:18:04.039 * Loading RDB produced by version 6.2.8
redis_1  | 1:M 10 Jan 2023 14:18:04.039 * RDB age 271528 seconds
redis_1  | 1:M 10 Jan 2023 14:18:04.039 * RDB memory usage when created 0.77 Mb
redis_1  | 1:M 10 Jan 2023 14:18:04.039 # Done loading RDB, keys loaded: 0, keys expired: 0.
redis_1  | 1:M 10 Jan 2023 14:18:04.039 * DB loaded from disk: 0.001 seconds
redis_1  | 1:M 10 Jan 2023 14:18:04.039 * Ready to accept connections
nginx_1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1  | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx_1  | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx_1  | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1  | 2023/01/10 14:18:05 [notice] 1#1: using the "epoll" event method
nginx_1  | 2023/01/10 14:18:05 [notice] 1#1: nginx/1.23.3
nginx_1  | 2023/01/10 14:18:05 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
nginx_1  | 2023/01/10 14:18:05 [notice] 1#1: OS: Linux 5.15.0-1026-aws
nginx_1  | 2023/01/10 14:18:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx_1  | 2023/01/10 14:18:05 [notice] 1#1: start worker processes
nginx_1  | 2023/01/10 14:18:05 [notice] 1#1: start worker process 28
web_1    | INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
web_1    | INFO  [alembic.runtime.migration] Will assume transactional DDL.
web_1    | INFO:     Started server process [8]
web_1    | INFO:     Waiting for application startup.
web_1    | INFO:     Application startup complete.
web_1    | INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL C to quit)

But when I go to the http://<ip address> I get

This site can't be reached

What's my mistake?

upd

enter image description here

CodePudding user response:

What about your security group ? Did you opened the HTTP port range on port 80 for example ?

  • Related