From e2d51191a3aae39339e3b5f7674612c19e30cd1b Mon Sep 17 00:00:00 2001 From: Filippo Brizzi Date: Wed, 31 Jan 2024 17:16:51 +0000 Subject: [PATCH] improved docker build --- .github/workflows/build.yml | 4 +--- docker/build.sh | 27 ++++++++++++++++++++++++--- docker/build_deps.sh | 30 +++++++++++++++++++++++------- docker/publish.sh | 7 ------- docker/publish_deps.sh | 9 --------- 5 files changed, 48 insertions(+), 29 deletions(-) delete mode 100755 docker/publish.sh delete mode 100755 docker/publish_deps.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 06d771de..7f2149da 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,7 +34,6 @@ jobs: run: | cd ${{github.workspace}}/docker ./build.sh - ./publish.sh docker_deps_build_and_publish: needs: docker_build_and_publish runs-on: ubuntu-latest @@ -56,8 +55,7 @@ jobs: if: steps.filter.outputs.external == 'true' run: | cd ${{github.workspace}}/docker - ./build_dev.sh - ./publish_dev.sh + ./build_deps.sh build: needs: docker_deps_build_and_publish # TODO: use the docker image published at the step over diff --git a/docker/build.sh b/docker/build.sh index dac76a38..d40cd4c4 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -2,16 +2,37 @@ source ./version.sh +HOST_ARCH=$(uname -m) +ARCH=${1:-${HOST_ARCH}} -ARCH=$(uname -m) +if [ "${ARCH}" == "aarch64" ]; then + ARCH = "arm64" +fi if [ "${ARCH}" == "x86_64" ]; then # see https://hub.docker.com/r/nvidia/opengl/tags BASE_IMAGE="nvidia/opengl:1.0-glvnd-runtime-ubuntu22.04" -else +elif ["${ARCH}" == "arm64" ]; then BASE_IMAGE="arm64v8/ubuntu:22.04" +else + echo "Supported arch value: arm64, x86_64" + exit 1 fi ncores=$(cat /proc/cpuinfo | grep processor | wc -l) -docker build -t ${IMAGE}:${VERSION} -f Dockerfile --cpuset-cpus "0-$ncores" --build-arg BASE_IMAGE=${BASE_IMAGE} . --tag ${IMAGE}:latest +IMAGE_NAME="ghcr.io/filippobrizzi/${ARC}/${IMAGE}" + +function docker_tag_exists() { + docker manifest inspect ${IMAGE_NAME}:${VERSION} > /dev/null +} + +if docker_tag_exists; then + echo "Image already exists, pulling it" + docker pull ${IMAGE_NAME}:${VERSION} +else + echo "Building image: ${IMAGE_NAME}:${VERSION}" + docker build -t ${IMAGE_NAME}:${VERSION} -f Dockerfile --cpuset-cpus "0-$ncores" --build-arg BASE_IMAGE=${BASE_IMAGE} . --tag ${IMAGE}:latest + docker push ${IMAGE_NAME}:${VERSION} + docker push ${IMAGE_NAME}:latest +fi diff --git a/docker/build_deps.sh b/docker/build_deps.sh index 53ff9fe2..0e13b7cc 100755 --- a/docker/build_deps.sh +++ b/docker/build_deps.sh @@ -2,15 +2,31 @@ source ./version.sh -ncores=$(cat /proc/cpuinfo | grep processor | wc -l) -((ncores--)) +HOST_ARCH=$(uname -m) +ARCH=${1:-${HOST_ARCH}} +if [ "${ARCH}" == "aarch64" ]; then + ARCH = "arm64" +fi + +BASE_IMAGE=ghcr.io/filippobrizzi/${ARCH}/${IMAGE}:${VERSION} +docker pull ${BASE_IMAGE} SUFFIX=deps +IMAGE_NAME="ghcr.io/filippobrizzi/${ARC}/${IMAGE}_${SUFFIX}" -pushd ../ +function docker_tag_exists() { + docker manifest inspect ${IMAGE_NAME}:${VERSION} > /dev/null +} -BASE_IMAGE=ghcr.io/filippobrizzi/${IMAGE}:${VERSION} -docker pull ${BASE_IMAGE} -docker build . -t ${IMAGE}_${SUFFIX}:${VERSION} -f docker/Dockerfile_deps --cpuset-cpus "0-$ncores" --build-arg BASE_IMAGE=${BASE_IMAGE} --tag ${IMAGE}_${SUFFIX}:latest +if docker_tag_exists; then + echo "Image already exists, pulling it: ${IMAGE_NAME}:${VERSION}" + docker pull ${IMAGE_NAME}:${VERSION} +else + echo "Building image: ${IMAGE_NAME}:${VERSION}" + pushd ../ + docker build . -t ${IMAGE_NAME}:${VERSION} -f docker/Dockerfile_deps --cpuset-cpus "0-$ncores" --build-arg BASE_IMAGE=${BASE_IMAGE} --tag ${IMAGE_NAME}_${SUFFIX}:latest + popd -popd + docker push ${IMAGE_NAME}:${VERSION} + docker push ${IMAGE_NAME}:latest +fi diff --git a/docker/publish.sh b/docker/publish.sh deleted file mode 100755 index de13fab5..00000000 --- a/docker/publish.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -source ./version.sh - -docker image tag ${IMAGE}:${VERSION} ghcr.io/filippobrizzi/${IMAGE}:${VERSION} -docker push ghcr.io/filippobrizzi/${IMAGE}:${VERSION} -docker image tag ghcr.io/filippobrizzi/${IMAGE}:${VERSION} ghcr.io/filippobrizzi/${IMAGE}:latest diff --git a/docker/publish_deps.sh b/docker/publish_deps.sh deleted file mode 100755 index e78d84c1..00000000 --- a/docker/publish_deps.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -source ./version.sh - -SUFFIX=deps - -docker image tag ${IMAGE}_deps:${VERSION} ghcr.io/filippobrizzi/${IMAGE}_deps:${VERSION} -docker push ghcr.io/filippobrizzi/${IMAGE}_${SUFFIX}:${VERSION} -docker image tag ghcr.io/filippobrizzi/${IMAGE}_${SUFFIX}:${VERSION} ghcr.io/filippobrizzi/${IMAGE}_${SUFFIX}:latest