Skip to content

Instantly share code, notes, and snippets.

@strongjz
Forked from piersharding/docker-credentials.sh
Created September 4, 2020 19:19
Show Gist options
  • Save strongjz/723d083404fc68d99030b409be2a2041 to your computer and use it in GitHub Desktop.
Save strongjz/723d083404fc68d99030b409be2a2041 to your computer and use it in GitHub Desktop.
Create Docker config.json file credentials
#!/bin/bash
# This script creates a docker config.json file with the auth section
# as an example of what can be passed into GitLab-CI and used in
# conjunction with DOCKER_CONFIG - the config file directory location.
# command line parameter default values
DOCKER_REGISTRY=""
DOCKER_USER=""
DOCKER_PASSWORD=""
WRITE="no"
function parse_params() {
while getopts r:u:p:hw arg; do
CLEAN_ARG=`echo ${OPTARG} | tr -d '[:cntrl:]'`
case $arg in
h)
echo "./${0} [-r <Docker registry: eg: docker.io> -u <Docker User: eg: ${USER}@some.where> -p <Docker Password: eg: secret> -w <actually write file>]"
exit
;;
r)
DOCKER_REGISTRY="${CLEAN_ARG}"
;;
u)
DOCKER_USER="${CLEAN_ARG}"
;;
p)
DOCKER_PASSWORD="${CLEAN_ARG}"
;;
w)
WRITE="yes"
;;
esac
done
}
# parse command line parameters if we have some
if [ $# -ge 1 ]; then
parse_params $@;
fi
# must be either - not both
if [ "${INSERT}" = "yes" ] && [ "${DELETE}" = "yes" ]; then
echo "Please select -i (inset xhost entries) or -d (delete xhost entries) - not both"
exit 1
fi
# if neither then effectively noop mode
if [ "${INSERT}" = "no" ] && [ "${DELETE}" = "no" ]; then
echo "No update selected (insert or delete), noop mode only"
fi
# no network specified then try auto-discovery
if [ -z "${DOCKER_REGISTRY}" ] || [ -z "${DOCKER_USER}" ] || [ -z "${DOCKER_PASSWORD}" ]; then
echo "Please specify registry (-r), user (-u), and password (-p)"
exit 1
fi
PASSWD=`echo "${DOCKER_USER}:${DOCKER_PASSWORD}" | tr -d '\n' | base64 -i -w 0`
echo "Selected registry ${DOCKER_REGISTRY} and user ${DOCKER_USER} (base64 encoded credentials: ${PASSWD})"
CONFIG="\
{\n
\"auths\": {\n
\"docker.io\": {\n
\"auth\": \"${PASSWD}\"\n
}\n
}\n
}\n"
# check for noop
if [ "${WRITE}" = "yes" ]; then
echo "Writing to config.json"
printf "${CONFIG}" > config.json
fi
echo "Docker config:"
printf "${CONFIG}"
echo "Docker config base64 endcoded:"
printf "${CONFIG}" | base64 -w 0
echo ""
exit 0
@magnetik
Copy link

There is a little missing "docker.io" hardcoded in the docker config output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment