Setting an environmental variable in a docker-comp

2019-08-18 22:42发布

问题:

I'm trying to set a specific environmental variable in my docker-compose.yml file. I don't think my docker-compose up is registering/reading it correctly.

  apigateway.web:
    image: traefik
    command: --api --docker # Enables the web UI and tells Traefik to listen to docker
    environment:
      - COMPOSE_CONVERT_WINDOWS_PATHS=1
    ports:
      - "80"   # The HTTP port
      - "8080" # The Web UI (enabled by --api)
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events

I know I could set it in a .env file. Isn't this basically the same thing? Of course, the .env file is like auto adding an environment: section to all of my services in my docker-compose.yml file ... vs ... just setting that variable for 1 service. But besides that, it's the same thing, right? an environmental variable is an environmental variable.

I'm asking this because when I add that single environmental variable to that service I don't believe it's getting 'picked up / read' because this error still occurs:

Recreating 3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1 ... 
Recreating 3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1 ... error
ERROR: for 3ae831a08eab_dockercompose17057127240787984866_apigateway.web_1  Cannot create container for service apigateway.web: b'Mount denied:\nThe source path "\\\\var\\\\run\\\\docker.sock:/var/run/docker.sock"\nis not a valid Windows path'
ERROR: for apigateway.web  Cannot create container for service apigateway.web: b'Mount denied:\nThe source path "\\\\var\\\\run\\\\docker.sock:/var/run/docker.sock"\nis not a valid Windows path'
Encountered errors while bringing up the project..

And this error seems to be talked about in this GitHub issue -> with the answer being: add an environmental variable COMPOSE_CONVERT_WINDOWS_PATHS=1.

Notes:

  • I'm on a windows machine.
  • Docker is running as Linux containers.

So -- am I right or what I'm doing is totally wrong and I actually do need to create a .env file because of .

thanks in advance!

回答1:

I believe you want to set COMPOSE_CONVERT_WINDOWS_PATHS=1 on your host machine so docker-compose knows to convert the windows-style to unix-style volume definitions.

$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "1" docker-compose up