Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

418
Views
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 :)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!