How to Set Up Docker on a VPS
By Harborvm7 min read
Key takeaways
- Install Docker on a Linux VPS with the official convenience script or your distro's package manager, then add your user to the docker group so you can run commands without sudo.
- Map ports with -p host:container to expose a container, and store anything you want to keep in a named volume so it survives a container rebuild.
- In the 2025 Stack Overflow Developer Survey, 73.8% of professional developers reported using Docker, the largest single-year jump of any tool measured.
- Harborvm gives NO backups and NO snapshots, so keep your own copy of volumes and your docker-compose.yml off the server.
To set up Docker on a VPS, install the Docker engine from the official repository, add your user to the docker group, then run a container with docker run. On a Linux VPS the whole process takes a few minutes, and the same steps work on Ubuntu, Debian, AlmaLinux, Rocky Linux and Fedora.
This guide walks through each step with exact commands: installing Docker and Compose, running your first container with mapped ports, keeping data with volumes, locking down the daemon, and running a multi-container app. Docker is now the default way most developers package software. In the 2025 Stack Overflow Developer Survey, 73.8% of professional developers said they use it, the largest single-year rise of any technology measured (Stack Overflow, 2025).
Why a KVM VPS Is the Right Host for Docker
Docker needs a real Linux kernel and the control groups and namespaces that come with it. A KVM VPS gives you that, because every VM runs its own kernel. Container-style VPS products built on OpenVZ share the host kernel and often block the features Docker depends on, so Docker either refuses to start or behaves strangely.
Every Harborvm plan is full KVM on hardware we run ourselves, with 1 vCPU equal to one real CPU thread and memory reserved rather than oversold. That matters for Docker: a build or a busy container gets the cycles it was promised instead of fighting noisy neighbors.

Pick 2 GB of RAM for a handful of light containers, or 4 GB and up once you add a database or several services. The Small plan at $10 a month handles a first Docker host; step up to Medium or Large as your stack grows.
Installing Docker and Docker Compose on Linux
Install Docker from the official repository so you get the current engine plus the Compose plugin in one step. On Ubuntu or Debian, the convenience script is the quickest route. Run it on a fresh VPS, then add your own user to the docker group.
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER
Log out and back in so the group change takes effect. Now check the install:
docker version
docker compose version
On AlmaLinux, Rocky Linux or Fedora, the same script works, or you can add Docker's dnf repository by hand. The official Docker install documentation lists the repository method for each distribution if you prefer not to pipe a script into a shell.
Adding your user to the docker group lets you run commands without sudo. Be aware that this is effectively root access, since the daemon runs as root. Only trusted users should be in that group. We cover the wider server hardening steps in our VPS security checklist.
Modern Docker ships Compose as the docker compose plugin, so there is no separate package to chase. If docker compose version prints a version number, you already have everything you need for the multi-container section later.
Running Your First Container and Mapping Ports
A container is a running copy of an image. To prove the install works, run the official Nginx web server image and map a port from the VPS to the container. Docker pulls the image the first time, then starts it in a second.
docker run -d --name web -p 8080:80 nginx
The -p 8080:80 flag maps port 8080 on the VPS to port 80 inside the container. The format is always host:container. Visit your server's address on port 8080 and you will see the Nginx welcome page. To list what is running and then stop and remove it:
docker ps
docker stop web && docker rm web
Containers do not survive a reboot unless you tell them to. Add --restart unless-stopped to the docker run line and Docker brings the container back after the VPS restarts. This one flag saves a lot of confusion after a kernel update or a scheduled reboot.
One note on networking. Harborvm gives each VM a private address behind the server's IP, with up to 10 forwarded ports rather than a dedicated public IPv4. Map a container to a forwarded port and it is reachable from the internet. For a clean domain and automatic HTTPS in front of a container, put a reverse proxy such as Caddy or Nginx on a forwarded port.
Persisting Data with Volumes
Anything written inside a container disappears when you remove it. To keep data, store it in a named volume that lives on the VPS disk and survives rebuilds. This is how databases and file uploads stay safe across upgrades.
docker volume create pgdata
docker run -d --name db \
-e POSTGRES_PASSWORD=change_me \
-v pgdata:/var/lib/postgresql/data \
postgres:16
The -v pgdata:/var/lib/postgresql/data flag mounts the volume at the path where Postgres keeps its files. Remove and recreate the container and the data is still there, because the volume is separate from the container. You can inspect or list volumes at any time:
docker volume ls
docker volume inspect pgdata
A volume protects data against a container rebuild, but it is still a single copy on one disk. Harborvm offers NO backups and NO snapshots, so a volume is not a backup. Copy important volumes off the server yourself. A simple nightly job that runs docker run --rm -v pgdata:/data -v $(pwd):/out alpine tar czf /out/pgdata.tgz /data and ships the archive to storage elsewhere is enough for most people.
Securing the Docker Daemon and Containers
The Docker daemon runs as root, so a loose configuration is a direct path to full server access. The first rule is simple: never expose the Docker API on a public port. Leave the daemon on its local socket and reach it over SSH, not over TCP.
Keep containers lean too. Run as a non-root user inside the container where the image allows it, drop capabilities you do not need, and pull official or well-known images rather than random ones. A short checklist for a new host:
- Do not publish ports you are not using. Each
-pis an open door. - Keep the engine current with
sudo apt update && sudo apt upgradeor thednfequivalent. - Pin image tags to a version, such as
postgres:16, instead oflatest, so an upgrade is a deliberate choice. - Remember that outbound port 25 is blocked on Harborvm, so a container that sends mail must use a relay on 587 or 465.
For Harborvm's private networking, bind internal services to the private network or a WireGuard VPN interface rather than a forwarded port. Then only machines you trust can reach them. Our walkthrough on setting up a VPN on your VPS covers the WireGuard side.
Managing Multi-Container Apps with Compose
Real apps are rarely one container. Docker Compose describes a whole stack in a single docker-compose.yml file so you can start everything with one command. Here is a web app with a Postgres database:
services:
web:
image: nginx
ports:
- "8080:80"
restart: unless-stopped
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: change_me
volumes:
- pgdata:/var/lib/postgresql/data
restart: unless-stopped
volumes:
pgdata:
Save that file and bring the stack up in the background:
docker compose up -d
docker compose ps
docker compose logs -f
Compose creates a private network so the containers reach each other by name. The web service can talk to db on the hostname db with no extra setup. To stop everything, run docker compose down; add -v only when you also want to delete the volumes.
Keep your docker-compose.yml in version control or at least copied off the server, for the same reason volumes need their own backup. The file plus your volumes is the whole state of your app, and on Harborvm you are the one holding the copy.
Your next step is practical: spin up a Linux VPS, run the install commands above, and bring up a one-container Nginx to confirm port mapping works. Once that is solid, move your real stack into a Compose file. A Harborvm Small plan at $10 a month, ready in about a minute on Ubuntu or Debian, is enough to get a Docker host running today.
Frequently asked questions
How much RAM does a VPS need to run Docker?
A small Linux VPS with 2 GB of RAM runs the Docker engine and a few light containers comfortably. Heavier stacks like databases or several services at once want 4 GB or more.
Can I run Docker inside a VPS, or do I need bare metal?
You can run Docker on a KVM VPS because KVM gives each VM a real kernel and the control groups Docker needs. Containers on OpenVZ-style VPS often fail for that reason.
Do I need Docker Compose as a separate install?
No. Modern Docker ships Compose as the built-in 'docker compose' plugin. If you install from Docker's own repository, you get it automatically with no extra package.
Will my containers survive a reboot?
Only if you set a restart policy. Add --restart unless-stopped to docker run, or restart: unless-stopped in Compose, and Docker starts the container again after the VPS reboots.