Add container environment variables correctly to the health check#33249
Add container environment variables correctly to the health check#33249mlaventure merged 1 commit intomoby:masterfrom boaz0:env_validate
Conversation
opts/env_test.go
Outdated
There was a problem hiding this comment.
This and the one below are not valid environment variable name usually
There was a problem hiding this comment.
Agree but when doing os.Setenv("asd!qwe", "myvalue") it works.
So I am not sure what to do here. In addition, I have no idea whether that name is valid on windows too?
Tried to Google "valid environment variable names" but the only thing I found is that = can't be in the environment variable.
There was a problem hiding this comment.
I think technically any character except = and NUL are allowed in the variable names, but implementations usually restrict themselves to the set of Portable characters as defined by POSIX -> [a-zA-Z_][a-zA-Z0-9_]*
There was a problem hiding this comment.
Nope; we don't validate env var names; IIRC there's no official standard for it, and we did validation in the past, but broke people that used systems that don't comply with those rules.
We leave the validation to the container itself to tell if it's valid or not
There was a problem hiding this comment.
Fair enough, I have yet to use a shell that accept non POSIX names though :)
There was a problem hiding this comment.
I was about to quote the open group page for this 👍
|
OK, now I realized that if the variable isn't set, it should be discarded (DockerSuite.TestRunEnvironmentErase). I am going to change this now. |
|
Failure in |
|
@thaJeztah yes it is. I am working on fixing it 😺 |
|
Did some testing with this. First ran a regular container, no healthcheck, and variations of ENV vars, to check the behavior; $ docker build -t repro-33249 -<<EOF
FROM busybox
ENV IWILLBEUNSET=thiswasmyvalue
EOF
$ FROMENV=hello docker run -dit --name foobar \
-e IDONOTEXIST \
-e IWILLBEUNSET \
-e IAMEMPTY= \
-e FROMENV \
repro-33249
$ docker inspect --format '{{json .Config.Env }}' foobar | jq .
[
"IDONOTEXIST",
"IWILLBEUNSET",
"IAMEMPTY=",
"FROMENV=hello",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
]
$ docker exec foobar env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=211bf084aab2
IAMEMPTY=
FROMENV=hello
HOME=/rootSo;
Basically, in With this patch (adding a healthcheck):$ docker build -t repro-33249 -<<EOF
FROM busybox
ENV IWILLBEUNSET=thiswasmyvalue
HEALTHCHECK --interval=1s --timeout=5s --retries=5 CMD env; exit 0;
EOF
$ FROMENV=hello docker run -dit --name foobar \
-e IDONOTEXIST \
-e IWILLBEUNSET \
-e IAMEMPTY= \
-e FROMENV \
repro-33249
$ docker inspect --format '{{json .Config.Env }}' foobar | jq .
[
"IDONOTEXIST",
"IWILLBEUNSET",
"IAMEMPTY=",
"FROMENV=hello",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
]
$ docker exec foobar env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=2c0f5e0add62
IAMEMPTY=
FROMENV=hello
HOME=/rootAll of the above are the same (as expected). Checking the $ docker inspect --format '{{json .State.Health.Log }}' foobar | jq .
[
{
"Start": "2017-05-19T11:27:41.45071088Z",
"End": "2017-05-19T11:27:41.505086124Z",
"ExitCode": 0,
"Output": "SHLVL=1\nHOME=/root\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\nFROMENV=hello\nIAMEMPTY=\nPWD=/\n"
}
](expanding, and sorting a bit) Interesting; |
|
So, as far as I can see, this works as expected. I wonder if we (separate from this PR) should look into why |
thaJeztah
left a comment
There was a problem hiding this comment.
LGTM, but @mlaventure PTAL if this is all good. Also wondering how a regular run / exec differs from this health check
|
@thaJeztah I have an idea what happened! Thanks for noticing the "HOSTNAME" thing. |
The health check process doesn't have all the environment varialbes in the container or has them set incorrectly. This patch should fix that problem. Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
mlaventure
left a comment
There was a problem hiding this comment.
LGTM.
Thanks for the fix!
fixes/closes #33021
- What I did
Add environment variables and set the environment variables for the health check.
- How I did it
Use
ReplaceOrAppendEnvValues,CreateDaemonEnvironmentandsetupLinkedContainersto collect all of them and to set them correctly.- How to verify it
Run the following test:
$ TESTFLAGS='-check.f TestUnsetEnvVarHealthCheck' hack/make.sh test-integration-cli