Repository navigation
[docker-outside-of-docker] Add alwaysUseSocketProxy option #1761
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
Open
aremily
wants to merge
2
commits into
devcontainers:main
Choose a base branch
from
aremily:docker-outside-of-docker-always-socket-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| # Optional: Import test library | ||
| source dev-container-features-test-lib | ||
|
|
||
| # This script runs in a fresh process, which always gets up-to-date supplementary groups, so it | ||
| # cannot show whether the container's own command (started before the entrypoint finished) can use | ||
| # the socket. Take that command's groups instead and run docker with exactly those. | ||
| INIT_PID="$(pgrep -u "$(id -u)" -f '[C]ontainer started' | head -n 1)" | ||
| INIT_GROUPS="$(awk '/^Groups:/ {$1=""; print}' "/proc/${INIT_PID}/status" | xargs | tr ' ' ',')" | ||
| DOCKER_BIN="$(command -v docker)" | ||
|
|
||
| echo "container command pid: ${INIT_PID}, its groups: ${INIT_GROUPS}" | ||
|
|
||
| check "container-command-found" bash -c "[ -n '${INIT_PID}' ]" | ||
| check "docker-socket-is-proxy" bash -c "[ -S /var/run/docker.sock ] && [ ! -L /var/run/docker.sock ]" | ||
| check "socat-proxy-running" bash -c "pgrep -af 'socat UNIX-LISTEN:/var/run/docker.sock.*UNIX-CONNECT:/var/run/docker-host.sock'" | ||
| check "log-contents-for-proxy" bash -c "grep 'Enabling socket proxy' /tmp/vscr-docker-from-docker.log" | ||
| check "docker-ps" bash -c "docker ps >/dev/null" | ||
| check "docker-ps-with-container-command-groups" bash -c "sudo setpriv --reuid '$(id -u)' --regid '$(id -g)' --groups '${INIT_GROUPS}' env HOME=/tmp '${DOCKER_BIN}' ps >/dev/null" | ||
|
|
||
| # Deterministic check of the proxy selection. Whether the host socket's GID would pick `groupmod` depends | ||
| # on the host (GID 0 or a GID already in the image picks the proxy anyway), so build a fixture instead: | ||
| # a socket owned by an unused, nonzero GID that forwards to the real socket. The host socket is untouched. | ||
| FIXTURE_GID=4242 | ||
| FIXTURE_DIR=/tmp/socket-proxy-fixture | ||
| DOCKER_GID_BEFORE="$(getent group docker | cut -d: -f3)" | ||
|
|
||
| check "fixture-gid-unused" bash -c "! getent group ${FIXTURE_GID}" | ||
|
|
||
| sudo mkdir -p "${FIXTURE_DIR}" | ||
| sudo chmod 777 "${FIXTURE_DIR}" | ||
| sudo socat "UNIX-LISTEN:${FIXTURE_DIR}/host.sock,fork,mode=660" UNIX-CONNECT:/var/run/docker-host.sock >/dev/null 2>&1 & | ||
| for _ in $(seq 1 50); do [ -S "${FIXTURE_DIR}/host.sock" ] && break; sleep 0.1; done | ||
| check "fixture-socket-created" bash -c "[ -S '${FIXTURE_DIR}/host.sock' ]" | ||
| sudo chown "root:${FIXTURE_GID}" "${FIXTURE_DIR}/host.sock" | ||
|
|
||
| # Run a copy of the generated docker-init.sh against the fixture. $1 = name, $2 = value of the | ||
| # option's baked-in condition ("true" as generated, "false" to emulate the default). | ||
| run_init_copy() { | ||
| local dir="${FIXTURE_DIR}/$1" | ||
| mkdir -p "${dir}" | ||
| sed -e "s#/tmp/vscr-docker-from-docker#${dir}/vscr#g" \ | ||
| -e "s#/var/run/docker-host.sock#${FIXTURE_DIR}/host.sock#g" \ | ||
| -e "s#/var/run/docker.sock#${dir}/docker.sock#g" \ | ||
| -e "s#\[ \"true\" != \"true\" \]#[ \"$2\" != \"true\" ]#" \ | ||
| /usr/local/share/docker-init.sh > "${dir}/init.sh" | ||
| chmod +x "${dir}/init.sh" | ||
| "${dir}/init.sh" true | ||
| } | ||
|
|
||
| run_init_copy option-on true | ||
| check "option-on-selects-proxy" bash -c "grep 'Enabling socket proxy' '${FIXTURE_DIR}/option-on/vscr.log'" | ||
| check "option-on-docker-gid-unchanged" bash -c "[ \"\$(getent group docker | cut -d: -f3)\" = '${DOCKER_GID_BEFORE}' ]" | ||
| check "option-on-socket-owned-by-user" bash -c "[ \"\$(stat -c %U '${FIXTURE_DIR}/option-on/docker.sock')\" = \"\$(id -un)\" ]" | ||
| check "option-on-docker-ps-via-fixture-proxy" bash -c "docker -H 'unix://${FIXTURE_DIR}/option-on/docker.sock' ps >/dev/null" | ||
|
|
||
| # Control, run last because it changes the docker group: with the condition off, this fixture takes | ||
| # the groupmod path. If it did not, the checks above would not prove anything. | ||
| check "control-condition-present-in-generated-script" bash -c "grep -qF '[ \"true\" != \"true\" ]' /usr/local/share/docker-init.sh" | ||
| run_init_copy option-off false | ||
| check "control-selects-groupmod" bash -c "[ \"\$(getent group docker | cut -d: -f3)\" = '${FIXTURE_GID}' ]" | ||
| check "control-does-not-start-proxy" bash -c "! grep 'Enabling socket proxy' '${FIXTURE_DIR}/option-off/vscr.log'" | ||
|
|
||
| # Report result | ||
| reportResults |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, thanks. Fixed in cc68071.
socket_proxy_alwaysnow builds a fixture instead of depending on the host socket's GID: a socket owned byan unused, nonzero GID (4242) that forwards to the real socket. The host socket is not touched. The script
runs a path-rewritten copy of the generated
docker-init.shagainst it. Afixture-socket-createdcheckmakes the test fail with a clear message if the fixture socket does not appear.
dockergroup's GID is unchanged,the proxy socket is owned by the user, and
docker psworks through it.groupmodpath (the group's GID becomes4242) and the proxy does not start. This shows the fixture really would select
groupmod.I also checked that the scenario fails when the option's condition is removed from
install.sh, includingwith the older checks disabled, so the new checks catch it on their own.
One caveat: the test matches the literal
[ "true" != "true" ]in the generated script to flip thecondition. If that line changes (for example #1749 rewrites the
if), thecontrol-condition-present-in-generated-scriptcheck fails instead of passing silently, and the test needsupdating.