Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 988
Priyanka.Patil Avatar asked Jan 07 '23 14:01

Priyanka.Patil


1 Answers

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.
like image 78
VonC Avatar answered Jan 09 '23 03:01

VonC