π³ Why Use a Bash Script to Install Docker?
Installing Docker manually works fine for a single server, but when you need to set up multiple machines or want a repeatable, error-free process, a bash script is the way to go.
Benefits of scripted installation:
- β‘ Speed: Install Docker in one command instead of running multiple steps
- π Consistency: Every server gets the exact same setup
- π Automation: Perfect for provisioning new servers in CI/CD pipelines
- π Documentation: The script itself serves as documentation of your setup process
β
Prerequisites
Before running the script, make sure you have:
- π₯οΈ Ubuntu 20.04, 22.04, or 24.04 server
- π Sudo privileges to execute administrative commands
- π Internet connection to download packages
- β±οΈ Time: The script takes approximately 2-5 minutes to complete
The Installation Script
Below is a complete bash script that handles the entire Docker installation process, including cleanup of old versions, repository setup, installation, and user permission configuration.
Create the script file:
Paste the following content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
| #!/bin/bash
set -e
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Docker Installation Script for Ubuntu ${NC}"
echo -e "${GREEN}========================================${NC}"
# Check if running as root or with sudo
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}[ERROR] Please run this script with sudo or as root.${NC}"
exit 1
fi
# Detect Ubuntu version
UBUNTU_VERSION=$(lsb_release -cs)
echo -e "${YELLOW}[INFO] Detected Ubuntu version: ${UBUNTU_VERSION}${NC}"
# Step 1: Remove old Docker versions
echo -e "${YELLOW}[1/6] Removing old Docker versions (if any)...${NC}"
apt-get remove -y docker docker-engine docker.io containerd runc 2>/dev/null || true
# Step 2: Update system and install dependencies
echo -e "${YELLOW}[2/6] Updating system and installing dependencies...${NC}"
apt-get update -y
apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release \
software-properties-common
# Step 3: Add Docker's official GPG key
echo -e "${YELLOW}[3/6] Adding Docker GPG key...${NC}"
install -m 0755 -d /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
# Step 4: Add Docker repository
echo -e "${YELLOW}[4/6] Adding Docker repository...${NC}"
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
${UBUNTU_VERSION} stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
# Step 5: Install Docker Engine
echo -e "${YELLOW}[5/6] Installing Docker Engine...${NC}"
apt-get update -y
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Step 6: Enable and start Docker
echo -e "${YELLOW}[6/6] Enabling and starting Docker service...${NC}"
systemctl enable docker
systemctl start docker
# Add current user to docker group (if run via sudo)
if [ -n "$SUDO_USER" ]; then
usermod -aG docker "$SUDO_USER"
echo -e "${GREEN}[INFO] User '${SUDO_USER}' added to the docker group.${NC}"
echo -e "${YELLOW}[NOTE] Log out and back in for group changes to take effect.${NC}"
fi
# Verify installation
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Installation Complete! π ${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
docker --version
docker compose version
echo ""
echo -e "${GREEN}[SUCCESS] Docker is installed and running.${NC}"
echo -e "${YELLOW}[TIP] Run 'docker run hello-world' to verify.${NC}"
|
How to Use the Script
Run Directly
Make the script executable and run it:
1
2
| chmod +x install-docker.sh
sudo ./install-docker.sh
|
Run Without Downloading
You can also run the script directly from a URL using curl (useful for remote server provisioning):
1
| curl -fsSL https://your-server.com/install-docker.sh | sudo bash
|
β οΈ Security Note: Always review scripts before piping them to bash. Only use this method with scripts you trust or host yourself.
What the Script Does
Here’s a breakdown of each step:
| Step | Action | Purpose |
|---|
| 1 | Remove old Docker versions | Prevents conflicts with previous installations |
| 2 | Update system & install dependencies | Ensures required packages are available |
| 3 | Add Docker GPG key | Authenticates packages from Docker’s repository |
| 4 | Add Docker repository | Configures APT to pull packages from Docker |
| 5 | Install Docker Engine | Installs Docker CE, CLI, containerd, Buildx, and Compose |
| 6 | Enable & start Docker | Ensures Docker runs now and on every boot |
Verifying the Installation
After the script completes, verify everything is working:
Check Docker version:
Check Docker Compose version:
Run the test container:
If you see the “Hello from Docker!” message, the installation was successful! π
Check Docker service status:
1
| sudo systemctl status docker
|
You should see active (running) in the output.
Customizing the Script
You can extend the script to fit your needs:
Add Docker Compose Standalone
If you need the standalone docker-compose command (v1 syntax):
1
2
3
4
| # Add this to the end of the script
COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep tag_name | cut -d '"' -f 4)
curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
|
Add Automatic Cleanup
Add a cleanup step to remove unnecessary packages after installation:
1
2
3
| # Add this before the final success message
apt-get autoremove -y
apt-get clean
|
π§ Troubleshooting
Script fails with “Permission denied”
Cause: The script was not run with sudo.
Solution:
1
| sudo ./install-docker.sh
|
Script fails at GPG key step
Cause: Network issues or DNS resolution problems.
Solution:
1
2
3
4
5
| # Check internet connectivity
ping -c 3 download.docker.com
# If DNS fails, try using Google's DNS
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
|
Docker commands require sudo after script
Cause: Group changes haven’t taken effect yet.
Solution:
1
2
3
4
5
| # Log out and log back in, or run:
newgrp docker
# Verify group membership
groups
|
π― Conclusion
Using a bash script to install Docker saves time and eliminates manual errors. Whether you’re setting up a single development machine or provisioning a fleet of production servers, this script provides a reliable and repeatable process. π
Key takeaways:
- π One command to install Docker completely
- π Idempotent β safe to run multiple times
- π οΈ Customizable β extend the script for your specific needs
- π Production-ready β includes Docker Compose plugin and Buildx
π References:
Happy automating! ππ