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

Remove leading and trailing double quotes from environment variable values in docker run commands #5434

Closed
wants to merge 1 commit into from

Conversation

peacewalker122
Copy link

@peacewalker122 peacewalker122 commented Sep 13, 2024

- What I did

Implemented functionality to automatically remove leading and trailing double quotes from environment variable values when they are set via the Docker CLI (docker run). This enhancement prevents issues caused by unintended double quotes in environment variable values, ensuring that the variables are set correctly inside containers.

- How I did it

  • Modified the environment variable parsing logic in opts/file.go,opts/parse.go to strip leading and trailing double quotes from environment variable values.
  • Used strings.Trim with " as the cutset to remove only the leading and trailing double quotes without affecting any internal quotes within the value.
  • Ensured that environment variable names are not altered.
  • Added unit tests in cli/command/container/opts_test.go to verify that leading and trailing double quotes are correctly removed.
  • Verified that values without leading/trailing double quotes remain unaffected.

- How to verify it

  1. Build the Docker CLI from the modified source code.

  2. Run a container with an environment variable that includes leading and trailing double quotes in its value:

    ./docker run --env MY_VAR="some value with spaces" alpine sh -c 'echo "[$MY_VAR]"'
  3. Expected output: some value with spaces

@thaJeztah
Copy link
Member

Why are you putting the 'MY_VAR="some value with spaces"' in single quotes? I think this should work it you remove those;

docker run --rm --env MY_VAR="some value with spaces" alpine printenv MY_VAR
some value with spaces

@peacewalker122
Copy link
Author

peacewalker122 commented Sep 14, 2024

Why are you putting the 'MY_VAR="some value with spaces"' in single quotes? I think this should work it you remove those;

docker run --rm --env MY_VAR="some value with spaces" alpine printenv MY_VAR
some value with spaces

Hi @thaJeztah. Yes you're right, it doesn't need to wrapped on that single qoutes. That was typo, sorry about that.

@thaJeztah
Copy link
Member

In that case; is there anything to fix here? Because this seems to be working as expected;

docker create -e MY_VAR="hello world" -e OTHER_VAR=hello-world --name mycontainer alpine

docker inspect --format '{{ json .Config.Env }}' mycontainer
["MY_VAR=hello world","OTHER_VAR=hello-world","PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]

I see your code also changes behavior for --env-file, which would be related to

However, I think that behavior was by design (--env-file does not interpolate values, and takes a pure name=value format)

@peacewalker122
Copy link
Author

In that case; is there anything to fix here? Because this seems to be working as expected;

docker create -e MY_VAR="hello world" -e OTHER_VAR=hello-world --name mycontainer alpine

Yes, you're right it works like expected.

I see your code also changes behavior for --env-file, which would be related to
However, I think that behavior was by design (--env-file does not interpolate values, and takes a pure name=value format)

Yes, this code were firstly intended for that purpose, If so I'll close the PR. Thanks for the review @thaJeztah

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.

2 participants