如何为开发和生产配置不同的dockerfile

编程入门 行业动态 更新时间:2024-10-24 20:21:57
本文介绍了如何为开发和生产配置不同的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

本文发布于:2023-11-01 01:16:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   dockerfile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!