答案可能很简单:只需根据另一个创建 4 个Dockerfiles。
您可以添加一个卷以从源部分共享构建。问题是您是希望结果资产包含在图像中还是每次都从源构建它。
创建 4 个文件夹,每个文件夹中都有 Dockerfile。
生产
production/Dockefile:
FROM # put server here
COPY # put config here
# some other option
# volume sharing?
构建
build/Dockerfile:
# install dependencies
ADD # add sources here
RUN # some building script
调试
debug/Dockefile:
# ideally, configure production or build image
测试
test/Dockefile:
FROM # import production
# install test dependencies
RUN # test runner
还有几个选项。
1. 使用带有否定模式的 .gitignore(或 ADD?)
*
!directory-i-want-to-add
!another-directory-i-want-to-add
另外使用 docker 命令指定 dockerfiles 和上下文:
docker build -t my/debug-image -f docker-debug .
docker build -t my/serve-image -f docker-serve .
docker build -t my/build-image -f docker-build .
docker build -t my/test-image -f docker-test .
你也可以使用不同的 gitignore 文件。
- 装载卷
完全跳过发送上下文,只需在运行时使用安装卷(使用
-v host-dir:/docker-dir)。
所以你必须:
docker build -t my/build-image -f docker-build . # build `build` image (devtools like gulp, grunt, bundle, npm, etc)
docker run -v output:/output my/build-image build-command # copies files to output dir
docker build -t my/serve-image -f docker-serve . # build production from output dir
docker run my/serve-image # production-like serving from included or mounted dir
docker build -t my/serve-image -f docker-debug . # build debug from output dir
docker run my/serve-image # debug-like serving (uses build-image with some watch magic)