-
Notifications
You must be signed in to change notification settings - Fork 234
Description
When S6_KEEP_ENV is disabled, empty but defined environment variables are not preserved when executing commands via with-contenv.
Steps
Using the following docker-compose.yaml:
# compose.yaml
x-service-def:
&service-def
build:
context: .
dockerfile_inline: |
# syntax=docker/dockerfile:1.17.0
FROM alpine
ADD https://github.com/just-containers/s6-overlay/releases/download/v3.2.1.0/s6-overlay-noarch.tar.xz /tmp
RUN ls -al /tmp
RUN tar -v -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz
ADD https://github.com/just-containers/s6-overlay/releases/download/v3.2.1.0/s6-overlay-x86_64.tar.xz /tmp
RUN tar -v -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz
ENTRYPOINT ["/init"]
platform: linux/amd64
command:
- /command/with-contenv
- printenv
# Set of environment variables
x-env-def:
&environments
S6_VERBOSITY: 0
NOT_EMPTY_VARIABLE: 'not-empty'
EMPTY_BUT_DEFINED_VARIABLE: ''
MULTI_LINE: |-
first line
second line
services:
with_keep_env_disabled:
<<: *service-def
environment:
<<: *environments
with_keep_env_enabled:
<<: *service-def
environment:
<<: *environments
S6_KEEP_ENV: 1- Run
docker compose run --rm with_keep_env_disabled. - Observe lack of env
EMPTY_BUT_DEFINED_VARIABLEin output
E.g.
NOT_EMPTY_VARIABLE=not-empty
MULTI_LINE=first line
second line
HOME=/root
S6_VERBOSITY=0
SHLVL=2
PATH=/command:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TERM=xterm
HOSTNAME=8869c1e90a28
PWD=/
- Execute
docker compose run --rm with_keep_env_enabled - Observe presence of env
EMPTY_BUT_DEFINED_VARIABLE
HOSTNAME=419b37a708f4
SHLVL=3
HOME=/root
OLDPWD=/
NOT_EMPTY_VARIABLE=not-empty
S6_VERBOSITY=0
TERM=xterm
EMPTY_BUT_DEFINED_VARIABLE=
PATH=/command:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MULTI_LINE=first line
second line
PWD=/
S6_KEEP_ENV=1
The expectation would be to have same behavior with and without S6_KEEP_ENV
If you've identified a fix, please open a pull request.
Maybe I identified the cause of the issue. I can open a pull request with some minimal guidance on the best resolution possible
From my analysis the issue relies on the fact that env dir /run/s6/container_environment is created using s6-linux-init-maker -s /run/s6/container_environment when S6_KEEP_ENV is disabled ref at stage0.
Doing so s6-linux-init is instructed to create an env dir which should be (quoting doc) compatible with s6-envdir -fn (see https://skarnet.org/software/s6-linux-init/s6-linux-init-maker.html).
- Envdir
env_dumpdocumentation function https://skarnet.org/software/skalibs/libstddjb/env.html - Place where s6-linux-init env_dump is performed: https://github.com/skarnet/s6-linux-init/blob/main/src/init/s6-linux-init.c#L283
- env_dump implementation https://github.com/skarnet/skalibs/blob/main/src/libenvexec/env_dump.c
with-contenv loads the env dir s6-envdir -Lfn (https://github.com/just-containers/s6-overlay/blob/master/layout/rootfs-overlay/package/admin/s6-overlay-%40VERSION%40/command/with-contenv#L10C3-L10C17), so whole file, no chomp
Possible resolutions I can think of:
- Modify env_dump behavior to match
s6-dumpenv -Nand update with-contenv to uses6-envdir -Lf - Avoid relying on s6-linux-init-maker for envdir creation when S6_KEEP_ENV=0. Instead, use:
s6-dumpenv -Nand updatewith-contenvto uses6-envdir -Lf.