Skip to content

Commit bbb24d1

Browse files
Docker-in-docker: Add retries to the docker-init script until docker daemon starts (#637)
* Docker-in-docker: Add retries until docker daemon starts * address comments
1 parent 29859af commit bbb24d1

File tree

5 files changed

+88
-9
lines changed

5 files changed

+88
-9
lines changed

src/docker-in-docker/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "docker-in-docker",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"name": "Docker (Docker-in-Docker)",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/docker-in-docker",
66
"description": "Create child containers *inside* a container, independent from the host's docker instance. Installs Docker extension in the container along with needed CLIs.",

src/docker-in-docker/install.sh

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#-------------------------------------------------------------------------------------------------------------
66
#
77
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/docker-in-docker.md
8-
# Maintainer: The VS Code and Codespaces Teams
8+
# Maintainer: The Dev Container spec maintainers
99

1010

1111
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
423423
INNEREOF
424424
)"
425425
426-
# Start using sudo if not invoked as root
427-
if [ "$(id -u)" -ne 0 ]; then
428-
sudo /bin/sh -c "${dockerd_start}"
429-
else
430-
eval "${dockerd_start}"
431-
fi
426+
retry_count=0
427+
docker_ok="false"
428+
429+
until [ "${docker_ok}" = "true" ] || [ "${retry_count}" -eq "5" ];
430+
do
431+
# Start using sudo if not invoked as root
432+
if [ "$(id -u)" -ne 0 ]; then
433+
sudo /bin/sh -c "${dockerd_start}"
434+
else
435+
eval "${dockerd_start}"
436+
fi
437+
438+
set +e
439+
docker info > /dev/null 2>&1 && docker_ok="true"
440+
441+
if [ "${docker_ok}" != "true" ]; then
442+
echo "(*) Failed to start docker, retrying in 5s..."
443+
retry_count=`expr $retry_count + 1`
444+
sleep 5s
445+
fi
446+
set -e
447+
done
432448
433449
set +e
434450
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "docker-buildx" docker buildx version
10+
check "docker-ps" docker ps
11+
12+
sleep 5s
13+
14+
# Stop docker
15+
pkill dockerd
16+
pkill containerd
17+
18+
sleep 5s
19+
20+
set +e
21+
docker_ok_code="$(docker info > /dev/null 2>&1; echo $?)"
22+
set -e
23+
24+
check "docker-not-running" bash -c "[[ ${docker_ok_code} == 1 ]]"
25+
26+
# Testing retry logic
27+
./test-scripts/docker-test-init.sh
28+
29+
check "docker-started-after-retries" docker ps
30+
31+
# Report result
32+
reportResults

test/docker-in-docker/scenarios.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
}
6868
},
6969
"remoteUser": "node"
70+
},
71+
"docker_retry": {
72+
"image": "ubuntu:focal",
73+
"features": {
74+
"docker-in-docker": {}
75+
}
7076
}
71-
7277
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
7+
retry_count=0
8+
docker_ok="false"
9+
10+
until [ "${docker_ok}" = "true" ] || [ "${retry_count}" -eq "5" ];
11+
do
12+
if [ "${retry_count}" -eq "3" ]; then
13+
echo "Starting docker after 3 retries..."
14+
/usr/local/share/docker-init.sh
15+
fi
16+
17+
set +e
18+
docker info > /dev/null 2>&1 && docker_ok="true"
19+
20+
if [ "${docker_ok}" != "true" ]; then
21+
echo "(*) Failed to start docker, retrying in 5s... Retry count: ${retry_count}"
22+
retry_count=`expr $retry_count + 1`
23+
sleep 1s
24+
fi
25+
set -e
26+
done

0 commit comments

Comments
 (0)