Skip to content

Instantly share code, notes, and snippets.

@johannesvedder
Last active June 8, 2024 09:56
Show Gist options
  • Save johannesvedder/0fafcbeabe8e069f96085bfedaebd9d0 to your computer and use it in GitHub Desktop.
Save johannesvedder/0fafcbeabe8e069f96085bfedaebd9d0 to your computer and use it in GitHub Desktop.
Install and configure docker for the use with a HPI VM
#!/bin/bash
# This script:
# - installs Docker with all prerequisites
# - configures the system to run Docker without root
# - configures Docker to start on boot with systemd
# - moves the Docker root to the home directory
# Configuration section
NEW_DOCKER_ROOT_DIR=$HOME/docker-root
# Script start
sudo apt-get update
sudo apt-get upgrade -y
# Install Docker
# https://docs.docker.com/engine/install/ubuntu/
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo chmod a+r /etc/apt/keyrings/docker.gpg
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
## Run docker without sudo
# sudo groupadd docker
sudo usermod -aG docker "$USER"
newgrp docker
docker run hello-world
## Run Docker on system boot
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
## Relocate docker root directory to home dir
# https://www.ibm.com/docs/en/z-logdata-analytics/5.1.0?topic=compose-relocating-docker-root-directory
sudo systemctl stop docker
sudo systemctl stop docker.socket
sudo systemctl stop containerd
mkdir -p "$NEW_DOCKER_ROOT_DIR"
sudo mv /var/lib/docker /"$NEW_DOCKER_ROOT_DIR"
sudo apt-get install -y jq
sudo jq --arg rootdir "$NEW_DOCKER_ROOT_DIR" '. + { "data-root": $rootdir }' /etc/docker/daemon.json \
| sudo tee -a daemon.json.tmp > /dev/null && sudo mv daemon.json.tmp /etc/docker/daemon.json
sudo systemctl start docker
OLD_DOCKER_ROOT_DIR=$(docker info -f '{{ .DockerRootDir}}')
if [ "$OLD_DOCKER_ROOT_DIR" = "$NEW_DOCKER_ROOT_DIR" ]
then
echo "Success! New docker root dir is at: $NEW_DOCKER_ROOT_DIR"
else
echo "ERROR! Docker root dir is still at: $OLD_DOCKER_ROOT_DIR"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment