Created
August 26, 2025 17:38
-
-
Save Aziz9022/21a126a4c44e67c1b21c021be61649f0 to your computer and use it in GitHub Desktop.
Ubuntu Docker Installation
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
| #!/bin/bash | |
| set -e | |
| # Ensure script is executed as root | |
| if [ "$(id -u)" -ne 0 ]; then | |
| echo "❌ Please run this script with sudo or as root." | |
| exit 1 | |
| fi | |
| echo "🚀 Starting Docker CE installation on Ubuntu..." | |
| # 1. Remove old/unofficial Docker packages | |
| echo "Removing old Docker packages if any..." | |
| apt-get remove -y docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc || true | |
| # 2. Update apt index and install prerequisites | |
| echo "Updating package index and installing prerequisites..." | |
| apt-get update | |
| apt-get install -y ca-certificates curl gnupg lsb-release | |
| # 3. Add Docker’s official GPG key | |
| echo "Adding Docker GPG key..." | |
| mkdir -p /etc/apt/keyrings | |
| curl -fsSL https://download.docker.com/linux/ubuntu/gpg \ | |
| | gpg --dearmor -o /etc/apt/keyrings/docker.gpg | |
| chmod a+r /etc/apt/keyrings/docker.gpg | |
| # 4. Add Docker apt repository | |
| echo "Adding Docker repository..." | |
| ARCH=$(dpkg --print-architecture) | |
| CODENAME=$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") | |
| echo "deb [arch=$ARCH signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $CODENAME stable" \ | |
| | tee /etc/apt/sources.list.d/docker.list > /dev/null | |
| # 5. Update apt index | |
| echo "Updating apt-index with the new Docker repository..." | |
| apt-get update | |
| # 6. Install Docker Engine and related components | |
| echo "Installing Docker Engine and components..." | |
| apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin | |
| # 7. Start and enable Docker service | |
| echo "Enabling and starting Docker service..." | |
| systemctl enable docker | |
| systemctl start docker | |
| # 8. Post-install: allow non-root users to use Docker (optional) | |
| if [ "$1" == "--non-root" ] || [ "$1" == "-u" ]; then | |
| echo "Adding $SUDO_USER (or current user) to docker group..." | |
| TARGET_USER="${SUDO_USER:-$(logname)}" | |
| groupadd docker 2>/dev/null || true | |
| usermod -aG docker "$TARGET_USER" | |
| echo "✅ User $TARGET_USER added to 'docker' group. Re-login or run 'newgrp docker' to apply changes." | |
| fi | |
| # 9. Test Docker installation | |
| echo "Running hello-world test..." | |
| docker run --rm hello-world | |
| echo "🎉 Docker installation completed successfully!" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Instructions
Save the script to a file, e.g.,
docker.sh.Make it executable:
Run it with root privileges:
Optional: To allow your user to run Docker without
sudo, pass the flag: