Daniel Gruszczyk
Daniel Gruszczyk

Reputation: 5622

docker-compose.yml passing arg to build from file contents

I would like to read contents of a file specified by an environment variable and pass it to docker-compose as build arg.
So then in my Dockerfile I can do:

ARG MY_FILE
RUN echo "$MY_FILE" > /my-file

This works perfectly:

docker-compose -f ./docker-compose.yml build --build-arg MY_FILE="$(cat $PATH_TO_MY_FILE)"

However, if I try to do this in docker-compose.yml like so:

    build:
      context: .
      args:
        - MY_FILE="$(cat $PATH_TO_MY_FILE)"

it fails with this error:

ERROR: Invalid interpolation format for "build" option in service "my-service": "MY_FILE="$(cat $PATH_TO_MY_FILE)""

Any idea how do I have to construct this string to have the same effect? I tried $$ etc, but doesn't seem to work...

Thanks for your help :)

Upvotes: 6

Views: 2182

Answers (2)

sendon1982
sendon1982

Reputation: 11314

In docker service 3, you can do that now.

web:
  image: xxxx
  env_file:
    - web-variables.env

If you have specified a Compose file with docker-compose -f FILE, paths in env_file are relative to the directory that file is in.

Upvotes: 1

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

Docker compose doesn't support this, so you have to use a workaround only. Which would either mean pre-processing the compose file or generate the command you ran by reading the yaml and interpolating by generating the command in bash

You can use something like yq and parse the parameters from docker-compose.yml and generate your command. But honestly what you are doing right now is simple and effective.

Upvotes: 1

Related Questions