Dockerfile 生产 / 构建 / 调试 / 测试环境

5

假设你有一个Web应用程序和一些工作流执行器:

  • http-server(提供预构建资产文件)- 生产环境
  • builder(从源代码编译/打包js/css/html)- 部署/开发环境
  • debugger/builder(即时从源代码构建,添加js源代码映射)- 开发环境
  • selenium(运行测试)- 集成测试

如何构建分层镜像以使这些工作流执行器最有效地运行?通过“最快速运行和最少编写”的方式来实现。


好的,还有单元测试,但那可能是另一个话题。 - Vanuan
那不是重复的:https://dev59.com/nYfca4cB1Zd3GeqPgkj4 它略有不同。这是一个更通用的。 - Vanuan
1个回答

2
答案可能很简单:只需创建4个Dockerfile,一个依赖于另一个。
您可以添加一个卷来共享从源代码构建的部分。问题是,您是否希望将结果资产包含在镜像中或每次从源代码构建它。
创建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. 使用带有负模式(或ADD?)的.gitignore。

*
!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 .

您也可以使用不同的git忽略文件。

  1. 挂载卷 在运行时仅使用挂载卷来跳过完全发送上下文(使用-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)

这可能并不适用于所有的测试运行器/调试器/构建工具,但这是我们应该努力追求的理想状态。在我看来。 - Vanuan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接