Skip to content

Commit e9d76a9

Browse files
simplified code
1 parent bb9e339 commit e9d76a9

File tree

1 file changed

+94
-122
lines changed

1 file changed

+94
-122
lines changed

src/docker-in-docker/install.sh

Lines changed: 94 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -222,32 +222,23 @@ if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then
222222
ADJUSTED_ID="debian"
223223
PKG_MGR_CMD="apt-get"
224224
# Use dpkg for Debian-based systems
225-
if command -v dpkg >/dev/null 2>&1; then
226-
architecture="$(dpkg --print-architecture)"
227-
else
228-
architecture="$(uname -m)"
229-
fi
225+
architecture="$(dpkg --print-architecture 2>/dev/null || uname -m)"
230226
elif [[ "${ID}" = "rhel" || "${ID}" = "fedora" || "${ID}" = "azurelinux" || "${ID}" = "mariner" || "${ID_LIKE}" = *"rhel"* || "${ID_LIKE}" = *"fedora"* || "${ID_LIKE}" = *"azurelinux"* || "${ID_LIKE}" = *"mariner"* ]]; then
231227
ADJUSTED_ID="rhel"
232228
# Determine the appropriate package manager for RHEL-based systems
233-
if type tdnf > /dev/null 2>&1; then
234-
PKG_MGR_CMD="tdnf"
235-
elif type dnf > /dev/null 2>&1; then
236-
PKG_MGR_CMD="dnf"
237-
elif type microdnf > /dev/null 2>&1; then
238-
PKG_MGR_CMD="microdnf"
239-
elif type yum > /dev/null 2>&1; then
240-
PKG_MGR_CMD="yum"
241-
else
229+
for pkg_mgr in tdnf dnf microdnf yum; do
230+
if command -v "$pkg_mgr" >/dev/null 2>&1; then
231+
PKG_MGR_CMD="$pkg_mgr"
232+
break
233+
fi
234+
done
235+
236+
if [ -z "${PKG_MGR_CMD}" ]; then
242237
err "Unable to find a supported package manager (tdnf, dnf, microdnf, yum)"
243238
exit 1
244239
fi
245-
# Use rpm for RHEL-based systems
246-
if command -v rpm >/dev/null 2>&1; then
247-
architecture="$(rpm --eval '%{_arch}')"
248-
else
249-
architecture="$(uname -m)"
250-
fi
240+
241+
architecture="$(rpm --eval '%{_arch}' 2>/dev/null || uname -m)"
251242
else
252243
err "Linux distro ${ID} not supported."
253244
exit 1
@@ -273,10 +264,10 @@ if [ "${USE_MOBY}" = "true" ]; then
273264
err "Supported distributions include: ${DOCKER_MOBY_ARCHIVE_VERSION_CODENAMES}"
274265
exit 1
275266
fi
276-
echo "(*) ${VERSION_CODENAME} is supported for Moby installation (supported: ${DOCKER_MOBY_ARCHIVE_VERSION_CODENAMES}) - setting up Microsoft repository"
267+
echo "(*) ${VERSION_CODENAME} is supported for Moby installation - setting up Microsoft repository"
277268
elif [ "${ADJUSTED_ID}" = "rhel" ]; then
278269
if [ "${ID}" = "azurelinux" ] || [ "${ID}" = "mariner" ]; then
279-
echo " (*) Azure Linux ${VERSION_ID}/Mariner ${VERSION_ID} detected - using Microsoft repositories for Moby packages"
270+
echo " (*) ${ID} ${VERSION_ID} detected - using Microsoft repositories for Moby packages"
280271
else
281272
echo "RHEL-based system (${ID}) detected - Moby packages may require additional configuration"
282273
fi
@@ -295,22 +286,23 @@ else
295286
fi
296287
fi
297288

298-
# Install dependencies
289+
# Install base dependencies
290+
base_packages="curl ca-certificates pigz iptables gnupg2 wget jq"
299291
case ${ADJUSTED_ID} in
300292
debian)
301-
check_packages apt-transport-https curl ca-certificates pigz iptables gnupg2 dirmngr wget jq
302-
if ! type git > /dev/null 2>&1; then
303-
check_packages git
304-
fi
293+
check_packages apt-transport-https $base_packages dirmngr
305294
;;
306295
rhel)
307-
check_packages curl ca-certificates pigz iptables gnupg2 wget jq tar gawk shadow-utils policycoreutils procps-ng systemd-libs systemd-devel
308-
if ! type git > /dev/null 2>&1; then
309-
check_packages git
310-
fi
296+
check_packages $base_packages tar gawk shadow-utils policycoreutils procps-ng systemd-libs systemd-devel
297+
311298
;;
312299
esac
313300

301+
# Install git if not already present
302+
if ! command -v git >/dev/null 2>&1; then
303+
check_packages git
304+
fi
305+
314306
# Swap to legacy iptables for compatibility (Debian only)
315307
if [ "${ADJUSTED_ID}" = "debian" ] && type iptables-legacy > /dev/null 2>&1; then
316308
update-alternatives --set iptables /usr/sbin/iptables-legacy
@@ -332,41 +324,26 @@ if [ "${USE_MOBY}" = "true" ]; then
332324
} | gpg --dearmor > /usr/share/keyrings/microsoft-archive-keyring.gpg
333325
echo "deb [arch=${architecture} signed-by=/usr/share/keyrings/microsoft-archive-keyring.gpg] https://packages.microsoft.com/repos/microsoft-${ID}-${VERSION_CODENAME}-prod ${VERSION_CODENAME} main" > /etc/apt/sources.list.d/microsoft.list
334326
;;
335-
rhel)
336-
if [ "${ID}" = "azurelinux" ]; then
337-
# Azure Linux - Microsoft doesn't provide separate Moby repositories
338-
# Use built-in repositories or recommend Docker CE
339-
echo "(*) Azure Linux detected"
340-
echo "(*) Microsoft does not provide separate Moby repositories for Azure Linux"
341-
echo "(*) Checking for built-in container packages..."
342-
343-
# Check if moby packages are available in default repos
344-
if ${PKG_MGR_CMD} list available moby-engine >/dev/null 2>&1; then
345-
echo "(*) Using built-in Azure Linux Moby packages"
346-
# Use default Azure Linux repositories - no additional repo needed
347-
else
348-
echo "(*) Moby packages not found in Azure Linux repositories"
349-
echo "(*) For Azure Linux, Docker CE ('moby': false) is recommended"
350-
err "Moby packages are not available for Azure Linux ${VERSION_ID}."
351-
err "Recommendation: Use '\"moby\": false' to install Docker CE instead."
352-
exit 1
353-
fi
354-
elif [ "${ID}" = "mariner" ]; then
355-
# CBL-Mariner - check if moby packages are available first
356-
echo "(*) CBL-Mariner detected"
357-
echo "(*) Checking for built-in container packages..."
358-
359-
# Check if moby packages are available in default repos first
360-
if ${PKG_MGR_CMD} list available moby-engine >/dev/null 2>&1; then
361-
echo "(*) Using built-in CBL-Mariner Moby packages"
362-
# Use default repositories - no additional repo needed
363-
else
364-
echo "(*) Moby packages not found in default repositories"
365-
echo "(*) Adding Microsoft repository for CBL-Mariner..."
366-
367-
# Add Microsoft repository if packages aren't available locally
368-
curl -sSL ${MICROSOFT_GPG_KEYS_URI} | gpg --dearmor > /etc/pki/rpm-gpg/microsoft.gpg
369-
cat > /etc/yum.repos.d/microsoft.repo << EOF
327+
rhel)
328+
echo "(*) ${ID} detected - checking for Moby packages..."
329+
330+
# Check if moby packages are available in default repos
331+
if ${PKG_MGR_CMD} list available moby-engine >/dev/null 2>&1; then
332+
echo "(*) Using built-in ${ID} Moby packages"
333+
else
334+
case "${ID}" in
335+
azurelinux)
336+
echo "(*) Moby packages not found in Azure Linux repositories"
337+
echo "(*) For Azure Linux, Docker CE ('moby': false) is recommended"
338+
err "Moby packages are not available for Azure Linux ${VERSION_ID}."
339+
err "Recommendation: Use '\"moby\": false' to install Docker CE instead."
340+
exit 1
341+
;;
342+
mariner)
343+
echo "(*) Adding Microsoft repository for CBL-Mariner..."
344+
# Add Microsoft repository if packages aren't available locally
345+
curl -sSL ${MICROSOFT_GPG_KEYS_URI} | gpg --dearmor > /etc/pki/rpm-gpg/microsoft.gpg
346+
cat > /etc/yum.repos.d/microsoft.repo << EOF
370347
[microsoft]
371348
name=Microsoft Repository
372349
baseurl=https://packages.microsoft.com/repos/microsoft-cbl-mariner-2.0-prod-base/
@@ -382,12 +359,14 @@ EOF
382359
err "Recommendation: Use '\"moby\": false' to install Docker CE instead."
383360
exit 1
384361
fi
385-
fi
386-
else
362+
;;
363+
*)
387364
err "Moby packages are not available for ${ID}. Please use 'moby': false option."
388365
exit 1
389-
fi
390-
;;
366+
;;
367+
esac
368+
fi
369+
;;
391370
esac
392371
else
393372
# Name of licensed engine/cli
@@ -398,15 +377,11 @@ else
398377
curl -fsSL https://download.docker.com/linux/${ID}/gpg | gpg --dearmor > /usr/share/keyrings/docker-archive-keyring.gpg
399378
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/${ID} ${VERSION_CODENAME} stable" > /etc/apt/sources.list.d/docker.list
400379
;;
401-
rhel)
402-
if [ "${ID}" = "azurelinux" ] || [ "${ID}" = "mariner" ]; then
403-
echo "(*) ${ID} detected"
404-
echo "(*) Note: Moby packages work better on Azure Linux. Consider using 'moby': true"
405-
echo "(*) Setting up Docker CE repository..."
406-
407-
# Create Docker CE repository for Azure Linux
380+
rhel)
381+
# Docker CE repository setup for RHEL-based systems
382+
setup_docker_ce_repo() {
408383
curl -fsSL https://download.docker.com/linux/centos/gpg > /etc/pki/rpm-gpg/docker-ce.gpg
409-
cat > /etc/yum.repos.d/docker-ce.repo << EOF
384+
cat > /etc/yum.repos.d/docker-ce.repo << EOF
410385
[docker-ce-stable]
411386
name=Docker CE Stable
412387
baseurl=https://download.docker.com/linux/centos/9/\$basearch/stable
@@ -416,58 +391,55 @@ gpgkey=file:///etc/pki/rpm-gpg/docker-ce.gpg
416391
skip_if_unavailable=1
417392
module_hotfixes=1
418393
EOF
419-
# Install device-mapper-libs for Docker CE storage management, but skip on Mariner due to repo sync issues and lack of strict requirement
394+
}
395+
install_azure_linux_deps() {
420396
echo "(*) Installing device-mapper libraries for Docker CE..."
421-
if [ "${ID}" != "mariner" ]; then
422-
${PKG_MGR_CMD} -y install device-mapper-libs 2>/dev/null || echo "(*) Device-mapper install failed, proceeding"
423-
fi
424-
425-
# Install other essential libraries for Docker CE
397+
[ "${ID}" != "mariner" ] && ${PKG_MGR_CMD} -y install device-mapper-libs 2>/dev/null || echo "(*) Device-mapper install failed, proceeding"
426398
echo "(*) Installing additional Docker CE dependencies..."
427-
${PKG_MGR_CMD} -y install \
428-
libseccomp \
429-
libtool-ltdl \
430-
systemd-libs \
431-
libcgroup \
432-
tar \
433-
xz || {
399+
${PKG_MGR_CMD} -y install libseccomp libtool-ltdl systemd-libs libcgroup tar xz || {
434400
echo "(*) Some optional dependencies could not be installed, continuing..."
435401
}
436-
437-
# For Azure Linux, install Docker CE without container-selinux complexity
438-
if [ "${USE_MOBY}" != "true" ]; then
439-
echo "(*) Docker CE installation for Azure Linux - skipping container-selinux"
440-
echo "(*) Note: SELinux policies will be minimal but Docker will function normally"
441-
# Create minimal SELinux context for Docker compatibility (if SELinux is enabled)
442-
if command -v getenforce >/dev/null 2>&1 && [ "$(getenforce 2>/dev/null)" != "Disabled" ]; then
443-
echo "(*) Creating minimal SELinux context for Docker compatibility..."
444-
mkdir -p /etc/selinux/targeted/contexts/files/ 2>/dev/null || true
445-
echo "/var/lib/docker(/.*)? system_u:object_r:container_file_t:s0" >> /etc/selinux/targeted/contexts/files/file_contexts.local 2>/dev/null || true
446-
fi
447-
else
448-
echo "(*) Using Moby - container-selinux not required"
402+
}
403+
setup_selinux_context() {
404+
if command -v getenforce >/dev/null 2>&1 && [ "$(getenforce 2>/dev/null)" != "Disabled" ]; then
405+
echo "(*) Creating minimal SELinux context for Docker compatibility..."
406+
mkdir -p /etc/selinux/targeted/contexts/files/ 2>/dev/null || true
407+
echo "/var/lib/docker(/.*)? system_u:object_r:container_file_t:s0" >> /etc/selinux/targeted/contexts/files/file_contexts.local 2>/dev/null || true
449408
fi
450-
else
451-
# Standard RHEL/CentOS/Fedora approach
452-
if command -v dnf >/dev/null 2>&1; then
453-
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
454-
else
455-
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 2>/dev/null || {
409+
}
410+
411+
# Special handling for RHEL Docker CE installation
412+
case "${ID}" in
413+
azurelinux|mariner)
414+
echo "(*) ${ID} detected"
415+
echo "(*) Note: Moby packages work better on Azure Linux. Consider using 'moby': true"
416+
echo "(*) Setting up Docker CE repository..."
417+
418+
setup_docker_ce_repo
419+
install_azure_linux_deps
420+
421+
if [ "${USE_MOBY}" != "true" ]; then
422+
echo "(*) Docker CE installation for Azure Linux - skipping container-selinux"
423+
echo "(*) Note: SELinux policies will be minimal but Docker will function normally"
424+
setup_selinux_context
425+
else
426+
echo "(*) Using Moby - container-selinux not required"
427+
fi
428+
;;
429+
*)
430+
# Standard RHEL/CentOS/Fedora approach
431+
if command -v dnf >/dev/null 2>&1; then
432+
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
433+
elif command -v yum-config-manager >/dev/null 2>&1; then
434+
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
435+
else
456436
# Manual fallback
457-
curl -fsSL https://download.docker.com/linux/centos/gpg > /etc/pki/rpm-gpg/docker-ce.gpg
458-
cat > /etc/yum.repos.d/docker-ce.repo << EOF
459-
[docker-ce-stable]
460-
name=Docker CE Stable
461-
baseurl=https://download.docker.com/linux/centos/9/\$basearch/stable
462-
enabled=1
463-
gpgcheck=1
464-
gpgkey=file:///etc/pki/rpm-gpg/docker-ce.gpg
465-
EOF
466-
}
467-
fi
437+
setup_docker_ce_repo
468438
fi
469439
;;
470-
esac
440+
esac
441+
;;
442+
esac
471443
fi
472444

473445
# Refresh package database

0 commit comments

Comments
 (0)