Skip to content

Strip matching quotes from --env-file values#6805

Open
veeceey wants to merge 1 commit intodocker:masterfrom
veeceey:fix/issue-3630-env-file-quotes
Open

Strip matching quotes from --env-file values#6805
veeceey wants to merge 1 commit intodocker:masterfrom
veeceey:fix/issue-3630-env-file-quotes

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 17, 2026

- What I did

Fixes #3630

I ran into this exact issue at work -- we share a single .env file between our local dev setup (where we source it) and our Docker containers (via --env-file). Any value with spaces in it needs quotes for source to work, but Docker was treating the quotes as literal characters. Drove me crazy for a while before I realized what was happening.

This adds quote stripping for values in --env-file parsing, matching how shells handle sourced files and how docker compose already works (fixed in docker/compose#8388).

- How I did it

Added a trimQuotes() helper in pkg/kvfile/kvfile.go that strips the outermost matching quotes (single or double) from values. Only strips when first and last characters are the same quote type -- mismatched or missing quotes are left as-is.

- How to verify it

Run the unit tests:

go test ./pkg/kvfile/... -v

The new TestTrimQuotes covers all the edge cases (matched/mismatched/nested/empty/single-char) and TestParseFromReaderQuotedValues verifies end-to-end parsing with various quoting styles.

You can also test manually:

echo 'FOO="hello world"' > /tmp/test.env
echo "BAR='single quoted'" >> /tmp/test.env
echo "BAZ=no quotes" >> /tmp/test.env
docker run --env-file /tmp/test.env alpine env

Before this change, FOO would be "hello world" (with literal quotes). After, it's hello world.

- Description for the changelog

docker run --env-file now strips matching surrounding quotes from values, consistent with shell source behavior and docker compose.

When using a .env file with both `source` on Linux and `docker run
--env-file`, quoted values like FOO="bar" would include the literal
quotes in Docker, breaking compatibility. This strips matching outer
quotes (single or double) from values, consistent with how shells
handle sourced env files and matching docker compose behavior.

Signed-off-by: Varun Chawla <varun_6april@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant

Comments