I want to set variables from an in my docker container. Sometimes I start the docker-container with docker-compose, sometimes I debug my solution via Visual Studio using its Container Orchestration support or its Docker support.
So I have an environment variable ENV_VARIABLE_ON_HOST
defined on my Windows host machine with the value "HOST_ENV_VALUE"
, and of course via command line I can pass it to the container via
docker compose up --env ENV_VARIABLE_IN_CONTAINER=$env:ENV_VARIABLE_ON_HOST
When using Visual Studio's Docker support, I can also pass it to the container by defining
<DockerfileRunArguments>--env ENV_VARIABLE_IN_CONTAINER=$(ENV_VARIABLE_ON_HOST)</DockerfileRunArguments>
in the .csproj file.
Weirdly enough this does not seem to work with the <ContainerRunArguments>
which is supposed to replace DockerfileRunArguments
But I'm trying to do the same thing when launching the project with docker-compose container orchestration from visual studio, so I defined the following service.
services:
myapi:
image: ${DOCKER_REGISTRY-}myapi
build:
context: .
dockerfile: MyDockerfile
environment:
- ENV_VARIABLE_IN_CONTAINER=$(ENV_VARIABLE_ON_HOST)
(I also tried some other variations of that)
However, this doesn't work. the container contains the environment variable ENV_VARIABLE_IN_CONTAINER
with the value "$(ENV_VARIABLE_ON_HOST)"
(i.e. the actual variable name as a string, not the value HOST_ENV_VALUE
which I expected).
Does anyone know what I'm doing wrong, or any alternative to passing the host's environment variable to the container using Visual Studio's Container Orchestration Support?
Most answers for passing env variables to docker-compose that I found suggest exporting the env variable in the shell, which is fine when starting docker-compose from the command line, but doesn't help with using Visual Studio.
I found it, I think
environment:
- ENV_VARIABLE_IN_CONTAINER=$ENV_VARIABLE_ON_HOST
seems to have worked.