Skip to content

Instantly share code, notes, and snippets.

@sysrex
Created October 1, 2025 13:04
Show Gist options
  • Save sysrex/8813e5c3ae83c607dbdad9ee14fe9c90 to your computer and use it in GitHub Desktop.
Save sysrex/8813e5c3ae83c607dbdad9ee14fe9c90 to your computer and use it in GitHub Desktop.
Update and install docker on ubuntu
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------
# Ubuntu Update + Docker Install
# -----------------------------
# - Updates & upgrades the system
# - Installs Docker Engine from Docker's official repo
# - Enables & starts Docker
# - Optionally adds the current user to the docker group
# Change this to "true" if you want the script to add the invoking user to the docker group automatically.
ADD_USER_TO_DOCKER_GROUP="true"
# Noninteractive APT to avoid prompts
export DEBIAN_FRONTEND=noninteractive
require_root() {
if [[ "$EUID" -ne 0 ]]; then
echo "Please run as root (e.g. sudo $0)"
exit 1
fi
}
detect_ubuntu() {
if [[ -f /etc/os-release ]]; then
# shellcheck disable=SC1091
. /etc/os-release
if [[ "${ID:-}" != "ubuntu" ]]; then
echo "This script is intended for Ubuntu. Detected: ${ID:-unknown}"
exit 1
fi
UBUNTU_CODENAME_RESOLVED="${UBUNTU_CODENAME:-$VERSION_CODENAME}"
else
echo "/etc/os-release not found. Cannot confirm Ubuntu."
exit 1
fi
}
apt_update_upgrade() {
echo "==> Updating package lists…"
apt-get update -y
echo "==> Upgrading packages… (this may take a while)"
# Use upgrade + dist-upgrade to pull in kernel/held deps; safe for most servers.
apt-get upgrade -y
apt-get dist-upgrade -y
echo "==> Installing prerequisites…"
apt-get install -y ca-certificates curl gnupg lsb-release
}
install_docker_repo() {
echo "==> Preparing Docker APT keyring…"
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
local ARCH
ARCH="$(dpkg --print-architecture)"
echo "==> Adding Docker APT repository for ${UBUNTU_CODENAME_RESOLVED} (${ARCH})…"
cat >/etc/apt/sources.list.d/docker.list <<EOF
deb [arch=${ARCH} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu ${UBUNTU_CODENAME_RESOLVED} stable
EOF
echo "==> apt-get update for Docker repo…"
apt-get update -y
}
remove_old_docker_packages_if_any() {
# Harmless if not installed; avoids conflicts.
echo "==> Removing old Docker packages if present…"
apt-get remove -y docker docker-engine docker.io containerd runc || true
}
install_docker() {
echo "==> Installing Docker Engine and components…"
apt-get install -y \
docker-ce \
docker-ce-cli \
containerd.io \
docker-buildx-plugin \
docker-compose-plugin
}
enable_start_services() {
echo "==> Enabling and starting Docker services…"
# Some environments (e.g., minimal containers/WSL) may lack systemd; ignore failures gracefully.
if command -v systemctl >/dev/null 2>&1; then
systemctl enable docker || true
systemctl enable containerd || true
systemctl start docker || true
systemctl start containerd || true
else
echo "systemctl not available; skipping enable/start."
fi
}
add_user_to_docker_group() {
if [[ "${ADD_USER_TO_DOCKER_GROUP}" == "true" ]]; then
# Choose a sensible user: if run via sudo, use SUDO_USER; else current user.
local TARGET_USER="${SUDO_USER:-$(id -un)}"
if id -nG "${TARGET_USER}" | grep -qw docker; then
echo "==> User ${TARGET_USER} is already in the docker group."
else
echo "==> Adding ${TARGET_USER} to docker group…"
groupadd -f docker
usermod -aG docker "${TARGET_USER}"
echo ">>> You may need to log out and back in (or 'newgrp docker') for group changes to take effect."
fi
fi
}
verify_install() {
echo "==> Verifying Docker installation…"
if command -v docker >/dev/null 2>&1; then
docker --version || true
echo "==> Running 'hello-world' test (may pull image)…"
# Run test but don't fail the entire script if it can't pull (e.g., offline env).
docker run --rm hello-world || true
else
echo "Docker CLI not found on PATH — something went wrong."
exit 1
fi
}
main() {
require_root
detect_ubuntu
apt_update_upgrade
remove_old_docker_packages_if_any
install_docker_repo
install_docker
enable_start_services
add_user_to_docker_group
echo "==> Cleaning up unused packages…"
apt-get autoremove -y
apt-get autoclean -y
verify_install
echo "✅ Done."
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment