dockerdocker-swarm-modedocker-stack

Docker Swarm with image versions externalized to .env file


I used to externalized my image versions to my .env file. This make it easy to maintain and I don't modify my docker-compose.yml file just to upgrade a version, so I'm sure I won't delete a line by mistake or whatever.

But when I try to deploy my services with stack to the swarm, docker engine complains that my image is not a correct reposity/tag, with the exact following message :

Error response from daemon: rpc error: code = 3 desc = ContainerSpec: "GROUP/IMAGE:" is not a valid repository/tag

To fix this, I can fix the image version directly in the docker-compose.yml file. Is there any logic here or it that a bug? But this mixes fix part of the docker-compose and variable ones.

Cheers, Olivier


Solution

  • As already mentioned the .env isn't currently supported by the docker stack. So the alternative way is to clearly specify the environment file using by env_file, e.g.:

    version: '3.3'
    services:
      foo-service:
        image: foo-image
        env_file:
          - .env
        environment:
          - SOME_ENV=qwerty
          - OTHER_ENV=${SOME_ENV} # Not supported
    

    Update: as per 2024 it still does not support variable substitution neither in env_file nor in environment compose file entry.

    Opposite to other answers here it would not either work for docker compose --file your.compose.yml config > processed.yml followed by docker stack deploy --compose-file processed.yml your-stack because the compose file formats for stack and compose mismatch (name, depends_on keys at least).