Compose: Unable to bind Windows environment variables to Docker Compose Envs

Created on 7 Aug 2017  路  5Comments  路  Source: docker/compose

Per SO post with noluck: https://stackoverflow.com/questions/45527969/unable-to-bind-windows-environment-variables-to-docker-compose-environment

I currently have a compose file for a Windows .Net service, and I want to be able to pass through a user-space environment variable:

Set from powershell:

powershell [Environment]::SetEnvironmentVariable(\"PREVIOUSLY_SET_WINDOWS_ENV_VARIABLE\", $env:computername, \"User\")

(i've also tried "machine" space env variables as well using the above).

To test after running the above:
echo %PREVIOUSLY_SET_WINDOWS_ENV_VARIABLE% from a new command prompt window.

Docker compose file:

    services:
      myservice.commandline:
        image: myservice.commandline:dev
        build:
          args:
            source: ${DOCKER_BUILD_SOURCE}
        volumes:
          - .\myservice.commandline:C:\app
          - ~\msvsmon:C:\msvsmon:ro
        entrypoint: powershell -Command [System.Threading.Thread]::Sleep([System.Threading.Timeout]::Infinite)
        environment:
          - MY_VARIABLE_NAME=${PREVIOUSLY_SET_WINDOWS_ENV_VARIABLE}
        labels:
          - "com.microsoft.visualstudio.targetoperatingsystem=windows"

This does not work - the environment variable shows as blank.

- MY_VARIABLE_NAME=${PREVIOUSLY_SET_WINDOWS_ENV_VARIABLE} doesn't work.

Is it possible to pass a windows environment variable through to a Docker Compose environment variable? This is easily accomplished using the *nix version of compose, so hoping i've just missed something.

Background:

  • ${PREVIOUSLY_SET_WINDOWS_ENV_VARIABLE} doesn't work.
  • $PREVIOUSLY_SET_WINDOWS_ENV_VARIABLE doesn't work.
  • $(PREVIOUSLY_SET_WINDOWS_ENV_VARIABLE) doesn't work.
  • ${%PREVIOUSLY_SET_WINDOWS_ENV_VARIABLE%} doesn't work.
grouwindows-client kinquestion

Most helpful comment

This works for me with no issues.

version: '3.2'
services:
   greg:
      image: microsoft/windowsservercore
      environment: 
        - "ContainerEnvVariable=${HostEnvVariable}"
      networks:
        - nat
networks:
  nat:
    external: true

$env:HostEnvVariable = "hello"

image

image

All 5 comments

Have you tried setting a process-level environment variable instead, like $env:PREVIOUSLY_SET_ENVIRONMENT_VARIABLE = $env:computername ?

using the "env" prefix doesn't fix this.

environment variables don't appear to work in Windows even if set at the system level using the System advanced options menus.

This works for me with no issues.

version: '3.2'
services:
   greg:
      image: microsoft/windowsservercore
      environment: 
        - "ContainerEnvVariable=${HostEnvVariable}"
      networks:
        - nat
networks:
  nat:
    external: true

$env:HostEnvVariable = "hello"

image

image

This worked after upgrading my docker compose files to 3.2 - the default installed by Visual Studio is 2.1

I'm having the same issue, but in my case I cannot migrate my files to 3.2 since I need to use the mem_limit option. My workaround was to mount a script that sets the variables and then run it inside the container on start, but I'd appreciate if there were a better way.

Was this page helpful?
0 / 5 - 0 ratings