Last active
May 3, 2016 09:44
-
-
Save xmik/a745ea1c9720ddbc77acc56fca0bdff4 to your computer and use it in GitHub Desktop.
Migrate docker images from docker registry v1 to docker registry v2
This file contains hidden or 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 | |
| # This script gets a list of all docker images from a docker registry v1 | |
| # and pulls each image. Set the DOCKER_REGISTRY_NO_HTTP variable below. | |
| # You may need to run `docker pull` command with `sudo`. | |
| # If you want to use it seeing the output in terminal and saving the output to file: | |
| # pull_images_from_v1.sh 2>&1 | tee ~/pull_output.txt | |
| DOCKER_REGISTRY_NO_HTTP="TODO!!" | |
| DOCKER_REGISTRY="http://${DOCKER_REGISTRY_NO_HTTP}" | |
| # download jq - JSON parser for bash | |
| wget https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 && chmod 755 ./jq-linux64 | |
| images_json=$(curl --silent $DOCKER_REGISTRY/v1/search) | |
| images=$(echo $images_json | ./jq-linux64 '.results[] | .name') | |
| for image in $images ; do | |
| # image is 1 string e.g. | |
| # "library/grafana" | |
| # "xmik/my-image" | |
| # remove the quotes (http://stackoverflow.com/questions/9733338/shell-script-remove-first-and-last-quote-from-a-variable ) | |
| image_no_quotes=$(echo "$image" | sed -e 's/^"//' -e 's/"$//') | |
| # get rid of the "library/" prefix | |
| if [[ "$image_no_quotes" == "library/"* ]];then | |
| image_name="${image_no_quotes#library/}" | |
| else | |
| image_name="$image_no_quotes" | |
| fi | |
| # get all the tags for each image | |
| # tags_json are 1 json object e.g.: | |
| # {"0.0.1": "1014df81fb533d4a92faf0ce3f0f723223b78d9fbad5edbce083cf4ea799903b", | |
| # "22ac6a1307ceba9f0784e6c7fac72e5a630f0465": "035e5599c3e5900bbf07a57ccceeab6881b90dcb5e4d06944ca61f876f2e12b2", | |
| # "latest": "72a1fba053f80f97ea1f5c8e21cf5615bd040f8afd81ddc440dda5cb58c3275b"} | |
| tags_json=$(curl --silent $DOCKER_REGISTRY/v1/repositories/$image_name/tags) | |
| # tags_names are an array e.g.: | |
| # "0.0.1" | |
| # "0.0.2" | |
| tags_names=$(echo $tags_json | ./jq-linux64 'keys[]') | |
| for tag_name in $tags_names ; do | |
| # remove the quotes (http://stackoverflow.com/questions/9733338/shell-script-remove-first-and-last-quote-from-a-variable ) | |
| tag_no_quotes=$(echo "$tag_name" | sed -e 's/^"//' -e 's/"$//') | |
| tag_name_length=$(echo $tag_no_quotes | wc -c) | |
| echo "Will pull: ${DOCKER_REGISTRY_NO_HTTP}/${image_name}:${tag_no_quotes}" | |
| docker pull "${DOCKER_REGISTRY_NO_HTTP}/${image_name}:${tag_no_quotes}" | |
| done | |
| done |
This file contains hidden or 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 | |
| # This script gets a list of all local docker images which names start with | |
| # a specified registry endpoint and pushes each image to the registry (v1 or v2). | |
| # It assumes that the registry endpoint you pulled from and you want to | |
| # push to is the same. Set the DOCKER_REGISTRY_NO_HTTP variable below. | |
| # You may need to run `docker push` command with `sudo`. | |
| # If you want to use it seeing the output in terminal and saving the output to file: | |
| # push_images.sh 2>&1 | tee ~/push_output.txt | |
| DOCKER_REGISTRY_NO_HTTP="TODO!!" | |
| # Get all local docker images that start with the registry name: | |
| # get the 1st and 2nd column of `docker images` command output and use | |
| # as output separator the ":" | |
| local_images=$(docker images | awk 'BEGIN {OFS=":";} {print $1,$2;}') | |
| for local_image in $local_images ; do | |
| if [[ "$local_image" == ${DOCKER_REGISTRY_NO_HTTP}* ]];then | |
| echo "Will push: ${local_image}" | |
| docker push "${local_image}" | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment