From 3455e99d4563285e0b688505098eaca0bc5e3c53 Mon Sep 17 00:00:00 2001 From: Shilong Liu Date: Fri, 18 Mar 2022 12:01:52 +0800 Subject: [PATCH] Add a config variable to override default container registry instead of dockerhub. (#10166) (#10262) * Add variable to reset default docker registry * fix bug in docker version control --- Makefile.work | 9 +++++++-- dockers/docker-base-buster/Dockerfile.j2 | 7 ++++--- dockers/docker-base-stretch/Dockerfile.j2 | 7 ++++--- dockers/docker-base/Dockerfile.j2 | 7 ++++--- dockers/docker-ptf/Dockerfile.j2 | 7 ++++--- dockers/docker-sonic-mgmt/Dockerfile.j2 | 3 ++- rules/config | 3 +++ scripts/docker_version_control.sh | 6 ++++-- slave.mk | 3 +++ sonic-slave-buster/Dockerfile.j2 | 9 +++++---- sonic-slave-jessie/Dockerfile.j2 | 7 ++++--- sonic-slave-stretch/Dockerfile.j2 | 7 ++++--- 12 files changed, 48 insertions(+), 27 deletions(-) diff --git a/Makefile.work b/Makefile.work index ced72b31a160..51d8c1fc6334 100644 --- a/Makefile.work +++ b/Makefile.work @@ -110,6 +110,10 @@ rules/config.user: include rules/config -include rules/config.user +ifneq ($(DEFAULT_CONTAINER_REGISTRY),) +override DEFAULT_CONTAINER_REGISTRY := $(DEFAULT_CONTAINER_REGISTRY)/ +endif + ifeq ($(ENABLE_DOCKER_BASE_PULL),) override ENABLE_DOCKER_BASE_PULL = n endif @@ -134,9 +138,9 @@ $(shell SONIC_VERSION_CONTROL_COMPONENTS=$(SONIC_VERSION_CONTROL_COMPONENTS) \ scripts/generate_buildinfo_config.sh) # Generate the slave Dockerfile, and prepare build info for it -$(shell CONFIGURED_ARCH=$(CONFIGURED_ARCH) MULTIARCH_QEMU_ENVIRON=$(MULTIARCH_QEMU_ENVIRON) DOCKER_EXTRA_OPTS=$(DOCKER_EXTRA_OPTS) j2 $(SLAVE_DIR)/Dockerfile.j2 > $(SLAVE_DIR)/Dockerfile) +$(shell CONFIGURED_ARCH=$(CONFIGURED_ARCH) MULTIARCH_QEMU_ENVIRON=$(MULTIARCH_QEMU_ENVIRON) DOCKER_EXTRA_OPTS=$(DOCKER_EXTRA_OPTS) DEFAULT_CONTAINER_REGISTRY=$(DEFAULT_CONTAINER_REGISTRY) j2 $(SLAVE_DIR)/Dockerfile.j2 > $(SLAVE_DIR)/Dockerfile) $(shell CONFIGURED_ARCH=$(CONFIGURED_ARCH) MULTIARCH_QEMU_ENVIRON=$(MULTIARCH_QEMU_ENVIRON) j2 $(SLAVE_DIR)/Dockerfile.user.j2 > $(SLAVE_DIR)/Dockerfile.user) -$(shell BUILD_SLAVE=y scripts/prepare_docker_buildinfo.sh $(SLAVE_BASE_IMAGE) $(SLAVE_DIR)/Dockerfile $(CONFIGURED_ARCH) "" $(BLDENV)) +$(shell BUILD_SLAVE=y DEFAULT_CONTAINER_REGISTRY=$(DEFAULT_CONTAINER_REGISTRY) scripts/prepare_docker_buildinfo.sh $(SLAVE_BASE_IMAGE) $(SLAVE_DIR)/Dockerfile $(CONFIGURED_ARCH) "" $(BLDENV)) # Add the versions in the tag, if the version change, need to rebuild the slave SLAVE_BASE_TAG = $(shell cat $(SLAVE_DIR)/Dockerfile $(SLAVE_DIR)/buildinfo/versions/versions-* src/sonic-build-hooks/hooks/* | sha1sum | awk '{print substr($$1,0,11);}') @@ -274,6 +278,7 @@ SONIC_BUILD_INSTRUCTION := make \ EXTRA_DOCKER_TARGETS=$(EXTRA_DOCKER_TARGETS) \ BUILD_LOG_TIMESTAMP=$(BUILD_LOG_TIMESTAMP) \ SONIC_ENABLE_IMAGE_SIGNATURE=$(ENABLE_IMAGE_SIGNATURE) \ + SONIC_DEFAULT_CONTAINER_REGISTRY=$(DEFAULT_CONTAINER_REGISTRY) \ SLAVE_DIR=$(SLAVE_DIR) \ $(SONIC_OVERRIDE_BUILD_VARS) diff --git a/dockers/docker-base-buster/Dockerfile.j2 b/dockers/docker-base-buster/Dockerfile.j2 index ac3fd7743e77..e16a0f677b07 100644 --- a/dockers/docker-base-buster/Dockerfile.j2 +++ b/dockers/docker-base-buster/Dockerfile.j2 @@ -1,10 +1,11 @@ +{% set prefix = DEFAULT_CONTAINER_REGISTRY %} {% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %} {% if CONFIGURED_ARCH == "armhf" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:armhf-buster +FROM {{ prefix }}multiarch/debian-debootstrap:armhf-buster {% elif CONFIGURED_ARCH == "arm64" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:arm64-buster +FROM {{ prefix }}multiarch/debian-debootstrap:arm64-buster {% else %} -FROM {{DOCKER_BASE_ARCH}}/debian:buster +FROM {{ prefix }}{{DOCKER_BASE_ARCH}}/debian:buster {% endif %} # Clean documentation in FROM image diff --git a/dockers/docker-base-stretch/Dockerfile.j2 b/dockers/docker-base-stretch/Dockerfile.j2 index b77814bb2820..5603dc502792 100644 --- a/dockers/docker-base-stretch/Dockerfile.j2 +++ b/dockers/docker-base-stretch/Dockerfile.j2 @@ -1,10 +1,11 @@ +{% set prefix = DEFAULT_CONTAINER_REGISTRY %} {% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %} {% if CONFIGURED_ARCH == "armhf" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:armhf-stretch +FROM {{ prefix }}multiarch/debian-debootstrap:armhf-stretch {% elif CONFIGURED_ARCH == "arm64" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:arm64-stretch +FROM {{ prefix }}multiarch/debian-debootstrap:arm64-stretch {% else %} -FROM {{DOCKER_BASE_ARCH}}/debian:stretch +FROM {{ prefix }}{{DOCKER_BASE_ARCH}}/debian:stretch {% endif %} # Clean documentation in FROM image diff --git a/dockers/docker-base/Dockerfile.j2 b/dockers/docker-base/Dockerfile.j2 index 616a4cde7aff..15df3fe8a754 100644 --- a/dockers/docker-base/Dockerfile.j2 +++ b/dockers/docker-base/Dockerfile.j2 @@ -1,9 +1,10 @@ +{% set prefix = DEFAULT_CONTAINER_REGISTRY %} {% if CONFIGURED_ARCH == "armhf" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:armhf-jessie +FROM {{ prefix }}multiarch/debian-debootstrap:armhf-jessie {% elif CONFIGURED_ARCH == "arm64" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:arm64-jessie +FROM {{ prefix }}multiarch/debian-debootstrap:arm64-jessie {% else %} -FROM {{DOCKER_BASE_ARCH}}/debian:jessie +FROM {{ prefix }}{{DOCKER_BASE_ARCH}}/debian:jessie {% endif %} ## Remove retired jessie-updates repo diff --git a/dockers/docker-ptf/Dockerfile.j2 b/dockers/docker-ptf/Dockerfile.j2 index fc87fa9f36f0..a46210c522a8 100644 --- a/dockers/docker-ptf/Dockerfile.j2 +++ b/dockers/docker-ptf/Dockerfile.j2 @@ -1,9 +1,10 @@ +{% set prefix = DEFAULT_CONTAINER_REGISTRY %} {% if CONFIGURED_ARCH == "armhf" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:armhf-stretch +FROM {{ prefix }}multiarch/debian-debootstrap:armhf-stretch {% elif CONFIGURED_ARCH == "arm64" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:arm64-stretch +FROM {{ prefix }}multiarch/debian-debootstrap:arm64-stretch {% else %} -FROM debian:stretch +FROM {{ prefix }}debian:stretch {% endif %} MAINTAINER Pavel Shirshov diff --git a/dockers/docker-sonic-mgmt/Dockerfile.j2 b/dockers/docker-sonic-mgmt/Dockerfile.j2 index fa965ef67c22..563c598b1bdc 100644 --- a/dockers/docker-sonic-mgmt/Dockerfile.j2 +++ b/dockers/docker-sonic-mgmt/Dockerfile.j2 @@ -1,4 +1,5 @@ -FROM ubuntu:18.04 +{% set prefix = DEFAULT_CONTAINER_REGISTRY %} +FROM {{ prefix }}ubuntu:18.04 ENV DEBIAN_FRONTEND=noninteractive diff --git a/rules/config b/rules/config index 07f868605544..539116e3c1f9 100644 --- a/rules/config +++ b/rules/config @@ -194,3 +194,6 @@ REGISTRY_SERVER ?= sonicdev-microsoft.azurecr.io # INCLUDE_MUX - build docker-mux for dual ToR (Gemini) INCLUDE_MUX = y + +# reset default container registry from dockerhub to other +DEFAULT_CONTAINER_REGISTRY ?= diff --git a/scripts/docker_version_control.sh b/scripts/docker_version_control.sh index 595477c859c6..da8cd0484f49 100755 --- a/scripts/docker_version_control.sh +++ b/scripts/docker_version_control.sh @@ -16,6 +16,8 @@ mkdir -p target/versions/default . src/sonic-build-hooks/buildinfo/config/buildinfo.config image_tag=`grep "^FROM " $DOCKERFILE | awk '{print$2}'` +image_tag_noprefix=$image_tag +[ -n "$DEFAULT_CONTAINER_REGISTRY" ] && image_tag_noprefix=$(echo $image_tag | sed "s#$DEFAULT_CONTAINER_REGISTRY##") image=`echo $image_tag | cut -f1 -d:` tag=`echo $image_tag | cut -f2 -d:` @@ -25,7 +27,7 @@ if [[ ",$SONIC_VERSION_CONTROL_COMPONENTS," == *,all,* ]] || [[ ",$SONIC_VERSION exit 0 fi if [ -f $version_file ];then - hash_value=`grep "${ARCH}:${image_tag}" $version_file | awk -F== '{print$2}'` + hash_value=`grep "${ARCH}:${image_tag_noprefix}" $version_file | awk -F== '{print$2}'` fi if [ -z $hash_value ];then hash_value=unknown @@ -43,5 +45,5 @@ else fi fi if [[ "$hash_value" != "unknown" ]];then - echo -e "${ARCH}:${image_tag}==$hash_value" >> $new_version_file + echo -e "${ARCH}:${image_tag_noprefix}==$hash_value" >> $new_version_file fi diff --git a/slave.mk b/slave.mk index 73522008b519..60253d53bf54 100644 --- a/slave.mk +++ b/slave.mk @@ -123,6 +123,8 @@ include $(RULES_PATH)/config export PACKAGE_URL_PREFIX export TRUSTED_GPG_URLS export SONIC_VERSION_CONTROL_COMPONENTS +DEFAULT_CONTAINER_REGISTRY := $(SONIC_DEFAULT_CONTAINER_REGISTRY) +export DEFAULT_CONTAINER_REGISTRY ifeq ($(SONIC_ENABLE_PFCWD_ON_START),y) ENABLE_PFCWD_ON_START = y @@ -281,6 +283,7 @@ $(info "TELEMETRY_WRITABLE" : "$(TELEMETRY_WRITABLE)") $(info "PDDF_SUPPORT" : "$(PDDF_SUPPORT)") $(info "MULTIARCH_QEMU_ENVIRON" : "$(MULTIARCH_QEMU_ENVIRON)") $(info "SONIC_VERSION_CONTROL_COMPONENTS": "$(SONIC_VERSION_CONTROL_COMPONENTS)") +$(info "DEFAULT_CONTAINER_REGISTRY" : "$(SONIC_DEFAULT_CONTAINER_REGISTRY)") $(info ) else $(info SONiC Build System for $(CONFIGURED_PLATFORM):$(CONFIGURED_ARCH)) diff --git a/sonic-slave-buster/Dockerfile.j2 b/sonic-slave-buster/Dockerfile.j2 index 09d50514a043..8851b8f0924d 100644 --- a/sonic-slave-buster/Dockerfile.j2 +++ b/sonic-slave-buster/Dockerfile.j2 @@ -1,11 +1,12 @@ +{% set prefix = DEFAULT_CONTAINER_REGISTRY %} {%- if CONFIGURED_ARCH == "armhf" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/qemu-user-static:x86_64-arm-5.0.0-2 as qemu -FROM multiarch/debian-debootstrap:armhf-buster +FROM {{ prefix }}multiarch/qemu-user-static:x86_64-arm-5.0.0-2 as qemu +FROM {{ prefix }}multiarch/debian-debootstrap:armhf-buster COPY --from=qemu /usr/bin/qemu-arm-static /usr/bin {%- elif CONFIGURED_ARCH == "arm64" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:arm64-buster +FROM {{ prefix }}multiarch/debian-debootstrap:arm64-buster {%- else -%} -FROM debian:buster +FROM {{ prefix }}debian:buster {%- endif %} MAINTAINER gulv@microsoft.com diff --git a/sonic-slave-jessie/Dockerfile.j2 b/sonic-slave-jessie/Dockerfile.j2 index 3c3e773ed2f9..8d58b4199a09 100644 --- a/sonic-slave-jessie/Dockerfile.j2 +++ b/sonic-slave-jessie/Dockerfile.j2 @@ -1,9 +1,10 @@ +{% set prefix = DEFAULT_CONTAINER_REGISTRY %} {%- if CONFIGURED_ARCH == "armhf" -%} -FROM multiarch/debian-debootstrap:armhf-jessie +FROM {{ prefix }}multiarch/debian-debootstrap:armhf-jessie {%- elif CONFIGURED_ARCH == "arm64" -%} -FROM multiarch/debian-debootstrap:arm64-jessie +FROM {{ prefix }}multiarch/debian-debootstrap:arm64-jessie {%- else -%} -FROM debian:jessie +FROM {{ prefix }}debian:jessie {%- endif %} MAINTAINER johnar@microsoft.com diff --git a/sonic-slave-stretch/Dockerfile.j2 b/sonic-slave-stretch/Dockerfile.j2 index 1cef023b7a04..9a9e9dca9d2a 100644 --- a/sonic-slave-stretch/Dockerfile.j2 +++ b/sonic-slave-stretch/Dockerfile.j2 @@ -1,9 +1,10 @@ +{% set prefix = DEFAULT_CONTAINER_REGISTRY %} {%- if CONFIGURED_ARCH == "armhf" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:armhf-stretch +FROM {{ prefix }}multiarch/debian-debootstrap:armhf-stretch {%- elif CONFIGURED_ARCH == "arm64" and MULTIARCH_QEMU_ENVIRON == "y" %} -FROM multiarch/debian-debootstrap:arm64-stretch +FROM {{ prefix }}multiarch/debian-debootstrap:arm64-stretch {%- else -%} -FROM debian:stretch +FROM {{ prefix }}debian:stretch {%- endif %} MAINTAINER gulv@microsoft.com