Skip to content

Instantly share code, notes, and snippets.

@mazzma12
Last active June 20, 2024 14:38
Show Gist options
  • Save mazzma12/deb645f5376203f1e78180ff73f23ccd to your computer and use it in GitHub Desktop.
Save mazzma12/deb645f5376203f1e78180ff73f23ccd to your computer and use it in GitHub Desktop.
Change docker cache directory and restart
#!/bin/bash
# Default Docker cache directory
DEFAULT_DOCKER_CACHE_DIR="/home/ec2-user/SageMaker/.cache/docker"
# Use the first argument as the Docker cache directory if provided, otherwise use the default
DOCKER_CACHE_DIR="${1:-$DEFAULT_DOCKER_CACHE_DIR}"
# Create the Docker cache directory if it does not exist
mkdir -p "$DOCKER_CACHE_DIR"
# Change the owner to root and set permissions
sudo chown -R root:root "$DOCKER_CACHE_DIR"
sudo chmod -R 755 "$DOCKER_CACHE_DIR"
# Check if the daemon.json file exists and create if not
if [ ! -f /etc/docker/daemon.json ]; then
echo "{}" | sudo tee /etc/docker/daemon.json > /dev/null
fi
# Safely update the Docker daemon configuration file using jq and a temporary file
TMP_FILE=$(mktemp)
sudo jq --arg dataRoot "$DOCKER_CACHE_DIR" '.["data-root"] = $dataRoot' /etc/docker/daemon.json > "$TMP_FILE" && sudo mv "$TMP_FILE" /etc/docker/daemon.json
# Restart Docker to apply the changes
sudo systemctl restart docker
# Check Docker's status to ensure it's running without issues
echo "Checking Docker status..."
sudo systemctl status docker.service
# Output the Docker root directory to verify the change
echo "Docker is configured to use the following root directory:"
docker info | grep "Docker Root Dir"
echo "Setup complete. Docker cache is now set to: $DOCKER_CACHE_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment