Last active
August 14, 2017 17:19
-
-
Save elyscape/0b6acb09842260cffd70d37a96cc4478 to your computer and use it in GitHub Desktop.
Bash script to allow you to use `FROM image` in Dockerfiles even when using a private registry
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
#!/usr/bin/env bash | |
readonly _skip_examples="local-image-skip-examples" | |
usage() { | |
local scriptname | |
scriptname="$(basename "$0")" | |
cat <<-EOF | |
Usage: ${scriptname} command | |
Commands: | |
fetch IMAGE REGISTRY Fetch a local-tagged copy of IMAGE from REGISTRY | |
push IMAGE REGISTRY Push IMAGE to REGISTRY | |
clean local IMAGE Remove a local-tagged copy of IMAGE | |
clean remote IMAGE REGISTRY Remove a REGISTRY-tagged copy of IMAGE | |
clean both IMAGE REGISTRY Remove both local- and REGISTRY-tagged IMAGE | |
EOF | |
[[ "$1" == "$_skip_examples" ]] && return | |
cat <<-EOF | |
Examples: | |
${scriptname} fetch company/service:2.5 docker.example.com | |
Pull docker.example.com/company/service:2.5 and tag it locally as | |
company/service:2.5. | |
${scriptname} push company/service:2.5 docker.example.com | |
Tag company/service:2.5 as docker.example.com/company/service:2.5 | |
and push it to the registry. | |
${scriptname} clean local company/service:2.5 | |
Remove company/service:2.5 from the local system. | |
${scriptname} clean remote company/service:2.5 docker.example.com | |
Remove docker.example.com/company/service:2.5 from the local system. | |
${scriptname} clean both company/service:2.5 docker.example.com | |
Remove docker.example.com/company/service:2.5 and company/service:2.5 | |
from the local system. | |
EOF | |
} | |
_retag() { | |
if [[ "$#" -ne 2 ]]; then | |
echo "${FUNCNAME[0]}: Expected 2 arguments, got ${#}." >&2 | |
return 1 | |
fi | |
declare source="$1" dest="$2" | |
echo "Tagging ${source} as ${dest}..." | |
docker tag "$source" "$dest" | |
} | |
fetch() { | |
if [[ "$#" -ne 2 ]]; then | |
echo "${FUNCNAME[0]}: Expected 2 arguments, got ${#}." >&2 | |
usage "$_skip_examples" | |
return 1 | |
fi | |
declare image="$1" registry="$2" | |
local remoteimage="${registry}/${image}" | |
echo "Pulling ${remoteimage}..." | |
docker pull "$remoteimage" | |
echo | |
_retag "$remoteimage" "$image" | |
} | |
push() { | |
if [[ "$#" -ne 2 ]]; then | |
echo "${FUNCNAME[0]}: Expected 2 arguments, got ${#}." >&2 | |
usage "$_skip_examples" | |
return 1 | |
fi | |
declare image="$1" registry="$2" | |
local remoteimage="${registry}/${image}" | |
_retag "$image" "$remoteimage" | |
echo | |
echo "Pushing ${remoteimage}..." | |
docker push "$remoteimage" | |
} | |
clean() { | |
case "$1" in | |
local) shift; _clean_local "$@";; | |
remote) shift; _clean_remote "$@";; | |
both) | |
shift | |
_clean_local "$@" | |
_clean_remote "$@" | |
;; | |
*) usage;; | |
esac | |
} | |
_clean_local() { | |
if [[ "$#" -lt 1 ]] || [[ "$#" -gt 2 ]]; then | |
echo "${FUNCNAME[0]}: Expected 1 argument, got ${#}." >&2 | |
usage | |
return 1 | |
fi | |
declare image="$1" | |
docker rmi "$image" || true | |
} | |
_clean_remote() { | |
if [[ "$#" -ne 2 ]]; then | |
echo "${FUNCNAME[0]}: Expected 2 arguments, got ${#}." >&2 | |
usage | |
return 1 | |
fi | |
declare image="$1" registry="$2" | |
local remoteimage="${registry}/${image}" | |
docker rmi "$remoteimage" || true | |
} | |
main() { | |
set -eo pipefail; [[ -n "$TRACE" ]] && set -x | |
case "$1" in | |
fetch) shift; fetch "$@";; | |
push) shift; push "$@";; | |
clean) shift; clean "$@";; | |
*) usage;; | |
esac | |
} | |
[[ "$0" == "${BASH_SOURCE[0]}" ]] && main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment