Skip to content

Commit

Permalink
Allow moby-buildx version to be specified in devcontainer.json (#838)
Browse files Browse the repository at this point in the history
* Allow moby-buildx version to be specified in devcontainer.json

Fixes #837

* Revert changes to README.md

* Address code review feedback

- Specify mobyBuildxVersion in devcontainer-feature.json with pinned and commented default value
- Bump minor version
- Leverage default value in install.sh
- Only set buildx_version_suffix when installing moby

* Handle varying distributions

* Fix typo / remove extra line
  • Loading branch information
sayhiben authored Feb 10, 2024
1 parent aa982d2 commit 91ac774
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/docker-in-docker/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "docker-in-docker",
"version": "2.8.1",
"version": "2.9.0",
"name": "Docker (Docker-in-Docker)",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/docker-in-docker",
"description": "Create child containers *inside* a container, independent from the host's docker instance. Installs Docker extension in the container along with needed CLIs.",
Expand All @@ -20,6 +20,11 @@
"default": true,
"description": "Install OSS Moby build instead of Docker CE"
},
"mobyBuildxVersion": {
"type": "string",
"default": "0.12.0",
"description": "Install a specific version of moby-buildx when using Moby. (2024-02-09: Microsoft's Package Manifest has mismatching filesize and SHA for 0.12.1; default is last known good version)"
},
"dockerDashComposeVersion": {
"type": "string",
"enum": [
Expand Down
34 changes: 28 additions & 6 deletions src/docker-in-docker/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

DOCKER_VERSION="${VERSION:-"latest"}" # The Docker/Moby Engine + CLI should match in version
USE_MOBY="${MOBY:-"true"}"
MOBY_BUILDX_VERSION="${MOBYBUILDXVERSION}"
DOCKER_DASH_COMPOSE_VERSION="${DOCKERDASHCOMPOSEVERSION:-"v1"}" # v1 or v2 or none
AZURE_DNS_AUTO_DETECTION="${AZUREDNSAUTODETECTION:-"true"}"
DOCKER_DEFAULT_ADDRESS_POOL="${DOCKERDEFAULTADDRESSPOOL}"
Expand Down Expand Up @@ -81,7 +82,7 @@ find_version_from_git_tags() {
local repository=$2
local prefix=${3:-"tags/v"}
local separator=${4:-"."}
local last_part_optional=${5:-"false"}
local last_part_optional=${5:-"false"}
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
local escaped_separator=${separator//./\\.}
local last_part
Expand Down Expand Up @@ -198,14 +199,35 @@ else
echo "cli_version_suffix ${cli_version_suffix}"
fi

# Version matching for moby-buildx
if [ "${USE_MOBY}" = "true" ]; then
if [ "${MOBY_BUILDX_VERSION}" = "latest" ]; then
# Empty, meaning grab whatever "latest" is in apt repo
buildx_version_suffix=""
else
buildx_version_dot_escaped="${MOBY_BUILDX_VERSION//./\\.}"
buildx_version_dot_plus_escaped="${buildx_version_dot_escaped//+/\\+}"
buildx_version_regex="^(.+:)?${buildx_version_dot_plus_escaped}([\\.\\+ ~:-]|$)"
set +e
buildx_version_suffix="=$(apt-cache madison moby-buildx | awk -F"|" '{print $2}' | sed -e 's/^[ \t]*//' | grep -E -m 1 "${buildx_version_regex}")"
set -e
if [ -z "${buildx_version_suffix}" ] || [ "${buildx_version_suffix}" = "=" ]; then
err "No full or partial moby-buildx version match found for \"${MOBY_BUILDX_VERSION}\" on OS ${ID} ${VERSION_CODENAME} (${architecture}). Available versions:"
apt-cache madison moby-buildx | awk -F"|" '{print $2}' | grep -oP '^(.+:)?\K.+'
exit 1
fi
echo "buildx_version_suffix ${buildx_version_suffix}"
fi
fi

# Install Docker / Moby CLI if not already installed
if type docker > /dev/null 2>&1 && type dockerd > /dev/null 2>&1; then
echo "Docker / Moby CLI and Engine already installed."
else
if [ "${USE_MOBY}" = "true" ]; then
# Install engine
set +e # Handle error gracefully
apt-get -y install --no-install-recommends moby-cli${cli_version_suffix} moby-buildx moby-engine${engine_version_suffix}
apt-get -y install --no-install-recommends moby-cli${cli_version_suffix} moby-buildx${buildx_version_suffix} moby-engine${engine_version_suffix}
if [ $? -ne 0 ]; then
err "Packages for moby not available in OS ${ID} ${VERSION_CODENAME} (${architecture}). To resolve, either: (1) set feature option '\"moby\": false' , or (2) choose a compatible OS version (eg: 'ubuntu-20.04')."
exit 1
Expand Down Expand Up @@ -399,7 +421,7 @@ dockerd_start="AZURE_DNS_AUTO_DETECTION=${AZURE_DNS_AUTO_DETECTION} DOCKER_DEFAU
retry_cgroup_nesting=`expr $retry_cgroup_nesting + 1`
set -e
done
done
# -- End: dind wrapper script --
Expand Down Expand Up @@ -442,7 +464,7 @@ retry_docker_start_count=0
docker_ok="false"
until [ "${docker_ok}" = "true" ] || [ "${retry_docker_start_count}" -eq "5" ];
do
do
# Start using sudo if not invoked as root
if [ "$(id -u)" -ne 0 ]; then
sudo /bin/sh -c "${dockerd_start}"
Expand All @@ -460,15 +482,15 @@ do
retry_count=`expr $retry_count + 1`
done
if [ "${docker_ok}" != "true" ] && [ "${retry_docker_start_count}" != "4" ]; then
echo "(*) Failed to start docker, retrying..."
set +e
sudo_if pkill dockerd
sudo_if pkill containerd
set -e
fi
retry_docker_start_count=`expr $retry_docker_start_count + 1`
done
Expand Down

0 comments on commit 91ac774

Please sign in to comment.