Skip to content

Instantly share code, notes, and snippets.

@funnyzak
Last active April 11, 2024 14:17
Show Gist options
  • Save funnyzak/d7bb8d7d403dd79a505fbd62b3916a69 to your computer and use it in GitHub Desktop.
Save funnyzak/d7bb8d7d403dd79a505fbd62b3916a69 to your computer and use it in GitHub Desktop.
It modifies the Docker data folder to a new location. It is useful if you want to move the Docker data folder to a different drive or partition.
#!/bin/bash
# It modifies the Docker data folder to a new location. It is useful if you want to move the Docker data folder to a different drive or partition.
# Example usage:
# ./modify_docker_data_folder.sh /mnt/docker_data
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
export PATH=$PATH:/usr/local/bin
# Check if Docker is installed
command -v docker >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "${red}Docker is not installed. Please install Docker first.${plain}"
exit 1
fi
# Get the new data directory from the command line argument
new_dir=$1
# Get the current Docker storage directory
current_dir=$(docker info --format='{{json .DockerRootDir}}')
# Remove the quotes from the current directory
current_dir=$(echo $current_dir | tr -d '"')
# If no new data directory is provided, prompt the user for one
if [[ -z "${new_dir}" ]]; then
read -ep "Enter the absolute path to the new Docker data directory (current: ${current_dir}): " new_dir
fi
# Check if the new data directory is empty
if [[ -z "${new_dir}" ]]; then
echo -e "${red}You did not enter a new data directory path.${plain}"
exit 1
fi
# if the new data directory is the same as the current one, exit
if [[ "${new_dir}" == "${current_dir}" ]]; then
echo -e "${yellow}The new data directory is the same as the current one. Exiting.${plain}"
exit 0
fi
# if the new data directory exists, exit
if [[ -d "${new_dir}" ]]; then
echo -e "${red}The new data directory already exists. Exiting.${plain}"
exit 1
fi
# Stop the Docker service
systemctl stop docker > /dev/null 2>&1
echo -e "${green}Docker service stopped${plain}"
# check and set "data-root" to /etc/docker/daemon.json
if [[ ! -f /etc/docker/daemon.json ]]; then
# create /etc/docker/daemon.json
echo "{\"data-root\": \"${new_dir}\"}" > /etc/docker/daemon.json
elif [[ $(grep -c "data-root" /etc/docker/daemon.json) -eq 0 ]]; then
# add "data-root" to /etc/docker/daemon.json
sed -i '1s/{/{\n "data-root": "'${new_dir}'",/' /etc/docker/daemon.json
else
echo -e "${yellow}Replacing data-root in /etc/docker/daemon.json${plain}"
sed -i "s|\"data-root\": \".*\"|\"data-root\": \"${new_dir}\"|g" /etc/docker/daemon.json
fi
# Copy the existing data to the new directory
mkdir -p $new_dir
echo -e "${green}Copying existing data to the new directory${plain}"
rsync -aP $current_dir/* $new_dir > /dev/null 2>&1
echo -e "${green}Copying completed${plain}"
# Reload the service
systemctl daemon-reload
# Start Docker
systemctl start docker > /dev/null 2>&1
echo -e "${green}Docker service started${plain}"
# Wait for Docker to start
sleep 5
# Print the old and new directories
echo -e "Docker data directory has been changed from ${current_dir} to ${new_dir}"
# Check if the storage location has been successfully changed
echo -e "${green}Checking the new Docker data directory${plain} ..."
docker info --format='{{.DockerRootDir}}'
echo -e "${green}Checking completed${plain}"
echo -e "${green}Modify Docker data folder completed${plain}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment