Priyanka.Patil
Priyanka.Patil

Reputation: 1197

How to provide and use command line arguments in docker build?

I have a Dockerfile as shown below:

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/

I need a command line argument to be passed during docker build to be specified for the $hibernate_path.

How do I do this?

Upvotes: 4

Views: 8466

Answers (2)

VonC
VonC

Reputation: 1323055

If this is purely a build-time variable, you can use the --build-arg option of docker build.

This flag allows you to pass the build-time variables that are accessed like regular environment variables in the RUN instruction of the Dockerfile. Also, these values don’t persist in the intermediate or final images like ENV values do.

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

In 1.7, only the static ENV Dockerfile directive is available.
So one solution is to generate the Dockerfile you need from a template Dockerfile.tpl.

Dockerfile.tpl:

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

Whenever you want to build the image, you generate first the Dockerfile:

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

Then you build normally: docker build -t myimage .

You then benefit from (in docker 1.7):

  • build-time environment substitution
  • run-time environment variable.

Upvotes: 9

user2915097
user2915097

Reputation: 32156

You create a script that puts in your Dockerfile the required value and launches docker build -t mytag .

Upvotes: 0

Related Questions