cli icon indicating copy to clipboard operation
cli copied to clipboard

Handle quotes in --env-file values consistently with Linux/WSL2 "source"ing

Open markm77 opened this issue 3 years ago • 36 comments

Hi there,

I use a .env file to supply env vars for my application both when running locally and in a Docker container.

When running my application in Linux/WSL2 (Ubuntu), I source the .env file before running.

Example:

set -o allexport
source ./myVars.env
set +o allexport

And when running my application in Docker Desktop on Windows I use the --env-file option to specify the .env file:

docker run --env-file  .\myVars.env <etc>

Unfortunately, some of the env var values in the .env file need to contain spaces. This means they must be double-quoted to work correctly when sourceing in Linux. However when quoted, the parsing in docker run does not remove the quotes making the values invalid as they contain literal quote marks at beginning and end.

The consequence of this is I need to maintain a second (mostly duplicate) .env file for Docker use.

I would much prefer if docker run could parse .env files in the same way as my Linux sourceing does and in line with the general shell concept that quotes are a guide to how a string should be interpreted - not part of the string itself.

From searching, it seems this has been fixed in docker compose (https://github.com/docker/compose/issues/8388). Please can you also fix it in docker run?

markm77 avatar May 21 '22 13:05 markm77

What's the status on this issue and the corresponding PR?

ToshY avatar Jul 12 '22 17:07 ToshY

Swarm and Compose see the same value differently 😨

JWT_SECRET='{"type": "HS256", "key": "xxx"}'

$ docker compose up ... with .env_file

[container] $ echo $JWT_SECRET
{"type": "HS256", "key": "xxx"}

$ docker stack deploy ... with .env_file

[container] $ echo $JWT_SECRET
'{"type": "HS256", "key": "xxx"}'

ivan-kleshnin avatar Sep 02 '22 09:09 ivan-kleshnin

Hopefully this can get fix soon

4nif avatar Jan 18 '23 14:01 4nif

Yes, this stumped me for a while as well. Worked nice locally, but not when dockerized. Even more confusing when compose would've handled the variables differently.

TheodorRene avatar Jul 19 '23 14:07 TheodorRene

Just run into the same problem, either the script works locally OR in the container, but never in both. This is really useless and forces keeping the same content twice. Please fix this soon.

ulbi avatar Aug 12 '23 15:08 ulbi

Let me copy my comment over from https://github.com/docker/cli/issues/4665#issuecomment-1821862464

The value should be unquoted as documented for docker compose (https://docs.docker.com/compose/compose-file/05-services/#env_file)

Unfortunately that format is a breaking change introduced in compose, and I wonder if that change was intentional or because they conflated the format for the .env file and --env-file, which are very different. The --env-file format was designed to be a plain NAME=value, no parsing, and no handling on quotes.

The only processing happening is if a NAME is provided without = / =value, the value of NAME is set from an $NAME env-var if present in the current environment; https://docs.docker.com/engine/reference/commandline/run/#env

thaJeztah avatar Nov 21 '23 23:11 thaJeztah

@glours do you know if the change in format for --env-file in compose was an intentional change (as it's diverging from docker run --env-file, and a backward-incompatible change)?

thaJeztah avatar Nov 21 '23 23:11 thaJeztah

I've just spent a good while debugging an issue off the back of this, which is colouring my view, but I think this design choice is really confusing —

The --env-file format was designed to be a plain NAME=value, no parsing, and no handling on quotes.

At the moment, my natural inclination is to expect it to behave as per any other .env file parsing in other languages/frameworks.

The docs also make it sound as if -e and --env-file behave comparably (by grouping them together and not calling out the distinction), but the -e does parse quotes correctly (or at least they're parsed by the OS before assigning)

Personally, I'd much prefer if --env-file did unwrap quotes from the values, but if not then the 'principle of least surprise' would be to call it something else at least IMO.

0xGuybrush avatar Dec 12 '23 16:12 0xGuybrush

Any updates on this issue ?

Elio-FairlyMade avatar Jan 17 '24 16:01 Elio-FairlyMade

I just ran into this bug today, and it feels like this command should be removed or corrected. It doesn't make sense as is. Especially not according to the documentation

siriele avatar Feb 16 '24 01:02 siriele

@thaJeztah @glours

Do we have any update on this?

andersonbrands avatar Mar 19 '24 17:03 andersonbrands

Issue is currently preventing use of the env_file feature with docker stack. Currently placing env file sourcing inside a startup script to avoid issue.

pveierland avatar Mar 27 '24 02:03 pveierland

I just found this annoyed with the fact that --env-file does not handle quotes. Now I can't use the same .env file for both docker compose and regular docker run commands.

Please fix this.

phrfpeixoto avatar Apr 24 '24 23:04 phrfpeixoto

an alternative may be to mount the env file itself, instead of giving it to docker CLI, like this:

docker run -v ./.env:/usr/src/app/.env your:image

of course you need to customize path names to your env inside and outside the container.

pirafrank avatar May 08 '24 15:05 pirafrank

A .env-file in the root of a project can used by several different tools

  • bash / zsh /...
  • any dotenv-parser in any language (python, ...)

you can source a .env-file in bash via

set -a && source .env && set +a

Although not officially formalized, it is quite universally understood that a .env-file needs to have a bash-compatible syntax. Nearly all dotenv-parsers correctly support quoting and variable substitution. EXCEPT docker .... for over a decade.

Fixing the quotes only fixes some symptoms.

woutervh avatar May 08 '24 21:05 woutervh

I second the notion that .env files are typically understood to support, at a minimum, quoted values. I use .env files with four or five different tools. I can use the same .env files for all of those tools. Docker is the odd one out, and it is certainly unexpected. If the existing behavior of --env-file needs to be preserved, can we have a separate option like --dotenv-file which can accept the same sort of .env files that other dotenv-themed tools accept?

mccolljr avatar May 20 '24 19:05 mccolljr