我有一个具有以下结构的项目。
ProjectName/
├── Dockerfile
├── api/
│ ├── Dockerfile
│ └── manage.py
├── docker-compose.yml
├── frontend/
│ ├── Dockerfile
│ ├── build/
│ └── src/
└── manifests/
├── development.yml
└── production.ymldev有一个在两种环境中都很常见的数据库映像,dev.yml和prod.yml对于生产和开发都有相似但略有不同的映像。
示例: api使用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:./舱单/生产资料:
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:./清单/发展:
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:发布于 2018-05-02 04:44:11
根据可以在运行时设置的环境变量,可以将运行一个或另一个命令的脚本作为入口点:
docker run -e env=DEV
# or
docker run -e env=PROD您可以设置相同的文件中的环境变量。
https://stackoverflow.com/questions/50127278
复制相似问题