簡體   English   中英

使用 Docker 和 nginx 將 Django 項目部署到 AWS EB

[英]Deploy a Django project to AWS EB using Docker and nginx

目前,我嘗試將 Django 項目部署到 AWS EB,但我面臨很多問題。 我可以對項目進行 dockerize 並將其部署在 AWS 彈性 beantalk 上。 但是當我嘗試訪問該站點時,我總是看到:502 Bad Gateway。 在當地,該項目運行順利。 我不是很喜歡 nginx,也不知道如何解決這個問題。

這是我的項目結構:項目結構

這是我的 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"
   }
 ]
}

On-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;
}

我真的很感激任何形式的幫助!

  • 首先檢查堆棧溢出的這個問題: 鏈接到問題 如果這不能解決您的問題,請嘗試以下建議。
  • 嘗試用 RDS 替換 postgress 數據庫,並確保您的 RDS 和 EB 環境在同一 VPC 中。 當我嘗試在 EB 上部署 django 項目時遇到了這個問題。
  • 刪除 Postgress docker 映像並將其與 RDS 連接解決了我的問題。
  • 出於某種原因,EB 正在停止 postgress 圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM