Skip to content

Empty env variables are not preserved when S6_KEEP_ENV is disabled using with-contenv #627

@ximarx

Description

@ximarx

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
  1. Run docker compose run --rm with_keep_env_disabled.
  2. Observe lack of env EMPTY_BUT_DEFINED_VARIABLE in 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=/
  1. Execute docker compose run --rm with_keep_env_enabled
  2. 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).

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:

  1. Modify env_dump behavior to match s6-dumpenv -N and update with-contenv to use s6-envdir -Lf
  2. Avoid relying on s6-linux-init-maker for envdir creation when S6_KEEP_ENV=0. Instead, use: s6-dumpenv -N and update with-contenv to use s6-envdir -Lf.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions