6

How does one pass arguments into a dockerfile?

Lets say I have the dockerfile:

FROM    ubuntu:14.04

MAINTAINER Karl Morrison

sudo do-something-here myVarHere

I would want to build the file as so for example:

docker build basickarl/my-image-example /directory/of/my/dockerfile "my string to be passed to myVarHere here!"

2 Answers 2

7

Docker has ARG that you can use here

FROM    ubuntu:14.04

MAINTAINER Karl Morrison

ARG myVarHere

RUN do-something-here myVarHere

And then build using --build-arg:

docker build --build-arg myVarHere=value
Sign up to request clarification or add additional context in comments.

2 Comments

For those having trouble with build args, the above is largely correct, however the args are accessed as environment variables in the RUN commands. You can see the documentation on this here and here. The RUN command above would then be: RUN do-something-here $myVarHere
should the build file line "RUN do-something-here myVarHere" actually be "RUN do-something-here ${myVarHere}" ?
2

We've had a similar requirement and came up with a relatively simple script that does exactly that:

We create a file called dockerfile_template in which we use variables just like you describe. The script takes that file, performs string substitutions and copies it to dockerfile (no _template) before calling docker build dockerfile.

Works pretty good. Also very extensible for future requirements.

Update:

Scrap that. Use build-arg (here)

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.