Skip to content

Commit 8e4cedc

Browse files
authored
[Docker] Support rootless docker when using docker/bash.sh (apache#14590)
The `docker/bash.sh` script should omit the `--pid=host` argument and `docker/with_the_same_user` arguments when the host's docker daemon is running in [rootless mode](https://docs.docker.com/engine/security/rootless/). The `with_the_same_user` script is unnecessary in this mode, as rootless docker daemons already use the privileges of the user. The `--pid=host` flag is required when running in CI, and while it may be useful when running tests locally, it is only supported for rootless docker in versions docker versions 22.06 or greater ([requires this commit](moby/moby#41893)).
1 parent 608d357 commit 8e4cedc

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

docker/bash.sh

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,17 @@ DOCKER_ENV+=( --env CI_BUILD_HOME="${REPO_MOUNT_POINT}"
337337
--env CI_IMAGE_NAME="${DOCKER_IMAGE_NAME}"
338338
)
339339

340-
# Remove the container once it finishes running (--rm) and share the
341-
# PID namespace (--pid=host). The process inside does not have pid 1
342-
# and SIGKILL is propagated to the process inside, allowing jenkins to
343-
# kill it if needed.
344-
DOCKER_FLAGS+=( --rm --pid=host)
340+
# Remove the container once it finishes running (--rm).
341+
DOCKER_FLAGS+=(--rm)
342+
343+
# Share the PID namespace (--pid=host). The process inside does not
344+
# have pid 1 and SIGKILL is propagated to the process inside, allowing
345+
# jenkins to kill it if needed. This is only necessary for docker
346+
# daemons running as root.
347+
if [ -z "${DOCKER_IS_ROOTLESS}" ]; then
348+
DOCKER_FLAGS+=(--pid=host)
349+
fi
350+
345351

346352
# Expose services running in container to the host.
347353
if $USE_NET_HOST; then
@@ -460,6 +466,16 @@ if [ -f "${REPO_DIR}/.git" ]; then
460466
fi
461467
fi
462468

469+
# If the docker daemon is running as root, use the TVM-provided
470+
# "with_the_same_user" script to update the PID. When using rootless
471+
# docker, this step is unnecessary.
472+
if [ -z "${DOCKER_IS_ROOTLESS}" ]; then
473+
COMMAND=(
474+
bash --login /docker/with_the_same_user
475+
${COMMAND[@]+"${COMMAND[@]}"}
476+
)
477+
fi
478+
463479
# Print arguments.
464480
echo "REPO_DIR: ${REPO_DIR}"
465481
echo "DOCKER CONTAINER NAME: ${DOCKER_IMAGE_NAME}"
@@ -473,7 +489,6 @@ DOCKER_CMD=(${DOCKER_BINARY} run
473489
${DOCKER_MOUNT[@]+"${DOCKER_MOUNT[@]}"}
474490
${DOCKER_DEVICES[@]+"${DOCKER_DEVICES[@]}"}
475491
"${DOCKER_IMAGE_NAME}"
476-
bash --login /docker/with_the_same_user
477492
${COMMAND[@]+"${COMMAND[@]}"}
478493
)
479494

docker/dev_common.sh

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ INVOCATION_PWD="$(pwd)"
2727

2828
GIT_TOPLEVEL=$(cd $(dirname ${BASH_SOURCE[0]}) && git rev-parse --show-toplevel)
2929

30+
DOCKER_IS_ROOTLESS=$(docker info 2> /dev/null | grep 'Context: \+rootless')
31+
3032

3133
function lookup_image_spec() {
3234
img_spec=$(python3 "${GIT_TOPLEVEL}/ci/jenkins/data.py" "$1")

0 commit comments

Comments
 (0)