How to Install Docker on a VPS and Run Applications Easily
Running applications directly on a VPS can be simple—until it isn’t.
At the beginning, installing Nginx, PHP, Node.js, a database, and background workers “normally” feels manageable. But as soon as you maintain multiple apps, different versions, or team handovers, the server turns into a fragile system: one upgrade breaks another service, dependencies conflict, and rollback becomes stressful.
Docker solves that by packaging your application and its dependencies into containers. Instead of building your server by hand, you define your setup once (images, ports, environment variables, volumes), then run it the same way everywhere.
In this guide, you’ll learn how to install Docker on a VPS and run an application easily, using a professional, production-minded workflow. We’ll cover:
- installation for popular Linux distributions,
- the core Docker commands you’ll use daily,
- a practical Docker Compose example,
- firewall and security fundamentals,
- and troubleshooting for common VPS issues.
Table of Contents
- What You Need Before Installing Docker
- Docker vs Virtual Machines (Quick Context)
- Mechanism: How Docker Works on a VPS (Cause → Effect)
- Step 1: Update the VPS and Prepare Dependencies
- Step 2: Install Docker (Ubuntu/Debian)
- Step 3: Install Docker (CentOS/RHEL/Rocky/Alma)
- Step 4: Post-Install Setup (Non-root, Autostart)
- Step 5: Install Docker Compose
- Step 6: Run Your First Container
- Step 7: Run a Real App with Docker Compose (Example)
- Step 8: Production Tips (Security, Logs, Backups)
- Common Mistakes & Troubleshooting
- Quick Checklist
- FAQ
- Conclusion
What You Need Before Installing Docker
Before you start, make sure you have:
- A VPS with Ubuntu 22.04/24.04, Debian 11/12, or Rocky/Alma/CentOS
- Root access (or a sudo user)
- At least 1 GB RAM (2 GB+ recommended for databases)
- A domain name (optional, but useful for real deployments)
Cause → effect: If your VPS has too little RAM, containers may start but fail under load, because Linux will swap memory or kill processes (OOM). Docker doesn’t remove resource limits—it makes application packaging predictable.
Docker vs Virtual Machines (Quick Context)
A virtual machine (VM) emulates an entire operating system. Docker containers share the host kernel, but isolate processes, networks, and filesystems.
Cause → effect: Because containers share the host kernel, they are usually lighter and start faster than VMs. That makes Docker ideal for VPS environments where you want efficient resource usage and fast deployment.
Mechanism: How Docker Works on a VPS (Cause → Effect)
Understanding the mechanism helps you troubleshoot faster.
1) Images vs containers
- Image: a blueprint (application + dependencies)
- Container: a running instance of an image
Cause → effect: If an image is built correctly, your app runs the same way regardless of what packages are installed on the VPS. That reduces “works on my machine” problems.
2) Ports and networking
Containers run in an isolated network namespace.
Cause → effect: If you don’t publish ports (e.g., -p 80:80), the app may run but won’t be reachable from outside. Most “it works locally but not on the server” issues are actually port publishing or firewall issues.
3) Volumes and persistence
By default, container filesystems are ephemeral.
Cause → effect: If you run a database without volumes, restarting the container can delete your data. Production setups require volumes (or managed databases) for persistence.
4) Docker Compose
Compose lets you define multi-container apps in one file.
Cause → effect: Without Compose, you manage containers manually and configuration drifts over time. With Compose, your deployment becomes repeatable and version-controlled.
Step 1: Update the VPS and Prepare Dependencies
SSH into your VPS first:
ssh root@YOUR_SERVER_IP
For Ubuntu/Debian
sudo apt update
sudo apt -y upgrade
sudo apt -y install ca-certificates curl gnupg
Cause → effect: If your system packages are outdated, Docker dependencies may install but behave inconsistently, especially around TLS certificates and repositories.
Step 2: Install Docker (Ubuntu/Debian)
2.1 Add Docker’s official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL <https://download.docker.com/linux/ubuntu/gpg> | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
2.2 Add Docker repository
For Ubuntu:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] <https://download.docker.com/linux/ubuntu> \
$(. /etc/os-release && echo $VERSION_CODENAME) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
For Debian, replace ubuntu with debian:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] <https://download.docker.com/linux/debian> \
$(. /etc/os-release && echo $VERSION_CODENAME) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
2.3 Install Docker Engine
sudo apt update
sudo apt -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
2.4 Verify Docker
docker --version
sudo docker run --rm hello-world
Cause → effect: Running hello-world proves three things at once: Docker is installed, the daemon is running, and the VPS can pull images from the registry.
Step 3: Install Docker (CentOS/RHEL/Rocky/Alma)
sudo dnf -y update
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo <https://download.docker.com/linux/centos/docker-ce.repo>
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Enable and start Docker:
sudo systemctl enable --now docker
sudo systemctl status docker --no-pager
Test:
sudo docker run --rm hello-world
Step 4: Post-Install Setup (Non-root, Autostart)
4.1 Run Docker without sudo (recommended)
sudo usermod -aG docker $USER
newgrp docker
Test:
docker ps
Cause → effect: Without this, every Docker command requires sudo, which increases risk (it’s easier to run destructive commands as root).
4.2 Make sure Docker starts after reboot
On systemd systems, Docker usually starts automatically if enabled:
sudo systemctl is-enabled docker
Step 5: Install Docker Compose
Today, Compose is commonly installed as a Docker plugin (docker compose). Verify:
docker compose version
If your distro doesn’t provide it through Docker packages, you can install a standalone binary—but prefer the plugin when possible.
Cause → effect: Using the official plugin reduces version mismatch issues, which often cause confusing YAML errors.
Step 6: Run Your First Container
A quick test with Nginx:
docker run -d --name web -p 8080:80 nginx:alpine
Check:
docker ps
curl -I <http://localhost:8080>
Stop and remove:
docker stop web
docker rm web
Cause → effect: Publishing 8080:80 maps VPS port 8080 to container port 80. If you forget -p, the container runs but is unreachable from the outside.
Step 7: Run a Real App with Docker Compose (Example)
Below is a simple, production-friendly pattern: Nginx + an app + a database.
7.1 Create a project folder
mkdir -p ~/myapp
cd ~/myapp
7.2 Create compose.yml
services:
app:
image: nginx:alpine
container_name: myapp_app
ports:
- "8080:80"
restart: unless-stopped
db:
image: postgres:16-alpine
container_name: myapp_db
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
POSTGRES_DB: mydb
volumes:
- db_data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
db_data:
Start:
docker compose up -d
Inspect:
docker compose ps
docker compose logs --tail=100
Stop:
docker compose down
Stop (and remove volumes/data) — be careful:
docker compose down -v
Cause → effect: The volumes: section is what makes the database persistent. Without it, container recreation can wipe your data.
Note: This example uses Nginx as a placeholder “app” image. In real deployments, you’d replace it with your application image (Node/PHP/Python) and configure environment variables, health checks, and reverse proxy routing.
Step 8: Production Tips (Security, Logs, Backups)
1) Firewall: only open what you need
If you use ports 80/443:
- open 80/443 publicly
- keep 22 (SSH) restricted if possible
Cause → effect: Exposing unnecessary ports increases attack surface. Attack surface increases the chance of brute force attempts and misconfiguration-based breaches.
2) Avoid running databases on the public internet
Don’t publish Postgres/MySQL ports publicly unless absolutely required.
Cause → effect: Published database ports get scanned constantly. Even with strong passwords, exposure increases risk and can create unexpected load.
3) Log management
Use:
docker logs -f CONTAINER
And consider log rotation.
Cause → effect: Without rotation, logs can fill disk and cause outages (“disk full” is one of the most common VPS incidents).
4) Backups
- back up volumes (database)
- back up configuration files (
compose.yml,.env) - test restore procedures
Cause → effect: Untested backups create false confidence. Restore testing is what turns backups into real recovery.
Common Mistakes & Troubleshooting
1) “Cannot connect to the Docker daemon”
Fix:
sudo systemctl status docker --no-pager
sudo systemctl start docker
If you want non-root access, ensure you’re in the docker group:
groups
2) Container runs, but the app is not accessible
Check published ports:
docker ps
Check firewall:
sudo ufw status
# or
sudo firewall-cmd --list-all
3) Out of disk space
Check:
df -h
Clean unused Docker objects:
docker system prune
Be careful: pruning deletes unused images/containers and can affect rollback.
4) Permissions issues with volumes
Cause → effect: Container users may not match host user IDs. Fix by aligning ownership, using correct UID/GID, or choosing images that support non-root operation.
5) Server becomes slow after adding Docker
Cause → effect: Often caused by memory pressure, swap, or heavy disk I/O from logs and databases. Monitor CPU/RAM/I/O and set resource limits when necessary.
Quick Checklist
- [ ] Update the VPS before installing Docker
- [ ] Install Docker from the official repository
- [ ] Verify with
hello-world - [ ] Enable Docker to start at boot
- [ ] Use
docker composefor repeatable deployments - [ ] Publish only needed ports
- [ ] Use volumes for databases and persistent data
- [ ] Add log rotation and monitor disk usage
- [ ] Keep SSH secured (keys, non-default settings)
- [ ] Test backups and restores
FAQ
1) Should I install Docker on a low-RAM VPS (1 GB)?
You can, but avoid heavy stacks. Cause → effect: databases and multiple containers increase RAM usage quickly, which can trigger swapping and slow everything down.
2) Is Docker safe for production on a VPS?
Yes, if you follow good practices: least privilege, minimal exposed ports, updates, and monitoring.
3) What’s the difference between docker compose and docker-compose?
docker compose is the newer plugin-based approach. docker-compose is the older standalone tool.
4) Where should I store environment variables?
Use a .env file (not committed publicly) and reference it in Compose. Cause → effect: hardcoding secrets in YAML or shell history increases leakage risk.
5) Do I need a reverse proxy (Nginx/Traefik) with Docker?
For one app, not always. For multiple apps and HTTPS, yes—because you need routing and certificate management.
6) How do I update a Dockerized app?
Typically:
docker compose pull
docker compose up -d
Cause → effect: pulling updates replaces images; recreating containers applies the new version predictably.
Conclusion
Installing Docker on a VPS is one of the fastest ways to make deployments repeatable and reliable. Cause → effect: when your environment is defined as containers and Compose files, you reduce dependency conflicts, speed up rollbacks, and make scaling easier.
Start simple:
-
install Docker,
-
run a test container,
-
move your real application into a Compose stack,
-
then add production essentials (firewall rules, monitoring, backups, and HTTPS).
Done right, Docker turns a VPS from a “hand-built server” into an environment you can reproduce, manage, and improve with confidence.

Komentar
Posting Komentar