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.
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:
1
docker --version
Check Docker Compose version:
1
docker compose version
Run the test container:
1
docker run hello-world
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 scriptCOMPOSE_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 messageapt-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 connectivityping -c 3 download.docker.com
# If DNS fails, try using Google's DNSecho"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 membershipgroups
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