Last active
October 16, 2025 21:12
-
-
Save yibudak/7c7e240df57f4a38af3dfccbc34183fd to your computer and use it in GitHub Desktop.
Install Docker on Ubuntu Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # install-docker-ubuntu.sh | |
| set -euo pipefail | |
| SUDO='' ; [[ $EUID -ne 0 ]] && SUDO='sudo' | |
| usage() { | |
| echo "Usage: $0 [--no-test] | [--uninstall]" | |
| exit 1 | |
| } | |
| [[ "${1:-}" == "--help" ]] && usage | |
| # Supported Ubuntu releases | |
| . /etc/os-release | |
| CODENAME="${UBUNTU_CODENAME:-$VERSION_CODENAME}" | |
| case "$CODENAME" in | |
| jammy|noble|plucky|questing) ;; | |
| *) echo "Unsupported Ubuntu release: $CODENAME"; exit 1 ;; | |
| esac | |
| if [[ "${1:-}" == "--uninstall" ]]; then | |
| $SUDO apt-get -y purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras || true | |
| $SUDO rm -rf /var/lib/docker /var/lib/containerd || true | |
| $SUDO rm -f /etc/apt/sources.list.d/docker.list /etc/apt/keyrings/docker.asc || true | |
| echo "Docker uninstalled." | |
| exit 0 | |
| fi | |
| # Warn about firewall/nft usage | |
| if command -v ufw >/dev/null && sudo ufw status | grep -q "Status: active"; then | |
| echo "Note: Docker-published ports bypass ufw rules unless you manage DOCKER-USER chain." | |
| fi | |
| if command -v nft >/dev/null; then | |
| echo "Note: Use iptables rules (legacy or nft backend). nftables-native rules are not honored by Docker." | |
| fi | |
| # Remove conflicting packages | |
| for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do | |
| $SUDO apt-get -y remove "$pkg" || true | |
| done | |
| # Prereqs | |
| $SUDO apt-get update | |
| $SUDO apt-get -y install ca-certificates curl gnupg lsb-release | |
| # Keyring | |
| $SUDO install -m 0755 -d /etc/apt/keyrings | |
| $SUDO curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc | |
| $SUDO chmod a+r /etc/apt/keyrings/docker.asc | |
| # Repo | |
| echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu ${CODENAME} stable" | \ | |
| $SUDO tee /etc/apt/sources.list.d/docker.list >/dev/null | |
| # Install | |
| $SUDO apt-get update | |
| $SUDO apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin | |
| # Enable and start | |
| $SUDO systemctl enable --now docker || $SUDO systemctl start docker | |
| # Test (hello-world) | |
| if [[ "${1:-}" != "--no-test" ]]; then | |
| $SUDO docker run --rm hello-world || true | |
| fi | |
| # Add current user to docker group (if non-root) | |
| if [[ $EUID -ne 0 ]]; then | |
| if ! getent group docker >/dev/null; then $SUDO groupadd docker; fi | |
| $SUDO usermod -aG docker "$USER" | |
| echo "Added $USER to docker group. Log out and back in." | |
| fi | |
| echo "Docker installed on Ubuntu ${CODENAME}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wget -qO- 'https://gist.githubusercontent.com/yibudak/7c7e240df57f4a38af3dfccbc34183fd/raw/45ac3fa77ed690f1b23160183e7744388787a1eb/install-docker.sh' | bash