Equivalent of --env-file for build-arg?
Asked Answered
M

4

9

I'm building a Docker image using multiple build args, and was wondering if it was possible to pass them to docker build as a file, in the same way --env-file can be pased to docker run. The env file will be parsed by docker run automatically and the variables made available in the container.

Is it possible to specify a file of build arguments in the same way?

Mutz answered 18/4, 2017 at 13:43 Comment(0)
W
11

There's no such an option, at least for now. But if you have too many build args and want to save it in a file, you can archive it as follows:

  1. Save the following shell to buildargs.sh, make it executable and put it in your PATH:

    #!/bin/bash
    awk '{ sub ("\\\\$", " "); printf " --build-arg %s", $0  } END { print ""  }' $@
    
  2. Build your image with argfile like:

    docker build $(buildargs.sh argfile) -t your_image .
    
Wasting answered 18/4, 2017 at 14:58 Comment(1)
This seems to return a "docker build" requires exactly 1 argument: ` docker build $(./buildargs.sh test_args) -t test_build -f Dockerfile .` Where my test_args is just --build-arg PORT=4000 EDIT: nevermind they must be separate by line and without the --build-arg: ``` PORT=4000 STUFF=HELLO FOO=BAR ```Creese
B
1

This also works for us: docker build $(sed 's/^/--build-arg /' .env) -t image:tag .

Note this doesn't support comments in .env, but is a simple approach.

Brinkley answered 6/9 at 22:25 Comment(0)
S
0

This code is safe for build-arg's that contain spaces and special characters:

for arg in buildarg1 buildarg2 ; do opts+=(--build-arg "$arg") ; done

...

docker run ... "${opts[@]}"

Just substitute buildarg1 and so on with your build-arg's escaped.

Sicilia answered 18/4, 2017 at 15:9 Comment(0)
R
0

Using linux you can create a file (example: arg_file) with the variables declared:

ARG_VAL_1=Hello
ARG_VAL_2=World

Execute the source command on that file:

source arg_file

Then build a docker image using that variables run this command:

docker build \
  --build-arg "ARG_VAL_1=$ARG_VAL_1" \
  --build-arg "ARG_VAL_2=$ARG_VAL_2" .
Radome answered 26/1, 2022 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.