Skip to content

Commit 5964ac2

Browse files
author
David Roberts
authored
[7.10][ML] Retry pulling of Docker images to work around network issues (#1542)
Adds a function to retry pulling Docker images up to 5 times, and calls it to pre-fetch every Docker image that's used as a "FROM" image by our "docker build" commands. "docker build" will pull the "FROM" image if it's not present on the local machine, but will only try once, so is vulnerable to a transient problem with the Docker registry failing the whole build. By retrying "docker pull" several times before running "docker build" we ensure that the image is present locally when "docker build" runs and hence "docker build" never needs to pull it. Backport of #1535
1 parent 3657ce5 commit 5964ac2

10 files changed

+74
-10
lines changed

dev-tools/docker/build_check_style_image.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ set -e
2121

2222
cd `dirname $0`
2323

24-
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION check_style_image
24+
. ./prefetch_docker_image.sh
25+
CONTEXT=check_style_image
26+
prefetch_docker_image $CONTEXT/Dockerfile
27+
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION $CONTEXT
2528
# Get a username and password for this by visiting
26-
# https://docker.elastic.co:7000 and allowing it to authenticate against your
29+
# https://docker-auth.elastic.co and allowing it to authenticate against your
2730
# GitHub account
2831
docker login $HOST
2932
docker push $HOST/$ACCOUNT/$REPOSITORY:$VERSION

dev-tools/docker/build_linux_aarch64_cross_build_image.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ set -e
2323

2424
cd `dirname $0`
2525

26-
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION linux_aarch64_cross_image
26+
. ./prefetch_docker_image.sh
27+
CONTEXT=linux_aarch64_cross_image
28+
prefetch_docker_image $CONTEXT/Dockerfile
29+
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION $CONTEXT
2730
# Get a username and password for this by visiting
28-
# https://docker.elastic.co:7000 and allowing it to authenticate against your
31+
# https://docker-auth.elastic.co and allowing it to authenticate against your
2932
# GitHub account
3033
docker login $HOST
3134
docker push $HOST/$ACCOUNT/$REPOSITORY:$VERSION

dev-tools/docker/build_linux_aarch64_native_build_image.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ set -e
2929

3030
cd `dirname $0`
3131

32-
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION linux_aarch64_native_image
32+
. ./prefetch_docker_image.sh
33+
CONTEXT=linux_aarch64_native_image
34+
prefetch_docker_image $CONTEXT/Dockerfile
35+
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION $CONTEXT
3336
# Get a username and password for this by visiting
34-
# https://docker.elastic.co:7000 and allowing it to authenticate against your
37+
# https://docker-auth.elastic.co and allowing it to authenticate against your
3538
# GitHub account
3639
docker login $HOST
3740
docker push $HOST/$ACCOUNT/$REPOSITORY:$VERSION

dev-tools/docker/build_linux_build_image.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ set -e
2929

3030
cd `dirname $0`
3131

32-
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION linux_image
32+
. ./prefetch_docker_image.sh
33+
CONTEXT=linux_image
34+
prefetch_docker_image $CONTEXT/Dockerfile
35+
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION $CONTEXT
3336
# Get a username and password for this by visiting
34-
# https://docker.elastic.co:7000 and allowing it to authenticate against your
37+
# https://docker-auth.elastic.co and allowing it to authenticate against your
3538
# GitHub account
3639
docker login $HOST
3740
docker push $HOST/$ACCOUNT/$REPOSITORY:$VERSION

dev-tools/docker/build_macosx_build_image.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ set -e
2323

2424
cd `dirname $0`
2525

26-
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION macosx_image
26+
. ./prefetch_docker_image.sh
27+
CONTEXT=macosx_image
28+
prefetch_docker_image $CONTEXT/Dockerfile
29+
docker build --no-cache -t $HOST/$ACCOUNT/$REPOSITORY:$VERSION $CONTEXT
2730
# Get a username and password for this by visiting
28-
# https://docker.elastic.co:7000 and allowing it to authenticate against your
31+
# https://docker-auth.elastic.co and allowing it to authenticate against your
2932
# GitHub account
3033
docker login $HOST
3134
docker push $HOST/$ACCOUNT/$REPOSITORY:$VERSION
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
#
3+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
4+
# or more contributor license agreements. Licensed under the Elastic License;
5+
# you may not use this file except in compliance with the Elastic License.
6+
#
7+
8+
# Pull a docker image, retrying up to 5 times.
9+
#
10+
# Making sure the "FROM" image is present locally before building an image
11+
# based on it removes the risk of a "docker build" failing due to transient
12+
# Docker registry problems.
13+
#
14+
# The argument is the path to the Dockerfile to be built.
15+
function prefetch_docker_image {
16+
DOCKERFILE="$1"
17+
IMAGE=$(grep '^FROM' "$DOCKERFILE" | awk '{ print $2 }' | head -1)
18+
ATTEMPT=0
19+
MAX_RETRIES=5
20+
21+
while true
22+
do
23+
ATTEMPT=$((ATTEMPT+1))
24+
25+
if [ $ATTEMPT -gt $MAX_RETRIES ] ; then
26+
echo "Docker pull retries exceeded, aborting."
27+
exit 1
28+
fi
29+
30+
if docker pull "$IMAGE" ; then
31+
echo "Docker pull of $IMAGE successful."
32+
break
33+
else
34+
echo "Docker pull of $IMAGE unsuccessful, attempt '$ATTEMPT'."
35+
fi
36+
done
37+
}
38+

dev-tools/docker_build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ cd "$TOOLS_DIR/.."
6262
# necessary network access
6363
3rd_party/pull-eigen.sh
6464

65+
. "$TOOLS_DIR/docker/prefetch_docker_image.sh"
66+
6567
for PLATFORM in `echo $PLATFORMS | tr ' ' '\n' | sort -u`
6668
do
6769

@@ -72,6 +74,7 @@ do
7274
DOCKERFILE="$TOOLS_DIR/docker/${PLATFORM}_builder/Dockerfile"
7375
TEMP_TAG=`git rev-parse --short=14 HEAD`-$PLATFORM-$$
7476

77+
prefetch_docker_image "$DOCKERFILE"
7578
docker build --no-cache --force-rm -t $TEMP_TAG --build-arg VERSION_QUALIFIER="$VERSION_QUALIFIER" --build-arg SNAPSHOT=$SNAPSHOT -f "$DOCKERFILE" .
7679
# Using tar to copy the build artifacts out of the container seems more reliable
7780
# than docker cp, and also means the files end up with the correct uid/gid

dev-tools/docker_check_style.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ cd "$TOOLS_DIR/.."
2626
DOCKERFILE="$TOOLS_DIR/docker/style_checker/Dockerfile"
2727
TEMP_TAG=`git rev-parse --short=14 HEAD`-style-$$
2828

29+
. "$TOOLS_DIR/docker/prefetch_docker_image.sh"
30+
prefetch_docker_image "$DOCKERFILE"
2931
docker build --no-cache --force-rm -t $TEMP_TAG -f "$DOCKERFILE" .
3032
docker run --rm --workdir=/ml-cpp $TEMP_TAG dev-tools/check-style.sh --all
3133
RC=$?

dev-tools/docker_test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ cd "$TOOLS_DIR/.."
6262
# necessary network access
6363
3rd_party/pull-eigen.sh
6464

65+
. "$TOOLS_DIR/docker/prefetch_docker_image.sh"
66+
6567
for PLATFORM in `echo $PLATFORMS | tr ' ' '\n' | sort -u`
6668
do
6769

@@ -73,6 +75,7 @@ do
7375
DOCKERFILE="$TOOLS_DIR/docker/${PLATFORM}_tester/Dockerfile"
7476
TEMP_TAG=`git rev-parse --short=14 HEAD`-$PLATFORM-$$
7577

78+
prefetch_docker_image "$DOCKERFILE"
7679
docker build --no-cache --force-rm -t $TEMP_TAG --build-arg VERSION_QUALIFIER="$VERSION_QUALIFIER" --build-arg SNAPSHOT=$SNAPSHOT -f "$DOCKERFILE" .
7780
# Using tar to copy the build and test artifacts out of the container seems
7881
# more reliable than docker cp, and also means the files end up with the

dev-tools/jenkins_ci.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ git repack -a -d
6767
readonly GIT_TOPLEVEL=$(git rev-parse --show-toplevel 2> /dev/null)
6868
rm -f "${GIT_TOPLEVEL}/.git/objects/info/alternates"
6969

70+
# The Docker version is helpful to identify version-specific Docker bugs
71+
docker --version
72+
7073
# Build and test the native Linux architecture
7174
if [ "$HARDWARE_ARCH" = x86_64 ] ; then
7275

0 commit comments

Comments
 (0)