Skip to content

Commit

Permalink
Fix docker not available when Jenkins calls it too early (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs authored Dec 24, 2024
1 parent b0b522a commit c5641cc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 5 deletions.
7 changes: 7 additions & 0 deletions devcontainer/rootfs/after_initialization.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -eu

touch /tmp/container_initialized

exec -- "$@"
2 changes: 2 additions & 0 deletions devcontainer/rootfs/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ for file in /entrypoint.d/*; do
source "${file}"
done

set -- /after_initialization.sh "$@"

uid="$(id -u)"
if [[ "${uid}" -eq 0 ]]; then
# If running as root, simply execute s6-overlay
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash

export SKIP_CONTAINER_INITIALIZATION_CHECK="true"
exec docker version &>/dev/null
5 changes: 3 additions & 2 deletions devcontainer/rootfs/etc/s6-overlay/s6-rc.d/dind/condition.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ set -eu
# mode and therefore we should not start the dind service
if mountpoint --quiet "/var/run/docker.sock"; then
# in docker on docker mode, use docker-on-docker-shim by default
docker_path=$(command -v docker)
mv -f "${docker_path}" "${docker_path}.orig"
# the real docker is docker.orig as dokcer is our shim that waits for the container initialization
docker_path=$(command -v docker.orig)
mv -f "${docker_path}" "${docker_path}2"
dond_path=$(command -v dond)
mv -f "${dond_path}" "${docker_path}"

Expand Down
51 changes: 51 additions & 0 deletions devcontainer/rootfs/usr/bin/docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash
#
# Works around situations where the docker cli is called before the container
# is fully initialized. For example, Jenkins does not wait for the entrypoint
# script to finish before proceding with the build.

set -euo pipefail

function log_color() {
color_code="$1"
shift

printf "\033[${color_code}m%s\033[0m\n" "$*" >&2
}

function log_red() {
log_color "0;31" "$@"
}

function log_blue() {
log_color "0;34" "$@"
}

function log_task() {
log_blue "🔃" "$@"
}

function log_error() {
log_red "" "$@"
}

function error() {
log_error "$@"
exit 1
}

if [[ "${SKIP_CONTAINER_INITIALIZATION_CHECK:-"false"}" == "false" ]]; then
# Wait for 10s until the /tmp/container_initialized file is created
for attempt in {1..10}; do
if [[ -f /tmp/container_initialized ]]; then
break
elif [[ "${attempt}" -eq 5 ]]; then
log_task "Waiting more 5s for the /tmp/container_initialized file to be created"
elif [[ "${attempt}" -eq 10 ]]; then
error "The /tmp/container_initialized file was not created after 10s"
fi
sleep 1
done
fi

exec -- docker.orig "$@"
4 changes: 4 additions & 0 deletions devcontainer/scripts/prepare_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ ${CURL} "https://github.com/just-containers/s6-overlay/releases/download/v${S6_O
# fix sshd not starting
mkdir -p /run/sshd

# prepare for the docker shim that waits for the container initialization
docker_path=$(command -v docker)
mv -f "${docker_path}" "${docker_path}.orig"

# install docker-on-docker-shim
# renovate: datasource=github-releases depName=felipecrs/docker-on-docker-shim
DOND_SHIM_VERSION="0.8.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ pipeline {
}
}
steps {
sh 'docker version'
sh 'printenv | sort | tee /dev/stderr | grep -q ^USER='
retry(3) {
sh 'docker version'
}
sh 'IGNORE_FAILURE=false /ssh-command/get.sh'
}
}
Expand Down

0 comments on commit c5641cc

Please sign in to comment.