这是我当前的项目设置,我想知道是否有更优雅的方式。当前设置如下。
目录结构
<root>
- Dockerfile_base # base image for the other two
- Dockerfile_dev # development image
- Dockerfile_prod # production image
- Makefile
Dockerfiles:
# Dockerfile_base
FROM tensorflow/tensorflow:2.4.1-gpu
RUN pip install ...
# Dockerfile_dev
FROM eu.gcr.io/cool_project/cool_program_base:latest
RUN pip install <dev branch of this repo>
# Dockerfile_prod
FROM eu.gcr.io/cool_project/cool_program_base:latest
RUN pip install <master branch of this repo>
生成文件
deploybase:
docker build -f Dockerfile_base -t cool_program_base:latest .
docker tag cool_program_base:latest eu.gcr.io/cool_project/cool_program_base
docker push eu.gcr.io/cool_project/cool_program_base
deploydev:
docker build -f Dockerfile_dev -t cool_program_dev:latest .
docker tag cool_program_dev:latest eu.gcr.io/cool_project/cool_program_dev
docker push eu.gcr.io/cool_project/cool_program_dev
deployprod:
docker build -f Dockerfile_prod -t cool_program_prod:latest .
docker tag cool_program_prod:latest eu.gcr.io/cool_project/cool_program_prod
docker push eu.gcr.io/cool_project/cool_program_prod
Q1:有没有办法将三个 Dockerfile 合并为一个?我知道有多阶段构建,但我找不到如何使其工作。
Q2:如果可以的话,Makefile也能写的更紧凑吗?
答案 0 :(得分:2)
对于 docker 镜像,您可以使用 build-args,即具有单个参数化 Dockerfile:
Content-Type
那么:
ARG BRANCH=dev-branch
FROM eu.gcr.io/cool_project/cool_program_base:latest
RUN pip install $BRANCH
或
docker build -f Dockerfile --build-arg BRANCH=master -t cool_program_prod:latest .
你实际上不需要推送“cool_program_base”镜像,因为它的层已经包含在 dev 和 prod 镜像中。