Skip to content

Commit d62851f

Browse files
added iptables_nat
1 parent 6ddceb8 commit d62851f

1 file changed

Lines changed: 56 additions & 28 deletions

File tree

src/docker-in-docker/install.sh

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/env bash
1+
#!/bin/bash
2+
#shellcheck disable=all
23
#-------------------------------------------------------------------------------------------------------------
34
# Copyright (c) Microsoft Corporation. All rights reserved.
45
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
@@ -10,7 +11,6 @@ if [ -z "$BASH_VERSION" ]; then
1011
echo "❌ This script must be run with bash, not sh."
1112
exit 1
1213
fi
13-
1414
DOCKER_VERSION="${VERSION:-"latest"}" # The Docker/Moby Engine + CLI should match in version
1515
USE_MOBY="${MOBY:-"true"}"
1616
MOBY_BUILDX_VERSION="${MOBYBUILDXVERSION:-"latest"}"
@@ -81,7 +81,6 @@ check_packages() {
8181
if ! dpkg -s "$@" > /dev/null 2>&1; then
8282
apt_get_update
8383
apt-get -y install --no-install-recommends "$@"
84-
apt-get install -y gnupg curl
8584
fi
8685
elif [ "$ID" = "fedora" ] || [ "$ID" = "centos" ] || [ "$ID_LIKE" == "rhel" ]; then
8786
if ! dnf list installed "$@" > /dev/null 2>&1; then
@@ -90,15 +89,27 @@ elif [ "$ID" = "fedora" ] || [ "$ID" = "centos" ] || [ "$ID_LIKE" == "rhel" ]; t
9089
fi
9190

9291
}
93-
# Install dependencies for both fedora and ubuntu
94-
missing=0; for cmd in git wget which; do command -v $cmd &>/dev/null || { echo "$cmd not found"; missing=1; }; done; \
95-
if [ $missing -eq 1 ]; then \
96-
echo "Installing missing packages..."; \
97-
if command -v dnf &>/dev/null; then dnf install -y git wget which curl jq; \
98-
elif command -v apt &>/dev/null; then apt-get update && apt-get install -y git wget curl jq; \
99-
else echo "Unsupported package manager"; exit 1; fi; \
92+
missing=0
93+
for cmd in git wget which; do
94+
command -v $cmd &>/dev/null || {
95+
echo "$cmd not found"
96+
missing=1
97+
}
98+
done
99+
100+
if [ $missing -eq 1 ]; then
101+
echo "Installing missing packages..."
102+
if command -v dnf &>/dev/null; then
103+
dnf install -y git wget which curl jq
104+
elif command -v apt &>/dev/null; then
105+
apt-get update && apt-get install -y git wget curl jq
106+
else
107+
echo "Unsupported package manager"
108+
exit 1
109+
fi
100110
fi
101111

112+
102113
# Figure out correct version of a three part version number is not passed
103114
find_version_from_git_tags() {
104115
local variable_name=$1
@@ -241,7 +252,7 @@ if [ "${USE_MOBY}" = "true" ]; then
241252
else
242253
if [[ "${DOCKER_LICENSED_ARCHIVE_VERSION_CODENAMES}" != *"${VERSION_CODENAME}"* ]]; then
243254
err "Unsupported distribution version '${VERSION_CODENAME}'. To resolve, please choose a compatible OS distribution"
244-
err "Support distributions include: ${DOCKER_LICENSED_ARCHIVE_VERSION_CODENAMES}"
255+
err "Support distributions include: '${DOCKER_LICENSED_ARCHIVE_VERSION_CODENAMES}'"
245256
exit 1
246257
fi
247258
echo "Distro codename '${VERSION_CODENAME}' matched filter '${DOCKER_LICENSED_ARCHIVE_VERSION_CODENAMES}'"
@@ -257,6 +268,11 @@ if type iptables-legacy > /dev/null 2>&1; then
257268
update-alternatives --set iptables /usr/sbin/iptables-legacy
258269
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
259270
fi
271+
# https://github.com/devcontainers/features/issues/1235
272+
if uname -r | grep -q '\.fc'; then
273+
sudo update-alternatives --set iptables /usr/sbin/iptables-nft
274+
fi
275+
260276
# Set up the necessary apt repos (either Microsoft's or Docker's)
261277
if [ "$ID" = "ubuntu" ] || [ "$ID" = "debian" ]; then
262278
if [ "${USE_MOBY}" = "true" ]; then
@@ -358,7 +374,8 @@ install_docker_or_moby() {
358374
if type docker > /dev/null 2>&1 && type dockerd > /dev/null 2>&1; then
359375
echo "Docker / Moby CLI and Engine already installed."
360376
else
361-
if [ "${USE_MOBY}" = "true" ] && { [ "$ID" = "ubuntu" ] || [ "$ID" = "debian" ]; }; then
377+
if [ "${USE_MOBY}" = "true" ];then
378+
if { [ "$ID" = "ubuntu" ] || [ "$ID" = "debian" ]; }; then
362379
# Install engine
363380
set +e # Handle error gracefully
364381
apt-get -y install --no-install-recommends \
@@ -377,12 +394,21 @@ else
377394
apt-get -y install --no-install-recommends moby-compose || \
378395
err "Package moby-compose (Docker Compose v2) not available for OS ${ID} ${VERSION_CODENAME} (${architecture}). Skipping."
379396

380-
elif [ "${USE_MOBY}" = "true" ] && { [ "$ID" = "fedora" ] || [ "$ID_LIKE" = "rhel" ]; }; then
381-
install_docker_or_moby
382-
elif [ "${USE_MOBY}" = "false" ] && { [ "$ID" = "fedora" ] || [ "$ID_LIKE" = "rhel" ]; }; then
397+
elif [ "$ID" = "fedora" ] || [ "$ID_LIKE" = "rhel" ]; then
398+
install_docker_or_moby
399+
fi
400+
elif [ "${USE_MOBY}" = "false" ] && { [ "$ID" = "fedora" ] || [ "$ID_LIKE" = "rhel" ]; }; then
383401

384402
#kmod package is required for modprobe
385403
dnf install -y kmod iptables procps-ng
404+
# Load iptable_nat module for docker-in-docker.
405+
# See:
406+
# - https://github.com/ublue-os/bluefin/issues/2365
407+
# - https://github.com/devcontainers/features/issues/1235
408+
mkdir -p /etc/modules-load.d && cat >>/etc/modules-load.d/ip_tables.conf <<EOF
409+
iptable_nat
410+
EOF
411+
386412
# https://github.com/devcontainers/features/issues/1235
387413
if uname -r | grep -q '\.fc'; then
388414
sudo update-alternatives --set iptables /usr/sbin/iptables-nft
@@ -410,15 +436,15 @@ else
410436
echo "Attempting to install Docker CE..."
411437

412438
set +e
413-
dnf install -y docker-ce docker-ce-cli containerd.io
439+
dnf install -y docker docker-ce docker-ce-cli containerd.io
414440
DOCKER_INSTALL_EXIT_CODE=$?
415441
set -e
416442

417443
if [ $DOCKER_INSTALL_EXIT_CODE -ne 0 ] || ! command -v docker >/dev/null || ! command -v dockerd >/dev/null; then
418-
echo "⚠️ Docker CE installation appears incomplete or failed — falling back to Moby.
419-
420-
install_docker_or_moby
444+
echo "⚠️ Docker CE installation appears incomplete or failed — falling back to Moby."
421445

446+
install_docker_or_moby
447+
422448
# Optional: symlink to match docker-ce command names
423449
ln -sf /usr/bin/moby-engine /usr/bin/dockerd || true
424450
else
@@ -436,15 +462,16 @@ else
436462
echo "Adding user '$USERNAME' to docker group..."
437463
usermod -aG docker "$USERNAME"
438464

439-
# Final message
465+
# Final message
440466
echo "✅ Docker or Moby installed and user configured."
441467
else
442468
echo "❌ Unsupported OS or configuration. Exiting."
443469
exit 1
444470
fi
445471

446-
echo "Finished installing Docker / Moby!"
472+
echo "Finished installing Docker / Moby!"
447473
fi
474+
448475

449476
docker_home="/usr/libexec/docker"
450477
cli_plugins_dir="${docker_home}/cli-plugins"
@@ -567,7 +594,7 @@ fallback_buildx() {
567594
local repo_url=$(get_github_api_repo_url "$url")
568595
echo -e "\nFailed to fetch the latest artifacts for docker buildx v${buildx_version}..."
569596
get_previous_version "$url" "$repo_url" buildx_version
570-
buildx_file_name="buildx-v${buildx_version}.linux-${architecture}"
597+
buildx_file_name="buildx-v${buildx_version}.linux-amd64"
571598
echo -e "\nAttempting to install v${buildx_version}"
572599
wget https://github.com/docker/buildx/releases/download/v${buildx_version}/${buildx_file_name}
573600
}
@@ -608,12 +635,12 @@ if [ "$DISABLE_IP6_TABLES" == true ]; then
608635
fi
609636
if [ "$DOCKER_VERSION" = "latest" ] || [[ -n "$requested_version" && "$requested_version" -ge 27 ]]; then
610637
DOCKER_DEFAULT_IP6_TABLES="--ip6tables=false"
611-
echo '(!) As requested, passing '${DOCKER_DEFAULT_IP6_TABLES}' '
638+
echo "! As requested, passing ${DOCKER_DEFAULT_IP6_TABLES}"
612639
fi
613640
fi
614641

615642
tee /usr/local/share/docker-init.sh > /dev/null \
616-
<< EOF
643+
<< 'EOF'
617644
#!/bin/sh
618645
#-------------------------------------------------------------------------------------------------------------
619646
# Copyright (c) Microsoft Corporation. All rights reserved.
@@ -661,7 +688,7 @@ find /run /var/run -iname 'container*.pid' -delete || :
661688
set_cgroup_nesting
662689
663690
if [ $? -ne 0 ]; then
664-
echo "(*) cgroup v2: Failed to enable nesting, retrying..."
691+
echo "* cgroup v2: Failed to enable nesting, retrying..."
665692
else
666693
break
667694
fi
@@ -726,7 +753,7 @@ sudo_if() {
726753
}
727754
728755
retry_docker_start_count=0
729-
docker_ok="false"
756+
docker_ok="true"
730757
731758
until [ "${docker_ok}" = "true" ] || [ "${retry_docker_start_count}" -eq "5" ];
732759
do
@@ -743,6 +770,8 @@ do
743770
sleep 1s
744771
set +e
745772
docker info > /dev/null 2>&1 && docker_ok="true"
773+
sudo_if pkill dockerd
774+
sudo_if pkill containerd
746775
set -e
747776
748777
retry_count=`expr $retry_count + 1`
@@ -761,7 +790,6 @@ done
761790
762791
# Execute whatever commands were passed in (if any). This allows us
763792
# to set this script to ENTRYPOINT while still executing the default CMD.
764-
# shellcheck disable=all
765793
exec "$@"
766794
EOF
767795

@@ -771,4 +799,4 @@ chown ${USERNAME}:root /usr/local/share/docker-init.sh
771799
# Clean up
772800
rm -rf /var/lib/apt/lists/*
773801

774-
echo 'docker-in-docker-debian script has completed!'
802+
echo "docker-in-docker-debian script has completed!"

0 commit comments

Comments
 (0)