docker build with --build-arg with multiple arguments

DockerBuildArguments

Docker Problem Overview


According to the documentation, it's possible to define multiple args for the flag --build-arg, but I can't find out how. I tried the following:

docker build -t essearch/ess-elasticsearch:1.7.6 --build-arg number_of_shards=5 number_of_replicas=2 --no-cache .

=> This returns an error.

I also tried:

docker build -t essearch/ess-elasticsearch:1.7.6 --build-arg number_of_shards=5,number_of_replicas=2 --no-cache .

=> This sets one variable, number_of_shards, to the value "5,number_of_replicas=2"

Any idea how I can define multiple arguments?

Docker Solutions


Solution 1 - Docker

Use --build-arg with each argument.

If you are passing two argument then add --build-arg with each argument like:

docker build \
-t essearch/ess-elasticsearch:1.7.6 \
--build-arg number_of_shards=5 \
--build-arg number_of_replicas=2 \
--no-cache .

Solution 2 - Docker

The above answer by pl_rock is correct, the only thing I would add is to expect the ARG inside the Dockerfile if not you won't have access to it. So if you are doing

docker build -t essearch/ess-elasticsearch:1.7.6 --build-arg number_of_shards=5 --build-arg number_of_replicas=2 --no-cache .

Then inside the Dockerfile you should add

ARG number_of_replicas
ARG number_of_shards

I was running into this problem, so I hope I help someone (myself) in the future.

Solution 3 - Docker

If you want to use environment variable during build. Lets say setting username and password.

username= Ubuntu
password= swed24sw

Dockerfile

FROM ubuntu:16.04
ARG SMB_PASS
ARG SMB_USER
# Creates a new User
RUN useradd -ms /bin/bash $SMB_USER
# Enters the password twice.
RUN echo "$SMB_PASS\n$SMB_PASS" | smbpasswd -a $SMB_USER

Terminal Command

docker build --build-arg SMB_PASS=swed24sw --build-arg SMB_USER=Ubuntu . -t IMAGE_TAG

Solution 4 - Docker

It's a shame that we need multiple ARG too, it results in multiple layers and slows down the build because of that, and for anyone also wondering that, currently there is no way to set multiple ARGs per one line.

Solution 5 - Docker

In case you want to pass automatically build arguments from a specific file, you can do it this way :

docker build  $(cat .my-env-file-name | while read line; do out+="--build-arg $line"; done; echo $out; out="") .

Solution 6 - Docker

A way to pass in build arguments from a file using xargs is as follows:

cat .MY_ENV_FILE | xargs printf -- '--build-arg %s\n' | xargs docker build -t MY_TAG .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmilien BrigandView Question on Stackoverflow
Solution 1 - Dockerpl_rockView Answer on Stackoverflow
Solution 2 - DockerJavier PerezView Answer on Stackoverflow
Solution 3 - DockerFakabbir AminView Answer on Stackoverflow
Solution 4 - Dockerv1adkoView Answer on Stackoverflow
Solution 5 - DockerClément FaureView Answer on Stackoverflow
Solution 6 - DockerNgstigatorView Answer on Stackoverflow