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
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:
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 bydocker 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: