10

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

Olivier
  • 1,982
  • 3
  • 23
  • 36

5 Answers5

26

The yaml parser in docker stack deploy doesn't have all the same features of that in docker-compose. However, you can use docker-compose config to output a yaml file after it's done all the variable substitutions, extending other files, and merging multiple files together. This effectively turns docker-compose into a preprocessor.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Hi @bmitch. I know `docker stack deploy` does not have all features that `docker-compose` has. That what I wrote below in my own answer, with a link to the relative issue in GitHub. By the way, I didn't know we can use `docker-compose` as a preprocessor. Thanks for the tip. – Olivier Jun 22 '17 at 12:05
  • 4
    Thanks! I use a script deploy.sh that is simple and it just works: `#!/usr/bin/env bash docker-compose config > docker-compose-parsed.yaml docker stack deploy -c ./docker-compose-parsed.yaml MY_SERVICE ` – BangTheBank Apr 27 '18 at 12:37
  • 4
    This should be the accepted answer -- use this in conjunction with `docker stack deploy -c -` which reads from stdin, so you have `docker-compose config | docker stack deploy -c -` – wilco Nov 09 '18 at 00:45
8

The answer is quite simple: it's not a bug, nor a feature. .env is not currently supported by docker stack. You must source manually the .env running export $(cat .env) before running docker stack ...

There is an issue discussing this needs in the Docker Github. https://github.com/docker/docker.github.io/issues/3654 and another one discussing the problem and solution: https://github.com/moby/moby/issues/29133#issuecomment-285980447

Olivier
  • 1,982
  • 3
  • 23
  • 36
5

you can create a deploy.sh

export $(cat .env) > /dev/null 2>&1; docker stack deploy ${1:-STACK_NAME}
  • The .env parses without regular expressions or unstable tricks.
  • Errors on stderr produced by #comments inside the .env will be redirected to stdin (2>&1)
  • Undesired prints of all export and error now on stdin too, are redirected to /dev/null. This prevents console flood.
  • Those errors do not prevent .env being parsed correctly.

We can define STACK_NAME in our .env but we can also pass our customized stack_name

. deploy.sh <stack_name> (stack_name opcional)

This workaround gave me headaches for 3 nights

mrcbns
  • 73
  • 1
  • 6
4

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
xxxception
  • 915
  • 10
  • 6
  • Does it support variable substitution in the `.env` file, e.g. `PGDATABASE=${DATABASE_HOST}` when used like this? – jeverling May 11 '19 at 11:06
  • 4
    I just tried it out with a small example and the answer is no :( – Dirk May 24 '19 at 14:35
  • @RogerCollins, what is docker version do you have? What is a version of a docker-compose file? How looks like your .env file? Please, provide more details – xxxception Aug 07 '19 at 20:22
  • @xxxception docker versions >18.09 - `docker stack` does not support env_file. See the issue: https://github.com/moby/moby/issues/29133 – Roger Collins Aug 09 '19 at 16:44
  • 1
    You are confusing .env (used in the docker-compose template itself) and env_file (passed into the containers) – dnk8n Aug 27 '19 at 15:42
0

I have encountered the same issue, My issue was caused by running docker-compose in a different python virtual environment.

I hope this could help.

It seems that by running on a different python virtual env, I ruined the docker-compose dependency pythondotenv

kerolos
  • 127
  • 1
  • 2
  • 10