This script automates the installation of Docker and Docker Compose on an Ubuntu Server 24.04 LTS system. It ensures that the latest stable versions of Docker components are installed and configured properly. The script is designed to simplify the process by handling repository setup, key management, and user permissions automatically.
Manually installing Docker requires multiple steps, including adding repositories, setting up GPG keys, and handling permissions. This script streamlines the process by:
By running a single script, you save time and reduce the chances of misconfiguring your Docker setup.
The script consists of several steps, each crucial for setting up Docker and Docker Compose correctly.
echo "Updating package index and installing prerequisites..."
sudo apt update
sudo apt install -y ca-certificates curl gnupg lsb-release
These commands ensure that the system has the necessary packages to securely add and manage external repositories.
echo "Adding Docker's official GPG key and setting up repository..."
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
This section ensures that Docker’s repository is added securely, enabling the system to fetch the latest Docker packages.
echo "Installing Docker Engine..."
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
echo "Adding current user to the Docker group..."
TARGET_USER="${SUDO_USER:-$(logname 2>/dev/null || true)}"
if [[ -z "$TARGET_USER" || "$TARGET_USER" == "root" ]]; then
TARGET_USER="$(id -un)"
fi
sudo usermod -aG docker "$TARGET_USER"
echo "Added $TARGET_USER to the docker group."
This step allows the current user to run Docker commands without needing sudo. However, changes take effect only after logging out and back in.
echo "Verifying Docker installation..."
docker --version
echo "Verifying Docker Compose installation..."
docker compose version
These commands confirm that Docker and Docker Compose were installed correctly and display their versions.
echo "Testing Docker access (may require re-login)..."
if docker ps >/dev/null 2>&1; then
echo "Docker works without sudo."
else
echo "Docker requires a new login for group changes to apply."
echo "Run: newgrp docker"
echo "Or: log out and back in."
fi
This message reminds the user to log out and back in so that the docker group changes take effect.
install-docker.shchmod +x install-docker.sh
./install-docker.sh
Once the script completes, log out and log back in to ensure you can run Docker commands without sudo.
curl -fsSL https://wiki.kitpro.us/install-docker.sh | bash
⚠️ Security note: Always review scripts before running them with
bash. You can download and inspect the script first if preferred.
This script provides a quick and efficient way to set up Docker and Docker Compose on Ubuntu Server 24.04 LTS. By automating the installation process, it ensures consistency and reduces the chances of manual errors. If you frequently deploy containers, this script is a great tool to have in your toolkit.