From 46e0c05e7ebdfc7c3fe43aa0940b6482cb913bfb Mon Sep 17 00:00:00 2001 From: Hutson Betts Date: Thu, 28 Jan 2021 04:31:45 -0600 Subject: [PATCH] [ubuntu] feat(docker): authenticate using DockerHub credentials (#2548) Ubuntu builds, by default, pull images anonymously from the official DockerHub. DockerHub rate limits requests from anonymous and unpaid accounts: - https://docs.docker.com/docker-hub/download-rate-limit/ When those rate limits are reached the Packer build will fail with an error indicated the rate limit has been reached. Add support for providing credentials for a paid account that can be used by the Docker setup steps to authenticate when pulling images from DockerHub, thereby avoiding the rate limit. Related to #2094 --- .../linux/scripts/installers/docker-moby.sh | 13 ++++++++++- images/linux/ubuntu1604.json | 22 +++++++++++++++---- images/linux/ubuntu1804.json | 22 +++++++++++++++---- images/linux/ubuntu2004.json | 22 +++++++++++++++---- 4 files changed, 66 insertions(+), 13 deletions(-) diff --git a/images/linux/scripts/installers/docker-moby.sh b/images/linux/scripts/installers/docker-moby.sh index 3ab5410f6..bb900eb29 100644 --- a/images/linux/scripts/installers/docker-moby.sh +++ b/images/linux/scripts/installers/docker-moby.sh @@ -28,10 +28,21 @@ systemctl is-enabled --quiet docker.service || systemctl enable docker.service sleep 10 docker info +# If credentials are provided, attempt to log into Docker Hub +# with a paid account to avoid Docker Hub's rate limit. +if [ "${DOCKERHUB_LOGIN}" ] && [ "${DOCKERHUB_PASSWORD}" ]; then + docker login --username "${DOCKERHUB_LOGIN}" --password "${DOCKERHUB_PASSWORD}" +fi + # Pull images images=$(get_toolset_value '.docker.images[]') for image in $images; do docker pull "$image" done -invoke_tests "Tools" "Docker" \ No newline at end of file +# Always attempt to logout so we do not leave our credentials on the built +# image. Logout _should_ return a zero exit code even if no credentials were +# stored from earlier. +docker logout + +invoke_tests "Tools" "Docker" diff --git a/images/linux/ubuntu1604.json b/images/linux/ubuntu1604.json index 6f6dd2e53..06d7c9c50 100644 --- a/images/linux/ubuntu1604.json +++ b/images/linux/ubuntu1604.json @@ -20,7 +20,9 @@ "capture_name_prefix": "packer", "image_version": "dev", "image_os": "ubuntu16", - "run_validation_diskspace": "false" + "run_validation_diskspace": "false", + "dockerhub_login": "{{env `DOCKERHUB_LOGIN`}}", + "dockerhub_password": "{{env `DOCKERHUB_PASSWORD`}}" }, "sensitive-variables": [ "client_secret" @@ -166,6 +168,20 @@ ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" }, + { + "type": "shell", + "scripts": [ + "{{template_dir}}/scripts/installers/docker-compose.sh", + "{{template_dir}}/scripts/installers/docker-moby.sh" + ], + "environment_vars": [ + "HELPER_SCRIPTS={{user `helper_script_folder`}}", + "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}", + "DOCKERHUB_LOGIN={{user `dockerhub_login`}}", + "DOCKERHUB_PASSWORD={{user `dockerhub_password`}}" + ], + "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" + }, { "type": "shell", "scripts": [ @@ -181,8 +197,6 @@ "{{template_dir}}/scripts/installers/swift.sh", "{{template_dir}}/scripts/installers/cmake.sh", "{{template_dir}}/scripts/installers/codeql-bundle.sh", - "{{template_dir}}/scripts/installers/docker-compose.sh", - "{{template_dir}}/scripts/installers/docker-moby.sh", "{{template_dir}}/scripts/installers/dotnetcore-sdk.sh", "{{template_dir}}/scripts/installers/erlang.sh", "{{template_dir}}/scripts/installers/firefox.sh", @@ -351,4 +365,4 @@ "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" } ] -} \ No newline at end of file +} diff --git a/images/linux/ubuntu1804.json b/images/linux/ubuntu1804.json index df527ccab..31c993fca 100644 --- a/images/linux/ubuntu1804.json +++ b/images/linux/ubuntu1804.json @@ -20,7 +20,9 @@ "capture_name_prefix": "packer", "image_version": "dev", "image_os": "ubuntu18", - "run_validation_diskspace": "false" + "run_validation_diskspace": "false", + "dockerhub_login": "{{env `DOCKERHUB_LOGIN`}}", + "dockerhub_password": "{{env `DOCKERHUB_PASSWORD`}}" }, "sensitive-variables": [ "client_secret" @@ -169,6 +171,20 @@ ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" }, + { + "type": "shell", + "scripts": [ + "{{template_dir}}/scripts/installers/docker-compose.sh", + "{{template_dir}}/scripts/installers/docker-moby.sh" + ], + "environment_vars": [ + "HELPER_SCRIPTS={{user `helper_script_folder`}}", + "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}", + "DOCKERHUB_LOGIN={{user `dockerhub_login`}}", + "DOCKERHUB_PASSWORD={{user `dockerhub_password`}}" + ], + "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" + }, { "type": "shell", "scripts": [ @@ -184,8 +200,6 @@ "{{template_dir}}/scripts/installers/cmake.sh", "{{template_dir}}/scripts/installers/codeql-bundle.sh", "{{template_dir}}/scripts/installers/containers.sh", - "{{template_dir}}/scripts/installers/docker-compose.sh", - "{{template_dir}}/scripts/installers/docker-moby.sh", "{{template_dir}}/scripts/installers/dotnetcore-sdk.sh", "{{template_dir}}/scripts/installers/erlang.sh", "{{template_dir}}/scripts/installers/firefox.sh", @@ -356,4 +370,4 @@ "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" } ] -} \ No newline at end of file +} diff --git a/images/linux/ubuntu2004.json b/images/linux/ubuntu2004.json index 677d35a60..68b2da20c 100644 --- a/images/linux/ubuntu2004.json +++ b/images/linux/ubuntu2004.json @@ -20,7 +20,9 @@ "capture_name_prefix": "packer", "image_version": "dev", "image_os": "ubuntu20", - "run_validation_diskspace": "false" + "run_validation_diskspace": "false", + "dockerhub_login": "{{env `DOCKERHUB_LOGIN`}}", + "dockerhub_password": "{{env `DOCKERHUB_PASSWORD`}}" }, "sensitive-variables": [ "client_secret" @@ -169,6 +171,20 @@ ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" }, + { + "type": "shell", + "scripts": [ + "{{template_dir}}/scripts/installers/docker-compose.sh", + "{{template_dir}}/scripts/installers/docker-moby.sh" + ], + "environment_vars": [ + "HELPER_SCRIPTS={{user `helper_script_folder`}}", + "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}", + "DOCKERHUB_LOGIN={{user `dockerhub_login`}}", + "DOCKERHUB_PASSWORD={{user `dockerhub_password`}}" + ], + "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" + }, { "type": "shell", "scripts": [ @@ -184,8 +200,6 @@ "{{template_dir}}/scripts/installers/cmake.sh", "{{template_dir}}/scripts/installers/codeql-bundle.sh", "{{template_dir}}/scripts/installers/containers.sh", - "{{template_dir}}/scripts/installers/docker-compose.sh", - "{{template_dir}}/scripts/installers/docker-moby.sh", "{{template_dir}}/scripts/installers/dotnetcore-sdk.sh", "{{template_dir}}/scripts/installers/erlang.sh", "{{template_dir}}/scripts/installers/firefox.sh", @@ -357,4 +371,4 @@ "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" } ] -} \ No newline at end of file +}