1

lets start by example. I have a simple Dockerfile:

ARG arg1
ARG arg2
RUN echo "$arg1 $arg2"

And what i expect when i call the command

docker build --build-arg arg1=abc --build-arg arg2=${arg1} .

Is that i got the abc abc as the outbut, but i got abc. So is the result that i want possible? And how can I achieve it?

2
  • Does arg2 have to be set at build time, or can it be set in the Dockerfile? Commented Jan 23, 2018 at 10:14
  • Unfortunately it has to be set at build time. Most times it will be using default value, but it should be modifiable and then it should allow to do something like arg2=somedata${anotherArgument}somedata Commented Jan 23, 2018 at 10:19

1 Answer 1

1

Do this in your Dockerfile

FROM alpine
ARG arg1
ARG arg2
RUN 

echo "$arg1 ${arg2:-$arg1}"

You can now call it like this:

docker build --build-arg arg1=abc --build-arg arg2= .

output:

...truncated...
Step 4/4 : RUN echo "$arg1 ${arg2:-$arg1}"
 ---> Running in e65458b9ba6e
abc abc

or like this:

docker build --build-arg arg1=abc --build-arg arg2=override .

output:

...truncated...
Step 4/4 : RUN echo "$arg1 ${arg2:-$arg1}"
 ---> Running in e65458b9ba6e
abc override

(old answer)

Based on the little info you've provided. I would say:

arg1=abc; docker build --build-arg arg1=$arg1 --build-arg arg2=$arg1

should give you what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

It's good point, but in my case it will not work. I'll try to give more details. In most cases there will be used default arg1 and arg2 values. But user should be allowed to modify these parameters and user can do it from the GUI, but these arguments are always passed by the --build-arg parameter even if parameter is unchanged. So there can be a case when user change only arg1 but not arg2 and in that case arg2 should use value from arg1. This is quite generic and from bash i only call the script, i cant set the environment variables :(
@JanNowak i updated my answer to fit your new parameters. I don't really suggest it since its a little buried, i don't know about your team, but mine is very scared when they see shell not to mention when they see something like this: ${arg2:-$arg1}

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.