使用Docker和nginx将Django项目部署到AWS EB

时间:2020-10-13 15:54:57

标签: python django amazon-web-services docker nginx

当前,我尝试将Django项目部署到AWS EB,但是我遇到了很多问题。我可以对项目进行dockerize并将其部署在AWS Elastic beantalk上。但是,当我尝试访问该站点时,总是看到:502 Bad Gateway。在本地,该项目运行顺利。我不太喜欢nginx,也不知道如何解决这个问题。

这是我的项目结构: Project structure

这是我的Dockerfile:

# Creating image based on official python3 image
FROM python:3

MAINTAINER Jaron Bardenhagen

# Sets dumping log messages directly to stream instead of buffering
ENV PYTHONUNBUFFERED 1

# Creating and putting configurations
RUN mkdir /config
ADD config/app /config/

# Installing all python dependencies
RUN pip install -r /config/requirements.txt

# Open port 8000 to outside world
EXPOSE 8000

# When container starts, this script will be executed.
# Note that it is NOT executed during building
CMD ["sh", "/config/on-container-start.sh"]

# Creating and putting application inside container
# and setting it to working directory (meaning it is going to be default)
RUN mkdir /app
WORKDIR /app
ADD app /app/

这是我的docker-compose文件:

# File structure version
version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_DB_PORT: "5432"
      POSTGRES_DB_HOST: "*******"
      POSTGRES_PASSWORD: "*******"
      POSTGRES_USER: Jaron
      POSTGRES_DB: ebdb
  # Build from remote dockerfile
  # Connect local app folder with image folder, so changes will be pushed to image instantly
  # Open port 8000
  app:
    build:
      context: .
      dockerfile: config/app/Dockerfile
    hostname: app
    volumes:
      - ./app:/app
    expose:
      - "8000"
    depends_on:
      - db
  # Web server based on official nginx image
  # Connect external 8000 (which you can access from browser)
  # with internal port 8000(which will be linked to app port 8000 in configs)
  # Connect local nginx configuration with image configuration
  nginx:
    image: nginx
    hostname: nginx
    ports:
      - "8000:8000"
    volumes:
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - app

这是Dockerrun.aws文件:

{
 "AWSEBDockerrunVersion": "1",
 "Image": {
   "Name": "******/******:latest",
   "Update": "true"
 },
 "Ports": [
   {
     "ContainerPort": "8000"
   }
 ]
}

container-start.sh文件:

# Create migrations based on django models
python manage.py makemigrations

# Migrate created migrations to database
python manage.py migrate
# Start gunicorn server at port 8000 and keep an eye for app code changes
# If changes occur, kill worker and start a new one
gunicorn --reload project.wsgi:application -b 0.0.0.0:8000

这是Nginx设置的文件(app.conf):

# define group app
upstream app {
  # balancing by ip
  ip_hash;

  # define server app
  server app:8000;
}

# portal
server {

  # all other requests proxies to app
  location / {
    proxy_pass http://app/;
  }

  # only respond to port 8000
  listen 8000;

  # domain localhost
  server_name localhost;
}

我真的很感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

  • 首先在堆栈溢出上检查以下问题:link to question。 如果这样做不能解决您的问题,请尝试以下建议。
  • 尝试用RDS替换postgress数据库,并确保您的RDS和EB环境在同一VPC中。尝试在EB上部署django项目时遇到了这个问题。
  • 删除Postgress docker映像并将其与RDS连接解决了我的问题。
  • 出于某种原因,EB正在停止发布后期图像。