django - 用于开发和生产的 Dockerfile 的单独进程?

标签 django docker nginx docker-compose gunicorn

我有一个具有以下结构的项目。

ProjectName/
├── Dockerfile
├── api/
│   ├── Dockerfile
│   └── manage.py
├── docker-compose.yml
├── frontend/
│   ├── Dockerfile
│   ├── build/
│   └── src/
└── manifests/
    ├── development.yml
    └── production.yml

docker-compose.yml 有一个在两种环境中通用的数据库镜像,dev.yml 和 prod.yml 具有相似但略有不同的生产和开发镜像。

示例:api dev 使用 django 并且只运行 python manage.py runserver , 但在 prod 中它会运行 gunicorn api.wsgi .

前端运行 npm start但在 prod 中,我希望它基于不同的图像。当前 dockerfile 只适用于其中之一,因为 npm仅当我使用 FROM node 时才找到命令和 nginx命令仅在我使用 FROM kyma/docker-nginx 时出现.

那么在不同的环境中如何将它们分开呢?

./前端/Dockerfile:
FROM node

WORKDIR /app/frontend
COPY package.json /app/frontend

RUN npm install

EXPOSE 3000
CMD ["npm", "start"]

# Only run this bit in production environment, and not anything above this line.
#FROM kyma/docker-nginx
#COPY build/ /var/www
#CMD 'nginx'

./api/Dockerfile:
FROM python:3.5

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        postgresql-client \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app/api
COPY requirements.txt /app/api
RUN pip install -r requirements.txt

EXPOSE 8000

# Run this command in dev
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
# Run this command in prod
#CMD ["gunicorn", "api.wsgi", "-b 0.0.0.0:8000"]

./docker-compose.yml:
version: '3'

services:
  db:
    image: postgres
    restart: always
    ports:
  - "5432:5432"

volumes:
  node-modules:

./manifests/production.yml:
version: '3'

services:
  gunicorn:
    build: ./api
    command: ["gunicorn", "api.wsgi", "-b", "0.0.0.0:8000"]
    restart: always
    volumes:
      - ./api:/app/api
    ports:
      - "8000:8000"
    depends_on:
      - db
  nginx:
    build: ./frontend
    command: ["nginx"]
    restart: always
    volumes:
      - ./frontend:/app/frontend
      - ./frontend:/var/www
      - node-modules:/app/frontend/node_modules
    ports:
      - "80:80"

volumes:
  node-modules:

./manifests/development.yml:
version: '3'

services:
  django:
    build: ./api
    command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
    restart: always
    volumes:
      - ./api:/app/api
    ports:
      - "8000:8000"
    depends_on:
      - db
  frontend:
    build: ./frontend
    command: ["npm", "start"]
    restart: always
    volumes:
      - ./frontend:/app/frontend
      - node-modules:/app/frontend/node_modules
    ports:
      - "3000:3000"

volumes:
  node-modules:

最佳答案

您可以将脚本作为 ENTRYPOINT 运行一个或另一个命令,具体取决于您可以在运行时设置的环境变量:

docker run -e env=DEV
# or
docker run -e env=PROD

您可以设置相同的 environment variable in a docker compose file .

关于django - 用于开发和生产的 Dockerfile 的单独进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127278/

相关文章:

python - 我怎样才能在 django 的 barre 链接中显示特定的 url

json - 读取 JSON 文件时发生 KeyError

django - 模块 'jwt' 没有属性 'ExpiredSignature'

ubuntu - 无法连接到 Docker 守护进程

php - Apache 上的 Docker,如何将变量传递给我的 PHP Web 应用程序?

php - ubuntu 的永久环境变量(系统、php、phpfpm 和 nginx 或 apache)

python - 获取 "No module named django_messages"

docker - 如何在 docker-compose.yml 中设置/获取项目名称

ssl - 在 Ubuntu 16 和 Nginx 中禁用 Diffie-Hellman (DH) key

ruby-on-rails - 将 rails+nginx webapp 转换为使用 HTTPS