Dockerfile生产/构build/debugging/testing环境

想象一下,你有你的Web应用程序和一些工作stream执行者:

  • http-server(服务预构build资产文件) – 生产
  • 生成器(从源代码编译/绑定js / css / html) – 部署/开发
  • debugging器/构build器(从源头上构build,添加js源地图) – 开发
  • selenium(运行testing) – 集成testing

我们如何构build分层的图像来让这些工作stream执行者最有效地工作? 我的意思是“跑得最快,写得最less”。

答案可能很简单:只要创build4个Dockerfile就可以了。

您可以添加一个卷来共享来自源代码部分的构build。 问题是你想要结果资产包含在图像中,还是每次都从源代码构build。

创build4个文件夹,每个文件夹都有Dockerfile

生产

production/Dockefile

 FROM # put server here COPY # put config here # some other option # volume sharing? 

build立

build/Dockerfile

 # install dependencies ADD # add sources here RUN # some building script 

debugging

debug/Dockefile

 # ideally, configure production or build image 

testing

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文件。

  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)