Skip to content

Instantly share code, notes, and snippets.

@shantanoo-desai
Created July 1, 2022 08:57
Show Gist options
  • Save shantanoo-desai/84b292b101d4d431ef1ceb33ee9928b3 to your computer and use it in GitHub Desktop.
Save shantanoo-desai/84b292b101d4d431ef1ceb33ee9928b3 to your computer and use it in GitHub Desktop.
Bash Script to add a Private Image Registry to your Local Machine with Docker
#!/bin/env bash
# Bash script to insert a private registry as "insecure-registries" for Docker Daemon
# as well as configure the Docker Config File to store the credentials locally
# Version: 0.1.0
# Authors: Shan Desai <[email protected]>
set -e
JQ=`which jq`
DOCKER=`which docker`
SPONGE=`which sponge`
ARTIFACT_REGISTRY=<ARTIFACT_URL_HERE>
DAEMONDIR=/etc/docker
DAEMONFILE=daemon.json
CONFIGDIR=$HOME/.docker
CONFIGFILE=config.json
### Check if either jq, sponge docker are not installed on the machine..
function deps_check {
echo -e "\e[32m[INFO]\e[0m Checking Dependencies needed for script execution"
if [[ -z $JQ || -z $DOCKER || -z $SPONGE ]];
then
echo -e "\e[31m[ERROR]\e[0m This script requires jq, sponge and Docker CLI to be installed.."
echo -e "\e[31m[ERROR]\e[0m - Please install jq (Ubuntu/Debian: apt-get install jq)"
echo -e "\e[31m[ERROR]\e[0m - Please install Docker Engine / CLI by referring to Docker Docs"
echo -e "\e[31m[ERROR]\e[0m - Please install sponge (Ubuntu/Debian: apt-get install moreutils"
exit 1
fi
}
### Check if the repository, is reachable via the network
function connectivity_check {
echo -e "\e[32m[INFO]\e[0m Checking Connectivity to ${ARTIFACT_REGISTRY} .."
CONNECTIVITYRESP=$(ping -c 3 -q $ARTIFACT_REGISTRY > /dev/null 2>&1)
if [[ $CONNECTIVITYRESP -ne 0 ]];
then
echo -e "\e[31m[ERROR]\e[0m Cannot reach Registry, please check network"
exit 2
else
echo -e "\e[32m[INFO]\e[0m Registry reachable.."
fi
}
function daemon_config {
echo -e "\e[32m[INFO]\e[0m Checking for Docker Daemon JSON file on machine"
if [ ! -d "$DAEMONDIR" ]; then
echo -e "\e[32m[INFO]\e[0m Daemon File Does not Exist.."
echo -e "\e[32m[INFO]\e[0m Creating file in ${DAEMONDIR}"
mkdir $DAEMONDIR
touch $DAEMONDIR/$DAEMONFILE
echo '{}' | $JQ --arg repo "$ARTIFACT_REGISTRY" '{"insecure-registries": [$repo]}' | $SPONGE $DAEMONDIR/$DAEMONFILE
elif [ -f "$DAEMONDIR/$DAEMONFILE" ]; then
echo -e "\e[32m[INFO]\e[0m Docker Daemon File exists"
echo -e "\e[32m[INFO]\e[0m Updating Docker Daemon file's Registry entry.."
if [ -s "$DAEMONDIR/$DAEMONFILE" ]; then
# Check if file is not empty
echo -e "\e[35m[DEBUG]\e[0m Non-Empty Daemon File"
# If the file already has other insecure registries, DO NOT overwrite the settings
# append the artifacts registry to the array of 'insecure-registries' if the key exists
# if the key does not exist then create one and add the value of the registry url
$JQ --arg repo "$ARTIFACT_REGISTRY" '
if ."insecure-registries" then
if ."insecure-registries" | index($repo) | not then
."insecure-registries"[."insecure-registries" | length] += $repo
else
.
end
else ."insecure-registries" += [$repo] end' $DAEMONDIR/$DAEMONFILE | $SPONGE $DAEMONDIR/$DAEMONFILE
else
# file exists but it is empty
echo -e "\e[35m[DEBUG]\e[0m Empty Daemon File"
echo '{}' | $JQ --arg repo "$ARTIFACT_REGISTRY" '
if ."insecure-registries" then
if ."insecure-registries" | index($repo) | not then
."insecure-registries"[."insecure-registries" | length] += $repo
else
.
end
else ."insecure-registries" += [$repo] end'| $SPONGE $DAEMONDIR/$DAEMONFILE
fi
fi
}
function docker_config {
echo -e "\e[32m[INFO]\e[0m Checking for Docker Config JSON file on machine"
if [ ! -d "$CONFIGDIR" ]; then
echo -e "\n\e[32m[INFO]\e[0m No Docker Config Directory found. Creating Config file.."
mkdir $CONFIGDIR
touch $CONFIGDIR/$CONFIGFILE
echo '{}' | $JQ --arg repo "$ARTIFACT_REGISTRY" --arg pwd "$password" '.auths[$repo]["auth"]=$pwd' | $SPONGE $CONFIGDIR/$CONFIGFILE
elif [ ! -f "$CONFIGDIR/$CONFIGFILE" ]; then
# if directory exists but not the file
touch $CONFIGDIR/$CONFIGFILE
echo '{}' | $JQ --arg repo "$ARTIFACT_REGISTRY" --arg pwd "$password" '.auths[$repo]["auth"]=$pwd' | $SPONGE $CONFIGDIR/$CONFIGFILE
else
echo -e "\n\e[32m[INFO]\e[0m Updating existing Config File.."
if [[ -s $CONFIGDIR/$CONFIGFILE ]]; then
# if file is not empty
$JQ --arg repo "$ARTIFACT_REGISTRY" --arg pwd "$password" '.auths[$repo]["auth"]=$pwd' $CONFIGDIR/$CONFIGFILE | $SPONGE $CONFIGDIR/$CONFIGFILE
else
# If file exists previously, but is empty
echo -e "\e[35m[DEBUG]\e[0m Empty Config File"
echo '{}' | $JQ --arg repo "$ARTIFACT_REGISTRY" --arg pwd "$password" '.auths[$repo]["auth"]=$pwd' | $SPONGE $CONFIGDIR/$CONFIGFILE
fi
fi
}
# Obtain credentials for Artifact Registry
function credentials_registry {
echo -e "\e[32m[INFO]\e[0m Enter your Emerson E-mail Address for Login Credentials"
read -p "Enter E-mail Address: " email_address
echo -e "\e[32m[INFO]\e[0m Enter your API Token from Registry"
echo -e "\e[33m[NOTE]\e[0m When Pasting the TOKEN as PASSWORD, the prompt will NOT display anything.."
echo -e "\e[33m[NOTE]\e[0m - After Pasting the TOKEN as PASSWORD, press ENTER.."
read -sp "Enter Password (Token): " password
}
## Step 0: Check for Dependencies
deps_check
## Step 1: Check for Connectivity to Registry
connectivity_check
## Step 2:
credentials_registry
## Step 3: Docker Daemon JSON File
daemon_config
## Step 4: Docker Config JSON File
docker_config
echo -e "\n\e[32m[INFO]\e[0m Docker Daemon needs to restart now.. You might need SuperUser Rights."
systemctl restart docker
echo -e "\e[32m[INFO]\e[0m Logging into Registry using Docker CLI"
$DOCKER login "$ARTIFACT_REGISTRY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment