如何为开发和生产配置不同的dockerfile [英] How to configure different dockerfile for development and production

查看:295
本文介绍了如何为开发和生产配置不同的dockerfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将docker用于laravel项目的开发和生产。我用于开发和生产的dockerfile略有不同。例如,我在开发环境中将本地目录安装到docker容器中,这样我就不需要为代码中的每个更改进行docker构建。



由于挂载目录仅在运行docker容器时可用,因此我无法将 composer install或 npm install之类的命令放入dockerfile进行开发。

p>

目前,我正在管理两个docker文件,有什么方法可以使用单个docker文件来执行此操作,并通过发送参数来确定执行docker build时要运行的命令。



我要实现的目标是



在docker文件中

  ... 
如果PROD然后运行composer,请安装
...

在docker构建期间

  docker build [PROD] -t mytag。 


解决方案

更新(2020):
自从三年前编写此书以来,许多事情发生了变化(包括我对此主题的看法)。我建议的方法是使用一个dockerfile 并使用脚本。请参阅@yamenk的答案。


原始:


您可以使用两个不同的Dockerfile。

 #./Dockerfile(非生产)
从foo / bar
MAINTAINER ...

#... 。

第二个:

 #./Dockerfile.production 
从foo / bar
MAINTAINER ...

RUN composer install

在调用build命令时,您可以知道应该使用哪个文件:

  $> docker build -t mytag。 
$> docker build -t mytag-production -f Dockerfile.production。


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.

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.

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.

What I am trying to achieve is

In docker file

...
IF PROD THEN RUN composer install
...

During docker build

docker build [PROD] -t mytag .

解决方案

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.

ORIGINAL:

You can use two different Dockerfiles.

# ./Dockerfile (non production)
FROM foo/bar
MAINTAINER ...

# ....

And a second one:

# ./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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆