简体   繁体   English

如何使用 docker-compose.yaml 和 dockerized nginx 服务器将 fastapi 应用程序部署到 EC2

[英]How to deploy fastapi app to EC2 with docker-compose.yaml and dockerized nginx server

Trying to switch to using docker-compose to deploy the application on EC2.尝试切换到使用 docker-compose 在 EC2 上部署应用程序。 At the same time, I want nginx to be deployed in docker image as well.同时,我希望 nginx 也部署在 docker 镜像中。

Dockerfile 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 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 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 .运行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但是当我 go 到http://<ip address>我得到

This site can't be reached

What's my mistake?我的错误是什么?

upd更新

在此处输入图像描述

What about your security group?你的安全组呢? Did you opened the HTTP port range on port 80 for example?例如,您是否在端口 80 上打开了 HTTP 端口范围?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 aws ec2 中使用 jenkins docker 容器设置部署 nodejs 应用程序 - How to setup deploy nodejs app using jenkins docker container in aws ec2 Docker-compose 启动 AWS EC2 服务器后自动启动 - Docker-compose auto-start after lauching AWS EC2 server 使用 CloudFormation 运行 Ec2 中存在的 docker-compose - Use CloudFormation to run a docker-compose present in an Ec2 如何使用 GitHubActions 将 angular 应用程序部署到 EC2,并使用 Yarn 作为 package 管理器? - How to deploy and angular app using GitHubActions to EC2 with Yarn as the package manager? 在 Amazon EC2 上安装 docker-compose Linux 2. 9kb docker-compose 文件 - Installing docker-compose on Amazon EC2 Linux 2. 9kb docker-compose file ec2 上 next.js 应用程序的 nginx 配置问题 - problem with the nginx config for a next js app on ec2 如何更新 EC2 服务器上的 RPM? - How to update RPM on an EC2 server? 如何在具有 mysql 数据库和 costum 域的 windows 服务器 ec2 实例上运行 django 应用程序 - How to run django app on windows server ec2 instance with mysql database and costum domain 如何在 2 个 ec2 实例上连接 2 个不同的 docker 容器? - How can I connect 2 different docker containers on 2 ec2 instances? 如何使用 docker-compose 将容器部署到谷歌云? - How to deploy container using docker-compose to google cloud?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM