Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inherited env var not found in multi-var .env #221

Closed

Conversation

flemay
Copy link

@flemay flemay commented Jan 15, 2022

Note: This PR is a replication of my previous PR in compose-spec/godotenv repository.

The package dotenv implements inherited environment variables which allows variables to be set on the host and passed to a container using key-only pairs in a .env file. Package compose-go uses this feature by calling the function ParseWithLookup of dotenv.

One expectation of this feature is that an environment variable will not be passed to the container if the host does not set it. This scenario is tested in dotenv. However, there is an issue if a .env file contains multiple key pairs resulting of environment variables not being set properly in the container.

This PR fixes this issue.

Demonstration

Define a .env file with the following:

ENV_A
ENV_B=
ENV_C=env_c

And a docker-compose.yml file:

services:
  alpine:
    image: alpine
    env_file: .env

Docker

$ docker run --rm --env-file=.env alpine env
ENV_B=
ENV_C=env_c

Here ENV_A has not been set and ENV_B is set with an empty value.

Docker-Compose (V1)

$ docker-compose run --rm alpine env
ENV_B=
ENV_C=env_c

With Docker-Compose (V1), we get the same outcome as Docker

Compose (v2)

$ docker compose run --rm alpine env
ENV_C=env_c
ENV_A=ENV_B=

Here's where the outcome is quite unexpected: ENV_A is set with the value ENV_B=.

Compose seems to have a problem with key-only items in .env file.

The package `dotenv` implements inherited environment variables which
allows variables to be set on the host and passed to a container using
key-only pairs in a .env file. Package `compose-go` uses this feature by
calling the function `ParseWithLookup` of `dotenv`.

One expectation of this feature is that an environment variable will not
be passed to the container if the host does not set it. This scenario is
tested in `dotenv`. However, there is an issue if a `.env` file contains
multiple key pairs resulting of environment variables not being set
properly in the container.

This commit fixes this issue.

Signed-off-by: Frederic Lemay <461102+flemay@users.noreply.github.com>
@flemay
Copy link
Author

flemay commented Jan 15, 2022

Closing this PR because the PR is more accurate. :)

@flemay flemay closed this Jan 15, 2022
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.

None yet

1 participant