GitHub Docker Install Script
Here's a future-proof Bash script for installing the latest Docker on Ubuntu Server along with a step-by-step guide explaining each command. This script follows best practices and ensures security by using the official Docker repository.
Docker Installation Script
Script: install_docker.sh
#!/bin/bash
# install_docker.sh - A script to install the latest Docker and Docker Compose on Ubuntu Server
# Author: amirstechport
# License: MIT
set -e
set -u
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
echo -e "${GREEN}Updating package index...${NC}"
sudo apt update && sudo apt upgrade -y
echo -e "${GREEN}Installing prerequisites...${NC}"
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common gnupg
echo -e "${GREEN}Adding Docker's official GPG key...${NC}"
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo -e "${GREEN}Adding Docker repository...${NC}"
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
echo -e "${GREEN}Updating package index to include Docker repository...${NC}"
sudo apt update
echo -e "${GREEN}Installing Docker Engine and Docker Compose Plugin...${NC}"
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
echo -e "${GREEN}Enabling and starting Docker service...${NC}"
sudo systemctl enable docker
sudo systemctl start docker
echo -e "${GREEN}Verifying Docker installation...${NC}"
sudo docker --version
sudo docker run hello-world
echo -e "${GREEN}Verifying Docker Compose installation...${NC}"
docker compose version
echo -e "${GREEN}Adding current user to the Docker group (optional)...${NC}"
sudo usermod -aG docker $USER
echo -e "${GREEN}Installation complete! Please log out and log back in to use Docker without sudo.${NC}"How to Use the Script
-
Download the Script:
Clone or download the script from my GitHub repository:wget https://github.com/amirstechport/amirstechport/raw/refs/heads/main/Docker/Install%20Docker/install_docker.sh -
Make the Script Executable:
chmod +x install_docker.sh -
Run the Script:
./install_docker.sh -
Log Out and Log Back In:
If you want to run Docker commands withoutsudo, log out and log back in for the group changes to take effect.
Step-by-Step Explanation of the Script
1. Updating Package Index
sudo apt update && sudo apt upgrade -y
- Updates the list of available packages and upgrades all installed packages to the latest versions.
2. Installing Prerequisites
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common gnupg
- Installs necessary dependencies for downloading and adding the Docker repository securely.
3. Adding Docker’s Official GPG Key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Downloads Docker's official GPG key and saves it in a secure location to verify package authenticity.
4. Adding Docker Repository
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Adds Docker's official repository to the system’s package sources.
5. Updating Package Index Again
sudo apt update
- Updates the package list to include Docker packages from the newly added repository.
6. Installing Docker
sudo apt install -y docker-ce docker-ce-cli containerd.io
- Installs the latest versions of Docker Engine, Docker CLI, and the container runtime
containerd.
7. Enabling and Starting Docker Service
sudo systemctl enable docker
sudo systemctl start docker
- Ensures Docker starts automatically on boot and starts the service immediately.
8. Verifying Docker Installation
sudo docker --version
sudo docker run hello-world
- Checks the Docker version and runs a test container to verify Docker is working.
9. Adding User to Docker Group
sudo usermod -aG docker $USER
- Adds the current user to the
dockergroup to allow running Docker commands withoutsudo.
Best Practices Followed
- Security:
- Uses Docker's official repository and GPG key.
- Future-Proof:
- Fetches the latest stable Docker version from the repository.
- Error Handling:
- The
set -eflag ensures the script exits on errors.
- The
- User Convenience:
- Adds the user to the
dockergroup for convenience.
- Adds the user to the
- Clear Messaging:
- Provides color-coded messages for each step to improve readability.
This script ensures you have an up-to-date and secure Docker installation. 🚀