如何为开发和生产配置不同的 dockerfile [英] How to configure different dockerfile for development and production
问题描述
我使用 docker 进行 laravel 项目的开发和生产.对于开发和生产,我的 dockerfile 略有不同.例如,我在开发环境中将本地目录挂载到 docker 容器,这样我就不需要为代码的每次更改都进行 docker build.
I use docker for development and in production for laravel project. I have slightly different dockerfile for development and production. For example I am mounting local directory to docker container in development environment so that I don't need to do docker build for every change in code.
由于挂载的目录只有在运行 docker 容器时才可用,所以我不能在 dockerfile 中放置composer install"或npm install"等命令进行开发.
As mounted directory will only be available when running the docker container I can't put commands like "composer install" or "npm install" in dockerfile for development.
目前我正在管理两个 docker 文件,有什么方法可以使用单个 docker 文件执行此操作,并通过发送参数来决定在执行 docker build 时运行哪些命令.
Currently I am managing two docker files, is there any way that I can do this with single docker file and decide which commands to run when doing docker build by sending parameters.
我想要实现的是
在 docker 文件中
...
IF PROD THEN RUN composer install
...
在 docker 构建期间
docker build [PROD] -t mytag .
推荐答案
更新(2020):自从这是 3 年前写的,很多事情都发生了变化(包括我对这个话题的看法).我建议的方法是使用 one dockerfile 并使用脚本.请参阅 @yamenk 的回答.
UPDATE (2020): Since this was written 3 years ago, many things have changed (including my opinion about this topic). My suggested way of doing this, is using one dockerfile and using scripts. Please see @yamenk's answer.
原文:
您可以使用两个不同的 Dockerfile.
You can use two different Dockerfiles.
# ./Dockerfile (non production)
FROM foo/bar
MAINTAINER ...
# ....
还有第二个:
# ./Dockerfile.production
FROM foo/bar
MAINTAINER ...
RUN composer install
在调用构建命令时,您可以知道它应该使用哪个文件:
While calling the build command, you can tell which file it should use:
$> docker build -t mytag .
$> docker build -t mytag-production -f Dockerfile.production .
这篇关于如何为开发和生产配置不同的 dockerfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!