4

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?

2
  • 1
    Dear negative voter..let me know what's wrong with the question? Commented Feb 22, 2016 at 11:51
  • This will not work. The docker file is only read one time, when you build your image. Commented Feb 22, 2016 at 11:54

2 Answers 2

9

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.
Sign up to request clarification or add additional context in comments.

17 Comments

Tried this. Says : flag provided but not defined: --build-arg See 'docker build --help'.
You must have docker 1.9 or 1.10
Hm..I'm using docker 1.7. Any options?
Yes: upgrade. 1.7 is obsolete.
@Priyanka.Patil ADD is not "runtime". It is build-time. Hence the need of build-arg.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.