如何在docker build中提供和使用命令行参数?

我有一个Dockerfile,如下所示:

FROM centos:centos6 MAINTAINER tapash ###### Helpful utils RUN yum -y install sudo RUN yum -y install curl RUN yum -y install unzip #########Copy hibernate.cfg.xml to Client ADD ${hibernate_path}/hibernate.cfg.xml /usr/share/tomcat7/webapps/roc_client/WEB-INF/classes/ 

我需要一个命令行参数在$ hibernate_path中指定的docker build期间传递。

我该怎么做呢?

如果这纯粹是构build时variables,那么可以使用--build-arg option of docker build--build-arg option of docker build

该标志允许您在Dockerfile的RUN指令中传递像常规环境variables一样访问的构build时variables。 而且,这些值不会像ENV值一样保留在中间或最终图像中。

 docker build --build-arg hibernate_path=/a/path/to/hibernate -t tag . 

在1.7中,只有静态ENV Dockerfile指令可用。
所以一个解决scheme是从模板Dockerfile.tpl生成所需的Dockerfile。

 Dockerfile.tpl: ... ENV hibernate_path=xxx ADD xxx/hibernate.cfg.xml /usr/share/tomcat7/webapps/roc_client/WEB-INF/classes/ ... 

每当你想build立图像,你首先生成的Dockerfile:

 sed "s,xxx,${hibernate_path},g" Dockerfile.tpl > Dockerfile 

然后你通常build立: docker build -t myimage .

然后,您将受益于(在docker 1.7中):

  • build立时间的环境替代
  • 运行时环境variables。

你创build一个脚本,将Dockerfile放入所需的值,并启动docker build -t mytag .