Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit 70899b4

Browse files
joshspicerChuxel
andauthored
Select docker engine and CLI versions for Docker scripts (#1078)
* version selection for docker/moby engine and cli * docker-from-docker CLI fuzzy version selection * wording * more wording * more wording * update features.json * typo * simplify' * remove comment * update dockerfiles and comments * better error guarding * apply suggestions from code review Co-authored-by: Chuck Lantz <[email protected]> * update regexes * add -.* * typo Co-authored-by: Chuck Lantz <[email protected]>
1 parent c9d3b88 commit 70899b4

File tree

6 files changed

+115
-40
lines changed

6 files changed

+115
-40
lines changed

containers/docker-from-docker/.devcontainer/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ ARG UPGRADE_PACKAGES="false"
99
ARG ENABLE_NONROOT_DOCKER="true"
1010
# [Option] Use the OSS Moby CLI instead of the licensed Docker CLI
1111
ARG USE_MOBY="true"
12+
# [Option] Select CLI version
13+
ARG CLI_VERSION="latest"
1214

1315
# Enable new "BUILDKIT" mode for Docker CLI
1416
ENV DOCKER_BUILDKIT=1
@@ -22,7 +24,7 @@ COPY library-scripts/*.sh /tmp/library-scripts/
2224
RUN apt-get update \
2325
&& /bin/bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" "true" "true" \
2426
# Use Docker script from script library to set things up
25-
&& /bin/bash /tmp/library-scripts/docker-debian.sh "${ENABLE_NONROOT_DOCKER}" "/var/run/docker-host.sock" "/var/run/docker.sock" "${USERNAME}" "${USE_MOBY}" \
27+
&& /bin/bash /tmp/library-scripts/docker-debian.sh "${ENABLE_NONROOT_DOCKER}" "/var/run/docker-host.sock" "/var/run/docker.sock" "${USERNAME}" "${USE_MOBY}" "${CLI_VERSION}" \
2628
# Clean up
2729
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts/
2830

containers/docker-in-docker/.devcontainer/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ ARG UPGRADE_PACKAGES="false"
99
ARG ENABLE_NONROOT_DOCKER="true"
1010
# [Option] Use the OSS Moby Engine instead of the licensed Docker Engine
1111
ARG USE_MOBY="true"
12+
# [Option] Engine/CLI Version
13+
ARG DOCKER_VERSION="latest"
1214

1315
# Enable new "BUILDKIT" mode for Docker CLI
1416
ENV DOCKER_BUILDKIT=1
@@ -22,7 +24,7 @@ COPY library-scripts/*.sh /tmp/library-scripts/
2224
RUN apt-get update \
2325
&& /bin/bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" "true" "true" \
2426
# Use Docker script from script library to set things up
25-
&& /bin/bash /tmp/library-scripts/docker-in-docker-debian.sh "${ENABLE_NONROOT_DOCKER}" "${USERNAME}" "${USE_MOBY}" \
27+
&& /bin/bash /tmp/library-scripts/docker-in-docker-debian.sh "${ENABLE_NONROOT_DOCKER}" "${USERNAME}" "${USE_MOBY}" "${DOCKER_VERSION}" \
2628
# Clean up
2729
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts/
2830

script-library/container-features/src/feature-scripts.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
_VSC_INSTALL_DOCKER_IN_DOCKER="docker-in-docker-debian.sh true automatic ${_BUILD_ARG_DOCKER_IN_DOCKER_MOBY:-true}"
2-
_VSC_INSTALL_DOCKER_FROM_DOCKER="docker-debian.sh true /var/run/docker-host.sock /var/run/docker.sock automatic ${_BUILD_ARG_DOCKER_FROM_DOCKER_MOBY:-true}"
1+
_VSC_INSTALL_DOCKER_IN_DOCKER="docker-in-docker-debian.sh true automatic ${_BUILD_ARG_DOCKER_IN_DOCKER_MOBY:-true} {_BUILD_ARG_DOCKER_IN_DOCKER_VERSION:-latest}
2+
_VSC_INSTALL_DOCKER_FROM_DOCKER="docker-debian.sh true /var/run/docker-host.sock /var/run/docker.sock automatic ${_BUILD_ARG_DOCKER_FROM_DOCKER_MOBY:-true} {_BUILD_ARG_DOCKER_FROM_DOCKER_VERSION:-latest}"
33
_VSC_INSTALL_KUBECTL_HELM_MINIKUBE="kubectl-helm-debian.sh ${_BUILD_ARG_KUBECTL_HELM_MINIKUBE_VERSION:-latest} ${_BUILD_ARG_KUBECTL_HELM_MINIKUBE_HELM:-latest} ${_BUILD_ARG_KUBECTL_HELM_MINIKUBE_MINIKUBE:-latest}"
44
_VSC_INSTALL_TERRAFORM="terraform-debian.sh ${_BUILD_ARG_TERRAFORM_VERSION:-latest} ${_BUILD_ARG_TERRAFORM_TFLINT:-latest} ${_BUILD_ARG_TERRAFORM_TERRAGRUNT:-latest}"
55
_VSC_INSTALL_GIT="git-from-src-debian.sh ${_BUILD_ARG_GIT_VERSION:-latest} ${_BUILD_ARG_GIT_PPA:-true}"

script-library/container-features/src/features.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"options": {
88
"version": {
99
"type": "string",
10-
"enum": ["latest"],
10+
"proposals": ["latest", "20.10" ],
1111
"default": "latest",
12-
"description": "Currently unused."
12+
"description": "Select or enter a Docker/Moby Engine version. (Available versions may vary by Linux distribution.)"
1313
},
1414
"moby": {
1515
"type": "boolean",
@@ -65,9 +65,9 @@
6565
"options": {
6666
"version": {
6767
"type": "string",
68-
"enum": ["latest"],
68+
"proposals": ["latest", "20.10"],
6969
"default": "latest",
70-
"description": "Currently unused."
70+
"description": "Select or enter a Docker/Moby CLI version. (Available versions may vary by Linux distribution.)"
7171
}
7272
},
7373
"buildArg": "_VSC_INSTALL_DOCKER_FROM_DOCKER",

script-library/docker-debian.sh

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/docker.md
88
# Maintainer: The VS Code and Codespaces Teams
99
#
10-
# Syntax: ./docker-debian.sh [enable non-root docker socket access flag] [source socket] [target socket] [non-root user] [use moby]
10+
# Syntax: ./docker-debian.sh [enable non-root docker socket access flag] [source socket] [target socket] [non-root user] [use moby] [CLI version]
1111

1212
ENABLE_NONROOT_DOCKER=${1:-"true"}
1313
SOURCE_SOCKET=${2:-"/var/run/docker-host.sock"}
1414
TARGET_SOCKET=${3:-"/var/run/docker.sock"}
1515
USERNAME=${4:-"automatic"}
1616
USE_MOBY=${5:-"true"}
17+
CLI_VERSION=${6:-"latest"}
1718
MICROSOFT_GPG_KEYS_URI="https://packages.microsoft.com/keys/microsoft.asc"
1819
DOCKER_DASH_COMPOSE_VERSION="1"
1920

@@ -109,6 +110,50 @@ find_version_from_git_tags() {
109110
echo "${variable_name}=${!variable_name}"
110111
}
111112

113+
114+
# Source /etc/os-release to get OS info
115+
. /etc/os-release
116+
# Fetch host/container arch.
117+
architecture="$(dpkg --print-architecture)"
118+
119+
# Set up the necessary apt repos (either Microsoft's or Docker's)
120+
if [ "${USE_MOBY}" = "true" ]; then
121+
122+
cli_package_name="moby-cli"
123+
124+
# Import key safely and import Microsoft apt repo
125+
get_common_setting MICROSOFT_GPG_KEYS_URI
126+
curl -sSL ${MICROSOFT_GPG_KEYS_URI} | gpg --dearmor > /usr/share/keyrings/microsoft-archive-keyring.gpg
127+
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
128+
else
129+
# Name of proprietary engine package
130+
cli_package_name="docker-ce-cli"
131+
132+
# Import key safely and import Docker apt repo
133+
curl -fsSL https://download.docker.com/linux/${ID}/gpg | gpg --dearmor > /usr/share/keyrings/docker-archive-keyring.gpg
134+
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
135+
fi
136+
137+
# Refresh apt lists
138+
apt-get update
139+
140+
# Soft version matching for CLI
141+
if [ "${CLI_VERSION}" = "latest" ] || [ "${CLI_VERSION}" = "lts" ] || [ "${CLI_VERSION}" = "stable" ]; then
142+
# Empty, meaning grab whatever "latest" is in apt repo
143+
cli_version_suffix=""
144+
else
145+
# Fetch a valid version from the apt-cache (eg: the Microsoft repo appends +azure, breakfix, etc...)
146+
cli_version_suffix="=$(apt-cache madison ${cli_package_name} | awk -F"|" '{print $2}' | sed -e 's/^[ \t]*//' | grep -E -m 1 "^(${CLI_VERSION})(\.|$|\+.*|-.*)")"
147+
148+
if [ -z ${cli_version_suffix} ] || [ ${cli_version_suffix} = "=" ]; then
149+
echo "ERR: Parsed CLI_VERSION (${CLI_VERSION}) was not found in the apt-cache for this package+distribution combo";
150+
echo "Available versions for your distribution (NOTE: pass to this script in the form -> MAJOR.MINOR.REV)"
151+
apt-cache madison ${cli_package_name} | awk -F"|" '{print $2}'
152+
exit 1
153+
fi
154+
echo "cli_version_suffix ${cli_version_suffix}"
155+
fi
156+
112157
# Ensure apt is in non-interactive to avoid prompts
113158
export DEBIAN_FRONTEND=noninteractive
114159

@@ -120,26 +165,14 @@ if ! type git > /dev/null 2>&1; then
120165
fi
121166

122167
# Install Docker / Moby CLI if not already installed
123-
architecture="$(dpkg --print-architecture)"
124168
if type docker > /dev/null 2>&1; then
125169
echo "Docker / Moby CLI already installed."
126170
else
127-
# Source /etc/os-release to get OS info
128-
. /etc/os-release
129171
if [ "${USE_MOBY}" = "true" ]; then
130-
# Import key safely (new 'signed-by' method rather than deprecated apt-key approach) and install
131-
get_common_setting MICROSOFT_GPG_KEYS_URI
132-
curl -sSL ${MICROSOFT_GPG_KEYS_URI} | gpg --dearmor > /usr/share/keyrings/microsoft-archive-keyring.gpg
133-
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
134-
apt-get update
135-
apt-get -y install --no-install-recommends moby-cli moby-buildx moby-engine
172+
apt-get -y install --no-install-recommends moby-cli${cli_version_suffix} moby-buildx
136173
apt-get -y install --no-install-recommends moby-compose || echo "(*) Package moby-compose (Docker Compose v2) not available for ${VERSION_CODENAME} ${architecture}. Skipping."
137174
else
138-
# Import key safely (new 'signed-by' method rather than deprecated apt-key approach) and install
139-
curl -fsSL https://download.docker.com/linux/${ID}/gpg | gpg --dearmor > /usr/share/keyrings/docker-archive-keyring.gpg
140-
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
141-
apt-get update
142-
apt-get -y install --no-install-recommends docker-ce-cli
175+
apt-get -y install --no-install-recommends docker-ce-cli${cli_version_suffix}
143176
fi
144177
fi
145178

script-library/docker-in-docker-debian.sh

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/docker-in-docker.md
88
# Maintainer: The VS Code and Codespaces Teams
99
#
10-
# Syntax: ./docker-in-docker-debian.sh [enable non-root docker access flag] [non-root user] [use moby]
10+
# Syntax: ./docker-in-docker-debian.sh [enable non-root docker access flag] [non-root user] [use moby] [Engine/CLI Version]
1111

1212
ENABLE_NONROOT_DOCKER=${1:-"true"}
1313
USERNAME=${2:-"automatic"}
1414
USE_MOBY=${3:-"true"}
15+
DOCKER_VERSION=${4:-"latest"} # The Docker/Moby Engine + CLI should match in version
1516
MICROSOFT_GPG_KEYS_URI="https://packages.microsoft.com/keys/microsoft.asc"
1617
DOCKER_DASH_COMPOSE_VERSION="1"
1718

@@ -123,33 +124,69 @@ if type iptables-legacy > /dev/null 2>&1; then
123124
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
124125
fi
125126

126-
# Install Docker / Moby CLI if not already installed
127+
# Source /etc/os-release to get OS info
128+
. /etc/os-release
129+
# Fetch host/container arch.
127130
architecture="$(dpkg --print-architecture)"
131+
132+
# Set up the necessary apt repos (either Microsoft's or Docker's)
133+
if [ "${USE_MOBY}" = "true" ]; then
134+
135+
# Name of open source engine/cli
136+
engine_package_name="moby-engine"
137+
cli_package_name="moby-cli"
138+
139+
# Import key safely and import Microsoft apt repo
140+
get_common_setting MICROSOFT_GPG_KEYS_URI
141+
curl -sSL ${MICROSOFT_GPG_KEYS_URI} | gpg --dearmor > /usr/share/keyrings/microsoft-archive-keyring.gpg
142+
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
143+
else
144+
# Name of licensed engine/cli
145+
engine_package_name="docker-ce"
146+
cli_package_name="docker-ce-cli"
147+
148+
# Import key safely and import Docker apt repo
149+
curl -fsSL https://download.docker.com/linux/${ID}/gpg | gpg --dearmor > /usr/share/keyrings/docker-archive-keyring.gpg
150+
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
151+
fi
152+
153+
# Refresh apt lists
154+
apt-get update
155+
156+
# Soft version matching
157+
if [ "${DOCKER_VERSION}" = "latest" ] || [ "${DOCKER_VERSION}" = "lts" ] || [ "${DOCKER_VERSION}" = "stable" ]; then
158+
# Empty, meaning grab whatever "latest" is in apt repo
159+
engine_version_suffix=""
160+
cli_version_suffix=""
161+
else
162+
# Fetch a valid version from the apt-cache (eg: the Microsoft repo appends +azure, breakfix, etc...)
163+
engine_version_suffix="=$(apt-cache madison ${engine_package_name} | awk -F"|" '{print $2}' | sed -e 's/^[ \t]*//' | grep -E -m 1 "^(${DOCKER_VERSION})(\.|$|\+.*|-.*)")"
164+
cli_version_suffix="=$(apt-cache madison ${cli_package_name} | awk -F"|" '{print $2}' | sed -e 's/^[ \t]*//' | grep -E -m 1 "^(${DOCKER_VERSION})(\.|$|\+.*|-.*)")"
165+
if [ ${engine_version_suffix} = "=" ] || [ ${cli_version_suffix} = "=" ] ; then
166+
echo "ERR: Provided VERSION (${DOCKER_VERSION}) was not found in the apt-cache for the CLI and/or Engine in this distribution";
167+
echo "Available *engine* versions for your distribution (NOTE: pass to this script in the form -> MAJOR.MINOR.REV)"
168+
apt-cache madison ${engine_package_name} | awk -F"|" '{print $2}'
169+
exit 1
170+
fi
171+
echo "engine_version_suffix ${engine_version_suffix}"
172+
echo "cli_version_suffix ${cli_version_suffix}"
173+
fi
174+
175+
# Install Docker / Moby CLI if not already installed
128176
if type docker > /dev/null 2>&1 && type dockerd > /dev/null 2>&1; then
129177
echo "Docker / Moby CLI and Engine already installed."
130178
else
131-
# Source /etc/os-release to get OS info
132-
. /etc/os-release
133179
if [ "${USE_MOBY}" = "true" ]; then
134-
# Import key safely (new 'signed-by' method rather than deprecated apt-key approach) and install
135-
get_common_setting MICROSOFT_GPG_KEYS_URI
136-
curl -sSL ${MICROSOFT_GPG_KEYS_URI} | gpg --dearmor > /usr/share/keyrings/microsoft-archive-keyring.gpg
137-
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
138-
apt-get update
139-
apt-get -y install --no-install-recommends moby-cli moby-buildx moby-engine
180+
apt-get -y install --no-install-recommends moby-cli${cli_version_suffix} moby-buildx moby-engine${engine_version_suffix}
140181
apt-get -y install --no-install-recommends moby-compose || echo "(*) Package moby-compose (Docker Compose v2) not available for ${VERSION_CODENAME} ${architecture}. Skipping."
141182
else
142-
# Import key safely (new 'signed-by' method rather than deprecated apt-key approach) and install
143-
curl -fsSL https://download.docker.com/linux/${ID}/gpg | gpg --dearmor > /usr/share/keyrings/docker-archive-keyring.gpg
144-
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
145-
apt-get update
146-
apt-get -y install --no-install-recommends docker-ce-cli docker-ce
183+
apt-get -y install --no-install-recommends docker-ce-cli${cli_version_suffix} docker-ce${engine_version_suffix}
147184
fi
148185
fi
149186

150-
echo "Finished installing docker / moby"
187+
echo "Finished installing docker / moby!"
151188

152-
# Install Docker Compose if not already installed and is on a supported architecture
189+
# Install Docker Compose if not already installed and is on a supported architecture
153190
if type docker-compose > /dev/null 2>&1; then
154191
echo "Docker Compose already installed."
155192
else
@@ -176,6 +213,7 @@ else
176213
${pipx_bin} install --system-site-packages --pip-args '--no-cache-dir --force-reinstall' docker-compose
177214
rm -rf /tmp/pip-tmp
178215
else
216+
# Only supports docker-compose v1
179217
find_version_from_git_tags DOCKER_DASH_COMPOSE_VERSION "https://github.com/docker/compose" "tags/"
180218
echo "(*) Installing docker-compose ${DOCKER_DASH_COMPOSE_VERSION}..."
181219
curl -fsSL "https://github.com/docker/compose/releases/download/${DOCKER_DASH_COMPOSE_VERSION}/docker-compose-Linux-x86_64" -o /usr/local/bin/docker-compose

0 commit comments

Comments
 (0)