aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamruddhi Khandale <skhandale@microsoft.com>2023-08-08 01:59:58 +0300
committerGitHub <noreply@github.com>2023-08-08 01:59:58 +0300
commitbbb24d10d417452ef23a8c506fd0e2b141b71f15 (patch)
tree11bd7cb31f8d26b64ee56916d920bbb4d6915769
parent29859aff280dc187d6f508900f8600ebd04c56b7 (diff)
Docker-in-docker: Add retries to the docker-init script until docker daemon starts (#637)feature_docker-in-docker_2.3.0
* Docker-in-docker: Add retries until docker daemon starts * address comments
-rw-r--r--src/docker-in-docker/devcontainer-feature.json2
-rwxr-xr-xsrc/docker-in-docker/install.sh30
-rw-r--r--test/docker-in-docker/docker_retry.sh32
-rw-r--r--test/docker-in-docker/scenarios.json7
-rw-r--r--test/docker-in-docker/test-scripts/docker-test-init.sh26
5 files changed, 88 insertions, 9 deletions
diff --git a/src/docker-in-docker/devcontainer-feature.json b/src/docker-in-docker/devcontainer-feature.json
index 3349821..b5e0aee 100644
--- a/src/docker-in-docker/devcontainer-feature.json
+++ b/src/docker-in-docker/devcontainer-feature.json
@@ -1,6 +1,6 @@
{
"id": "docker-in-docker",
- "version": "2.2.1",
+ "version": "2.3.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.",
diff --git a/src/docker-in-docker/install.sh b/src/docker-in-docker/install.sh
index 6c889f5..f7e5a7d 100755
--- a/src/docker-in-docker/install.sh
+++ b/src/docker-in-docker/install.sh
@@ -5,7 +5,7 @@
#-------------------------------------------------------------------------------------------------------------
#
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/docker-in-docker.md
-# Maintainer: The VS Code and Codespaces Teams
+# Maintainer: The Dev Container spec maintainers
DOCKER_VERSION="${VERSION:-"latest"}" # The Docker/Moby Engine + CLI should match in version
@@ -423,12 +423,28 @@ dockerd_start="AZURE_DNS_AUTO_DETECTION=${AZURE_DNS_AUTO_DETECTION} DOCKER_DEFAU
INNEREOF
)"
-# Start using sudo if not invoked as root
-if [ "$(id -u)" -ne 0 ]; then
- sudo /bin/sh -c "${dockerd_start}"
-else
- eval "${dockerd_start}"
-fi
+retry_count=0
+docker_ok="false"
+
+until [ "${docker_ok}" = "true" ] || [ "${retry_count}" -eq "5" ];
+do
+ # Start using sudo if not invoked as root
+ if [ "$(id -u)" -ne 0 ]; then
+ sudo /bin/sh -c "${dockerd_start}"
+ else
+ eval "${dockerd_start}"
+ fi
+
+ set +e
+ docker info > /dev/null 2>&1 && docker_ok="true"
+
+ if [ "${docker_ok}" != "true" ]; then
+ echo "(*) Failed to start docker, retrying in 5s..."
+ retry_count=`expr $retry_count + 1`
+ sleep 5s
+ fi
+ set -e
+done
set +e
diff --git a/test/docker-in-docker/docker_retry.sh b/test/docker-in-docker/docker_retry.sh
new file mode 100644
index 0000000..cc35fbb
--- /dev/null
+++ b/test/docker-in-docker/docker_retry.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+set -e
+
+# Optional: Import test library
+source dev-container-features-test-lib
+
+# Definition specific tests
+check "docker-buildx" docker buildx version
+check "docker-ps" docker ps
+
+sleep 5s
+
+# Stop docker
+pkill dockerd
+pkill containerd
+
+sleep 5s
+
+set +e
+ docker_ok_code="$(docker info > /dev/null 2>&1; echo $?)"
+set -e
+
+check "docker-not-running" bash -c "[[ ${docker_ok_code} == 1 ]]"
+
+# Testing retry logic
+./test-scripts/docker-test-init.sh
+
+check "docker-started-after-retries" docker ps
+
+# Report result
+reportResults
diff --git a/test/docker-in-docker/scenarios.json b/test/docker-in-docker/scenarios.json
index 9c44478..525ce69 100644
--- a/test/docker-in-docker/scenarios.json
+++ b/test/docker-in-docker/scenarios.json
@@ -67,6 +67,11 @@
}
},
"remoteUser": "node"
+ },
+ "docker_retry": {
+ "image": "ubuntu:focal",
+ "features": {
+ "docker-in-docker": {}
+ }
}
-
}
diff --git a/test/docker-in-docker/test-scripts/docker-test-init.sh b/test/docker-in-docker/test-scripts/docker-test-init.sh
new file mode 100644
index 0000000..b213fb4
--- /dev/null
+++ b/test/docker-in-docker/test-scripts/docker-test-init.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+#-------------------------------------------------------------------------------------------------------------
+# Copyright (c) Microsoft Corporation. All rights reserved.
+# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
+#-------------------------------------------------------------------------------------------------------------
+
+retry_count=0
+docker_ok="false"
+
+until [ "${docker_ok}" = "true" ] || [ "${retry_count}" -eq "5" ];
+do
+ if [ "${retry_count}" -eq "3" ]; then
+ echo "Starting docker after 3 retries..."
+ /usr/local/share/docker-init.sh
+ fi
+
+ set +e
+ docker info > /dev/null 2>&1 && docker_ok="true"
+
+ if [ "${docker_ok}" != "true" ]; then
+ echo "(*) Failed to start docker, retrying in 5s... Retry count: ${retry_count}"
+ retry_count=`expr $retry_count + 1`
+ sleep 1s
+ fi
+ set -e
+done \ No newline at end of file