【发布时间】:2018-10-12 03:05:26
【问题描述】:
我有一个具有以下结构的项目。
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 开发人员使用 django 并仅运行 python manage.py runserver,但在 prod 中它将运行 gunicorn api.wsgi。
前端运行npm start,但在产品中,我希望它基于不同的图像。目前 dockerfile 仅适用于其中一种,因为 npm 命令仅在我使用 FROM node 时才出现,nginx 命令仅在我使用 FROM kyma/docker-nginx 时出现。
那么在不同的环境中如何将它们分开呢?
./frontend/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:
【问题讨论】:
标签: django docker nginx docker-compose gunicorn