One-Click Docker Setup Script for Ubuntu

Photo by Pixabay on Pexels
One-Click Docker Setup Script for Ubuntu
Setting up Docker on a fresh Ubuntu server traditionally involves multiple commands, repository additions, and configuration steps. This one-click script automates the entire process, getting you from a bare-metal server to a fully functional Docker environment in under two minutes. Whether you are provisioning a new development machine or deploying to production, this script eliminates repetitive manual work and ensures every installation follows best practices consistently.
Why Automate Docker Installation?
Manual Docker installation is prone to errors. You might forget to add your user to the docker group, overlook the Docker Compose plugin, or misconfigure the daemon. An automated setup script guarantees consistency across environments, saves time during server provisioning, and serves as documented infrastructure-as-code for your team. When you need to spin up five new servers, running one script per server is far more reliable than typing twenty commands each time.
- Zero manual intervention after launch — fully non-interactive
- Idempotent — safe to run multiple times without side effects
- Configurable Docker version and registry mirrors for your region
- Includes Docker Compose V2 plugin and BuildKit for modern builds
- Enables log rotation to prevent disk fill-up from container logs
- Verifies the installation and reports the installed versions
The Complete Setup Script
Below is the full one-click installation script. Save it as install-docker.sh and run it with sudo bash install-docker.sh. The script is written with set -euo pipefail to fail safely if any step encounters an error, and each step prints its progress so you can see exactly where it is in the process.
#!/bin/bash
set -euo pipefail
# Docker One-Click Installer for Ubuntu 22.04+
echo "[1/7] Removing old Docker packages..."
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do
apt-get remove -y $pkg 2>/dev/null || true
done
echo "[2/7] Updating package index..."
apt-get update -qq
echo "[3/7] Installing prerequisites..."
apt-get install -y -qq ca-certificates curl gnupg lsb-release
echo "[4/7] Adding Docker GPG key and repository..."
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
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
echo "[5/7] Installing Docker Engine..."
apt-get update -qq
apt-get install -y -qq docker-ce docker-ce-cli containerd.io \
docker-buildx-plugin docker-compose-plugin
echo "[6/7] Configuring Docker daemon..."
cat > /etc/docker/daemon.json <<'EOF'
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"live-restore": true
}
EOF
echo "[7/7] Enabling and starting Docker..."
systemctl enable docker
systemctl start docker
echo "Installation complete!"
docker --version
docker compose version
Post-Installation Configuration
After the script completes, take these additional steps to finalize your setup and make sure everything works as expected.
Add Your User to the Docker Group
Running Docker commands without sudo requires adding your user to the docker group. Run these commands after installation:
sudo usermod -aG docker $USER
newgrp docker
After running these commands, log out and back in for the group changes to take full effect. Verify with docker ps — if it works without sudo, you are all set.
Verify the Installation
docker run hello-world
docker compose version
docker buildx version
All three commands should return success without errors. The hello-world container confirms the Docker Engine can pull images and run containers. docker compose version confirms the orchestration plugin is installed. docker buildx version confirms modern multi-platform build capabilities are available.
What About Docker Compose?
Modern Docker distributions bundle Docker Compose V2 as a plugin. You invoke it with docker compose (no hyphen) instead of the legacy docker-compose. The script installs the plugin automatically, so no separate download is needed. This is important because the standalone docker-compose binary is no longer maintained and lacks many features available in the V2 plugin, including improved service discovery and better integration with Docker Swarm.
Configuring Registry Mirrors
If you are in a region with slow Docker Hub access, edit /etc/docker/daemon.json to add mirror registries. This can dramatically improve pull speeds, especially for larger images like Python or Node.js runtimes.
{
"registry-mirrors": ["https://mirror.gcr.io"]
}
Restart Docker after making changes: systemctl restart docker. You can add multiple mirrors in the array — Docker will try them in order and fall back to Docker Hub if none respond.
Security Hardening
A production Docker host requires additional security measures beyond the basic setup. Container escape vulnerabilities are rare but serious, so hardening your Docker daemon is an essential step for any internet-facing deployment.
- Enable Docker Content Trust: set
DOCKER_CONTENT_TRUST=1in your environment to ensure only signed images can be pulled or run - Use read-only root filesystems for containers to prevent unauthorized writes
- Limit container capabilities with
--cap-drop=ALLand add back only what is needed - Enable user namespace remapping so containers run as non-root users
- Configure firewall rules for Docker bridge networks to restrict traffic between containers
- Regularly scan images with
docker scanor Trivy to identify known vulnerabilities
Uninstalling Docker Cleanly
Should you need to remove Docker, run these commands in order. The purge command removes packages and their configuration files, while deleting /var/lib/docker removes all containers, images, and volumes.
apt-get purge -y docker-ce docker-ce-cli containerd.io
apt-get autoremove -y
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
Be absolutely certain you want to remove everything before running that last rm command — there is no undo for data in /var/lib/docker.
Troubleshooting Common Issues
If the script encounters errors, check these common causes before digging deeper:
- Permission denied: Ensure you run the script with
sudo. The installation modifies system-level files and requires root access. - Network timeout: Your server may need a proxy or faster mirror. Check
curl -fsSL https://download.docker.comto verify connectivity. - Old kernel: Ubuntu 20.04+ is required for modern Docker features. Check your kernel with
uname -r. - AppArmor conflicts: Check
sudo aa-statusfor Docker profiles. Some VPS providers ship with restrictive AppArmor profiles that interfere with container execution. - iptables issues: Docker manages iptables rules automatically. If you have an existing firewall, make sure it does not block Docker's network bridges.
Download the Script
Get the complete one-click Docker setup script, along with configuration templates and a troubleshooting guide, free on freefile.store. The download includes the main installation script, a hardened daemon.json with security defaults, a Docker Compose starter template, and a comprehensive troubleshooting reference. Ideal for DevOps engineers, system administrators, and developers who provision Ubuntu servers on a regular basis.