Created
July 2, 2021 17:54
-
-
Save phistrom/ca09772dc810e3b2a31ad7933e717781 to your computer and use it in GitHub Desktop.
docker pull different architecture image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: pull-arch.sh IMAGE:TAG ARCHITECTURE | |
# Example: | |
# You're on an amd64 system, but you want the ARM version (arm64) of a the | |
# python:3 image for whatever reason: | |
# `pull-arch.sh python:3 arm64` | |
# would pull the arm64 version of the python:3 image from Dockerhub | |
# and tag it on your machine as python:3-arm64 | |
set -e | |
IMAGE="$1" | |
ARCH="$2" | |
# Image without label like python instead of python:3 | |
BARE_IMAGE=$(sed -e 's/:.*//' <<< "${IMAGE}") | |
# Add a :latest label if no label was given in the input | |
if [[ "${IMAGE}" != *":"* ]]; then | |
IMAGE="${IMAGE}:latest" | |
fi | |
# Get the SHA256 digest of the image to pull for the specified architecture | |
DIGEST=$(docker manifest inspect "${IMAGE}" | \ | |
jq -r '.manifests| .[] | select(.platform.architecture=="'"${ARCH}"'") | .digest') | |
# Pull the image by its digest | |
docker pull "${BARE_IMAGE}@${DIGEST}" | |
# Find the image ID of the image we just pulled | |
IMAGE_ID=$(docker inspect "${BARE_IMAGE}@${DIGEST}" | jq -r '.[0].Id') | |
# Tag the image with -ARCH on the end. | |
# For example, if we were given the arguments python:3 arm64 | |
# this image would be tagged python:3-arm64 | |
docker tag "${IMAGE_ID}" "${IMAGE}-${ARCH}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment