Skip to content

Instantly share code, notes, and snippets.

@Sugavanas
Last active November 13, 2021 00:08
Show Gist options
  • Save Sugavanas/16a7d8b90debf475d22c6c50f3e2eb13 to your computer and use it in GitHub Desktop.
Save Sugavanas/16a7d8b90debf475d22c6c50f3e2eb13 to your computer and use it in GitHub Desktop.
Change Docker Root Dir Path in Linux
#!/bin/bash
CURRENTDOCKERROOTDIR=/var/lib/docker
NEWDOCKERROOTDIR=
COPYFOLDER=false
DONTSTARTDOCKERAFTERCHANGE=false
FORCEOVERWRITE=false
JSONDAEMONFILEPATH=/etc/docker/daemon.json
STORAGEDRIVER=
usage="$(basename "$0") -o path [-h] [-i /current/root/dir] [-p /path/to/json/daemonfile] [-S storage-driver for docker] [-c] [-n] [-f]
where:
-o New Docker Root Dir path
-h show this help text
-i Current docker root dir (default: $CURRENTDOCKERROOTDIR)
-p Path to json daemon file (default: $JSONDAEMONFILEPATH)
-S Docker storage driver to use (default: none)
-c Copy old docker folder to new path and set permissions (default: false)
-n Don't Start docker after script ends (default: false)
-f Force overwrite daemon file (default: false)"
while getopts "i:o:p:S:cnfh" opt; do
case $opt in
i) CURRENTDOCKERROOTDIR="$OPTARG"
;;
o) NEWDOCKERROOTDIR="$OPTARG"
;;
c) COPYFOLDER=true
;;
n) DONTSTARTDOCKERAFTERCHANGE=true
;;
f) FORCEOVERWRITE=true
;;
p) JSONDAEMONFILEPATH="$OPTARG"
;;
S) STORAGEDRIVER="$OPTARG"
;;
h) echo "$usage"
exit
;;
\?) echo "Invalid option -$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
case $opt in
i|o|p|S)
case $OPTARG in
-*) echo "Option $opt needs a valid argument"
echo "$usage" >&2
exit 1
;;
esac
;;
esac
done
if [ -z "$NEWDOCKERROOTDIR" ]; then
echo "No new dir supplied supplied. Check usage with script.sh -h"
echo "$usage" >&2
exit 1
fi
DOCKERFOLDER="$NEWDOCKERROOTDIR/docker"
if [ $(/usr/bin/id -u) -ne 0 ]; then
echo "Not running as root"
exit 1
fi
if [[ -f "$JSONDAEMONFILEPATH" && "$FORCEOVERWRITE" = false ]]; then
echo "$JSONDAEMONFILEPATH already exists. Please delete it first, or manually add the root path. Pass -f to force overwrite."
exit 1
else
echo "$JSONDAEMONFILEPATH already exists but -f flag is true. Will Overwrite. Old Contents are printed below."
echo "=========================================================="
echo "$(<$JSONDAEMONFILEPATH)"
echo "=========================================================="
fi
systemctl stop docker
systemctl stop docker.socket
if [ "$COPYFOLDER" = true ]; then
mkdir -p "$NEWDOCKERROOTDIR"
chown -R root:root $NEWDOCKERROOTDIR
mkdir "$DOCKERFOLDER"
rsync -a "$CURRENTDOCKERROOTDIR" "$DOCKERFOLDER/"
fi;
if [ -z "$STORAGEDRIVER" ]; then
echo "{\"data-root\": \"$DOCKERFOLDER\"}" > "$JSONDAEMONFILEPATH"
else
echo "{\"data-root\": \"$DOCKERFOLDER\",\"storage-driver\": \"$STORAGEDRIVER\"}" > "$JSONDAEMONFILEPATH"
fi;
if [ "$DONTSTARTDOCKERAFTERCHANGE" = false ]; then
systemctl daemon-reload
systemctl start docker
systemctl start docker.socket
systemctl is-active --quiet docker && SUCCESS=true || SUCCESS=false
if [ "$SUCCESS" = true ]; then
echo "=========================================================="
echo "Successfully updated (maybe). Check below"
echo "=========================================================="
docker info | grep 'Docker Root Dir'
exit 0;
fi;
echo "Error starting docker. Check status below."
systemctl status docker --no-pager
fi;
echo ""
echo "Done! If there's any issues with docker starting, delete (or replace with contents printed above) $JSONDAEMONFILEPATH file."
echo "Running \"dockerd\" command might help"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment