Home > OS >  Why docker-compose --build-arg not work for yaml file?
Why docker-compose --build-arg not work for yaml file?

Time:01-11

I have .bat with arguments, which run docker-compose build

...
set "Build=Build"
set "PublishConfig=Debug"
...
docker-compose -f backend.yml --env-file ./.env build --build-arg EMP_PUBLISH_CONFIG=%PublishConfig% --build-arg EMP_BUILD_LOCALLY=%Build%
...

.env file with variables

EMP_PUBLISH_CONFIG=Release
EMP_BUILD_LOCALLY=""

.yml file

version: "3.7"

... 
services:
  customers:
    container_name: emp-customers
    build:
      context: ./../../
      dockerfile: ./ci/local/backend/Dockerfile${EMP_BUILD_LOCALLY}
      target: emp-customers

and few Dockerfiles like "DockerfileBuild" and "Dockerfile"

ARG EMP_PUBLISH_CONFIG=Release

FROM deploy AS emp-customers
COPY Src/Customers/Emp.Customers.WebApi/bin/$EMP_PUBLISH_CONFIG/net6.0/linux-x64/publish /app/Emp.Customers.WebApi

variable EMP_PUBLISH_CONFIG correctly override from .bat, but variable EMP_BUILD_LOCALLY uses only value from .env

Have any idea, why, and how I can override variable from .bat?

Thanks.

CodePudding user response:

Build arguments are only accessible on build time.

Compose yaml files defines runtime execution, so, they do not have access to build arguments. But you can use environment variables, that is what your .env file do.

References:

CodePudding user response:

Thanks. What I need - just set EMP_BUILD_LOCALLY in shell or .bat before run "docker-compose" and no need to use --build-arg for runtime variable:

...
set "Build=Build" -replace to set "EMP_BUILD_LOCALLY=Build" (it's overriding variable in .env file)
set "PublishConfig=Debug"
...

docker-compose -f backend.yml --env-file ./.env build --build-arg EMP_PUBLISH_CONFIG=%PublishConfig%
...
  • Related