user4442960
user4442960

Reputation:

Equivalent of --env-file for build-arg?

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?

Upvotes: 12

Views: 8579

Answers (4)

Excalibur
Excalibur

Reputation: 3367

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.

Upvotes: 1

mantonovic
mantonovic

Reputation: 55

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" .

Upvotes: 0

shizhz
shizhz

Reputation: 12501

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 .
    

Upvotes: 12

Ricardo Branco
Ricardo Branco

Reputation: 6079

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.

Upvotes: 0

Related Questions