skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

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

  2. 1. Why docker-compose –build-arg not work for yaml file?

    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.

    2. …how I can override variable from .bat?

    The context here are environment variables that are used on compose yaml file or in the container. Environment variables can be passed directly from host environment variables or environment variables command option definitions (--env-file or --env).

    Your .bat set command creates temporary environment variables that works as host environment variables, that can be overridden by docker compose command options (--env-file or --env).

    A good approach is to create different environment files and use them
    accordingly through --env-file command option.


    References:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search