Skip to content

Offer: your first month of web hosting is free on monthly plans. Order by 7 Jan. See plans

HostEthical
All guides

Guides 15 min read ·

Docker on a VPS: Install Docker on Ubuntu 24.04 and Deploy a Compose App Securely

Run Docker on a VPS in 2026: why KVM matters, installing Docker Engine on Ubuntu 24.04, a Compose app with Postgres behind Nginx, backups and security.

A Docker VPS is a virtual private server with Docker Engine installed, so you can run your apps, databases and tools as containers on a server you control. The VPS is the machine and Docker is software that runs containers on it, so "Docker vs VPS" isn't a choice between two products. For production, use a KVM VPS running Ubuntu 24.04, install Docker from Docker's own apt repository, publish container ports on 127.0.0.1 only, and put Nginx with a Let's Encrypt certificate in front.

This guide uses Docker Engine 29.10 and Docker Compose 5.8 (released on 16 and 17 November 2026) on Ubuntu 24.04 LTS, with PostgreSQL 18, Nginx and Portainer CE 2.45 LTS, as of November 2026.

Docker vs VPS: what is the difference?

A VPS is a virtual machine with its own operating system, root access, IP address and a fixed share of CPU, RAM and disk. Docker is a program you install on that operating system. Each container gets its own filesystem, processes and network, but all of them share the host's Linux kernel; Docker's security documentation explains that the isolation comes from kernel namespaces and control groups.

So you rent the VPS and run Docker on it. Docker adds packaging: your app, its runtime and its libraries ship as one image, and a compose.yaml file describes the whole stack, so it runs the same on your laptop and your server. The real alternative is a managed platform that runs your image for you: less work, though usually billed in US dollars per service. See Vercel alternatives in India.

Can every VPS run Docker? What KVM means

To Docker, a KVM VPS looks like a real computer. The KVM project describes full virtualisation in which "each virtual machine has private virtualized hardware", and your VPS boots its own Linux kernel, so Docker installs as it would on a physical server.

Container-based VPS products, built on LXC or LXD, share the host's kernel (Proxmox's documentation says such containers "use the kernel of the host system"). Docker there is a container inside a container, and only works if the provider has prepared for it: the LXD FAQ says nesting must be switched on, and that LXD containers "cannot load kernel modules", so the host must load any Docker needs. Before buying a cheap VPS for Docker, ask which virtualisation it uses. On a server you already have, run systemd-detect-virt: it prints kvm on a KVM machine and names such as lxc or openvz on container-based ones (systemd manual). HostEthical's Cloud VPS plans are KVM.

What are the Docker VPS minimum requirements?

Docker's Ubuntu install page asks for 64-bit Ubuntu 22.04, 24.04 or 26.04 LTS (or 26.10) on x86_64, arm64 and a few other architectures; its Debian page covers Debian 12 and 13. Docker publishes no RAM minimum for Docker Engine, which is light. What you run inside decides the size:

  • RAM is the sum of your containers plus room for builds, and building an image on the server needs far more memory than running it. A database uses roughly what you configure: PostgreSQL's shared_buffers is typically 128 MB by default. Check real use with docker stats.
  • Disk fills with images, volumes and logs. Docker keeps container logs with no size limit by default; step 6 below fixes that.
  • CPU matters most for builds and busy apps.

Dashboards add overhead: Coolify and Dokploy officially need at least 2 GB of RAM before your apps. VPS Starter's 8 GB leaves room for builds and a second stack.

How to install Docker on a VPS (Ubuntu 24.04), step by step

These steps assume a fresh Ubuntu 24.04 server and a non-root sudo user, called deploy here. If you don't have one yet, follow how to set up a VPS first.

1. Open only SSH, HTTP and HTTPS in ufw

sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Allow SSH before enabling the firewall, or you'll lock yourself out. One catch: Docker's documentation says Docker and ufw are incompatible. Traffic to a published container port is routed in the nat table before it reaches ufw's chains, so a published port is open to the internet whatever ufw says. The Compose file below avoids that by binding to 127.0.0.1.

2. Remove Ubuntu's docker.io package and the snap

Docker calls distribution packages like these unofficial, and says they may conflict with its own and must be removed first. Ubuntu 24.04's docker.io is version 29.1.3, against 29.10.0 from Docker. The docker snap is built by Canonical and "not endorsed or published by Docker, Inc.", and Portainer's install guide advises against it because of compatibility issues.

sudo apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc docker-buildx podman-docker containerd runc | cut -f1)
snap list docker 2>/dev/null && sudo snap remove docker

3. Add Docker's apt repository and install Docker Engine

This is Docker's official method: it saves Docker's signing key as /etc/apt/keyrings/docker.asc, and the Signed-By line in docker.sources makes apt accept packages from that repository only when they're signed with it.

# Add Docker's official GPG key
sudo apt update
sudo apt install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to apt sources
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

On Debian 12 or 13, change linux/ubuntu to linux/debian in both URLs (Debian install page) and use $(. /etc/os-release && echo "$VERSION_CODENAME") for the suite. Skip the get.docker.com convenience script on a production server: Docker says it isn't recommended for production, partly because it installs the latest stable release by default, which can bring unexpected major upgrades.

4. Check that Docker works

sudo systemctl status docker --no-pager
sudo docker run hello-world
docker compose version

On Ubuntu and Debian the Docker service starts at boot by default (post-installation steps). Compose is a CLI plugin, so the command is docker compose, with a space.

5. Run docker without sudo, knowing it means root

sudo usermod -aG docker $USER
exit          # log out, then SSH in again so the new group applies
docker run --rm hello-world

Read Docker's warning first: the docker group "grants root-level privileges to the user". A member can start a container with the host's / mounted, which Docker's security page says can "alter your host filesystem without any restriction". Add only the admin user you'd trust with sudo anyway.

The alternative is rootless mode, which runs the daemon and containers as your own user:

sudo apt install -y uidmap docker-ce-rootless-extras
sudo systemctl disable --now docker.service docker.socket
sudo rm /var/run/docker.sock
dockerd-rootless-setuptool.sh install     # as your user, without sudo
sudo loginctl enable-linger $USER         # starts it at boot, keeps it running after logout

Its documented trade-offs: ports below 1024 need extra configuration, -p doesn't pass the visitor's IP through by default, and Portainer's instructions assume Docker runs as root. The rest of this guide uses the standard install.

6. Turn on log rotation before you start any containers

Docker's default logging driver, json-file, writes container output to files that the post-installation guide warns can grow until the disk fills; its max-size defaults to unlimited. Create /etc/docker/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}
sudo systemctl restart docker

That keeps at most three 10 MB files per container. The values must be quoted strings, and the setting applies only to containers created afterwards, so recreate existing ones with docker compose up -d --force-recreate. The local driver is an alternative that rotates by default.

How to deploy a Docker Compose app to a VPS

The example is a typical stack: your web app, built from the Dockerfile in your repository and listening on port 3000, plus PostgreSQL, served by Nginx at https://app.example.com. First point an A record for that name at the server's IP (see DNS records explained).

1. Put the code, the secrets and compose.yaml in one folder

git clone https://github.com/your-name/myapp.git ~/myapp
cd ~/myapp
echo "POSTGRES_PASSWORD=$(openssl rand -hex 32)" > .env
chmod 600 .env
nano compose.yaml

Compose reads .env in the project folder to fill in variables such as ${POSTGRES_PASSWORD}; keep it in .gitignore. A hex password can't break the database URL. Paste this into compose.yaml:

services:
  app:
    build: .                       # or image: ghcr.io/your-name/myapp:1.4.2
    restart: unless-stopped
    environment:
      DATABASE_URL: postgres://myapp:${POSTGRES_PASSWORD}@db:5432/myapp
    ports:
      - "127.0.0.1:3000:3000"      # reachable only from this server
    volumes:
      - uploads:/app/uploads
    depends_on:
      db:
        condition: service_healthy
    mem_limit: 1g
    cpus: 2
    security_opt:
      - no-new-privileges:true

  db:
    image: postgres:18
    restart: unless-stopped
    environment:
      POSTGRES_USER: myapp
      POSTGRES_DB: myapp
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - pgdata:/var/lib/postgresql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U myapp -d myapp"]
      interval: 10s
      timeout: 5s
      retries: 5
    mem_limit: 1g

volumes:
  pgdata:
  uploads:
  • 127.0.0.1:3000:3000: the Compose reference warns that a port with no host IP binds to all interfaces, "bypassing host firewall rules". With 127.0.0.1, only the Docker host can reach it, which is all Nginx needs.
  • No ports on the database. The app reaches it as db on the project's private network; nothing outside the server can.
  • restart: unless-stopped brings containers back after a crash or reboot, but not after you stop one yourself (restart policies).
  • condition: service_healthy makes Compose wait for the Postgres healthcheck before starting the app.
  • mem_limit and cpus cap each container, so one leaking service can't starve the rest.
  • /var/lib/postgresql is the mount point from PostgreSQL 18 onwards; the official image moved it from /var/lib/postgresql/data. Pin the major version: PostgreSQL 19 came out on 19 November 2026, and moving between majors needs a dump and restore or pg_upgrade (versioning policy). The password applies only when the database is first created, so changing .env later won't change it.

2. Start the stack

docker compose config --quiet     # validates the file; silent if it's fine
docker compose up -d --build
docker compose ps
docker compose logs -f app        # Ctrl+C stops following
curl -I http://127.0.0.1:3000

3. Put Nginx in front and add HTTPS

sudo apt install -y nginx certbot python3-certbot-nginx
sudo nano /etc/nginx/sites-available/app.example.com
server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
sudo certbot --nginx -d app.example.com
sudo certbot renew --dry-run

Certbot gets a Let's Encrypt certificate and adds it to the Nginx config. As of November 2026 Let's Encrypt certificates last 90 days, dropping to 64 days on 10 February 2027 and 45 days on 16 February 2028 (Let's Encrypt's schedule). Ubuntu's certbot package installs a systemd timer that renews them automatically, and the dry run confirms renewal works. For WebSockets, add the upgrade headers from our Node.js hosting guide.

To keep everything in containers, run Caddy as a Compose service instead, publishing only its ports 80 and 443 and proxying to app:3000. Caddy gets and renews certificates automatically from Let's Encrypt or ZeroSSL if DNS points at the server, ports 80 and 443 are reachable and its data directory is persistent. Use one or the other: only one program can own those ports.

A KVM server that runs Docker properly. VPS Starter gives you 4 vCPU, 8 GB RAM, full root and a dedicated IPv4, in Mumbai or Germany, billed in rupees. See VPS plans →

How do you update containers and back up Docker volumes?

cd ~/myapp
git pull                                  # your new code
docker compose pull --ignore-buildable    # newer images, such as the latest postgres:18.x
docker compose build --pull               # rebuilds your app on a fresh base image
docker compose up -d                      # recreates only what changed
docker image prune -f                     # deletes old, untagged images

docker compose up -d recreates only containers whose image or configuration changed, keeping their volumes. If CI builds your images and pushes them to a registry, give the app an image: with a version tag, change the tag on release, then docker compose pull and docker compose up -d. Docker Engine updates with the system through sudo apt update && sudo apt upgrade, and unless-stopped containers return when the daemon restarts.

Back up the database with pg_dump, not by copying its volume: PostgreSQL's documentation says the server "must be shut down" for a usable file-level backup. Archive other volumes through a throwaway container, as Docker documents:

mkdir -p ~/backups
cd ~/myapp

# database: a consistent SQL dump while it runs
docker compose exec -T db pg_dump -U myapp myapp | gzip > ~/backups/myapp-$(date +%F).sql.gz

# uploads volume, mounted read-only into a temporary container
docker run --rm -v myapp_uploads:/data:ro -v "$HOME/backups":/backup alpine \
  tar czf /backup/uploads-$(date +%F).tar.gz -C /data .

Compose prefixes volume names with the project name (the folder name by default), so uploads becomes myapp_uploads. Check the name with docker volume ls first: given a name that doesn't exist, docker run creates an empty volume and you'd archive nothing. For a nightly dump at 02:30, add this with crontab -e (cron needs % escaped):

30 2 * * * cd /home/deploy/myapp && docker compose exec -T db pg_dump -U myapp myapp | gzip > /home/deploy/backups/myapp-$(date +\%F).sql.gz

Copy backups off the VPS, because a backup on the same disk dies with the disk. And beware docker compose down -v: the -v removes named volumes, your database included.

Docker VPS security: what should you lock down?

  • Publish only what should be public. Bind to 127.0.0.1 or leave ports out. To filter traffic to a published port, Docker's install guide says to add iptables rules to the DOCKER-USER chain, since ufw won't see it.
  • Keep the Docker API on its Unix socket. If the API is exposed over TCP, Docker calls protecting it with HTTPS and certificates "mandatory". A single VPS has no reason to expose it.
  • Think before mounting /var/run/docker.sock into a container. The OWASP Docker Security Cheat Sheet says socket access "is equivalent to giving unrestricted root access to your host", even mounted read-only. Tools that need it, such as Portainer, should be reachable only by you.
  • Run containers as a non-root user with USER in the Dockerfile or user: in Compose; otherwise the process runs as root in the container.
  • Remove what the app doesn't need. OWASP recommends no-new-privileges (in the example), dropping all capabilities and adding back only those needed, and a read-only filesystem: cap_drop: [ALL] and read_only: true in Compose, with a tmpfs for folders the app writes to. Test each change; some images need a capability to start.
  • Keep the host, Docker and images updated, and pin image versions so updates happen when you choose.

Should you add Portainer to your Docker VPS?

Portainer CE is a free web dashboard for containers, logs, volumes and Compose stacks, useful if you'd rather click than type. The current release is 2.45.4 LTS (18 November 2026). Its install guide publishes port 9443 on every address, plus port 8000, which only Edge agents use. This version keeps the interface on localhost:

docker volume create portainer_data
docker run -d -p 127.0.0.1:9443:9443 --name portainer --restart=always \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v portainer_data:/data portainer/portainer-ce:lts
docker logs portainer 2>&1 | grep setup_token

From your own computer, open a tunnel, then browse to https://localhost:9443 and accept the self-signed certificate warning:

ssh -L 9443:127.0.0.1:9443 deploy@YOUR_SERVER_IP

New instances ask for the setup token from the logs, and the admin user must be created within five minutes or Portainer stops; restarting the container gives you five more. Portainer holds the Docker socket, so guard its admin password like root's.

When should you use Coolify or Dokploy instead?

Plain Compose suits a few apps you deploy by hand and want to understand fully. If you'd rather push to Git and have the server build, deploy and add HTTPS to each app, with backups and logs in a dashboard, install Coolify or Dokploy. Both install Docker themselves and run their own proxy on ports 80 and 443, so start them on a fresh server rather than beside host Nginx. Coolify vs Dokploy compares them.

A single app may not need Docker at all: a Node.js API runs well under PM2 and Nginx (Node.js hosting in India), and deploying Next.js on a VPS covers both routes.

Running Docker on a HostEthical VPS

HostEthical's Cloud VPS is unmanaged KVM with full root access, a dedicated IPv4 address and SSD storage, so Docker installs exactly as above. Ubuntu 24.04 and 26.04 LTS and Debian 12 and 13 are among the OS images, all on Docker's supported list, and the server is built automatically after payment, usually within 5 to 15 minutes. Plans start at ₹860 a month, or about ₹774.17 a month when billed yearly. You pay in rupees by UPI or binance Pay (USDT), with no dollar card charges, and the price stays the same at renewal.

PlanvCPURAMStoragePaid monthlyPaid up front, per monthAt renewal
VPS Starter4 cores8 GB100 GB SSD₹860₹774.17 (yearly)Same price
VPS Pro6 cores12 GB200 GB SSD₹1,170₹1,053.33 (yearly)Same price
VPS Max8 cores24 GB300 GB SSD₹2,190₹1,971.67 (yearly)Same price

VPS Starter's 4 vCPU and 8 GB RAM is plenty for most single Compose apps, and for several small ones. Move to VPS Pro or Max to build large images on the server, run several stacks with their own databases, use Coolify with preview deployments, or run local AI models (see Ollama on a VPS). Choose Mumbai if your users are in India: requests travel a shorter distance and your data stays in India, for ₹380 a month more than Germany. Germany suits users in Europe, internal tools and bots; VPS hosting in Mumbai covers the trade-off.

You can also register your domain with us and point its A record at the VPS with our nameservers and DNS editor. If n8n is all you need, our n8n VPS comes with it preinstalled.

Run your Compose stack this week. Pick VPS Starter in Mumbai or Germany, pay by UPI, and follow the steps above. See Cloud VPS →

Frequently asked questions

Is Docker free to use on a VPS?

Yes. Docker Engine, the part you install on a Linux server, is open source under the Apache License 2.0 and costs nothing. Docker's paid subscriptions concern Docker Desktop, which larger companies (more than 250 employees or more than US$10 million in annual revenue) must pay for in commercial use. A VPS running Docker Engine from Docker's apt repository doesn't use Docker Desktop at all.

Should I type docker compose or docker-compose?

Use docker compose, with a space. The hyphenated docker-compose was Compose v1, a Python tool first released in 2014; Docker's documentation lists only Compose v2 and v5 as supported, and both run as a plugin of the Docker CLI. As of November 2026, Docker's apt repository installs Compose 5.8.0 as the docker-compose-plugin package. Old Compose files usually still work, but the top-level version: line is ignored.

Why can't my container connect to Postgres on localhost?

Inside a container, localhost means that container, not the server or the database container. Compose puts every service in a project on a shared network named after the project (for example myapp_default) and registers each service name in an internal DNS server, so the app should connect to db:5432, using the service name from compose.yaml. The database then needs no published port at all.

Can I run several Docker Compose apps on one VPS?

Yes. Give each app its own folder, so Compose treats it as a separate project with its own network and volumes, and publish each app on a different loopback port, such as 127.0.0.1:3000 and 127.0.0.1:3001. Add one Nginx server block per domain pointing at the right port and run certbot for each hostname. Watch total memory with docker stats, because every stack shares the server's RAM.

How do I move a Docker Compose app to a new VPS?

Install Docker on the new server, copy the project folder (compose.yaml, .env and your code) across with scp or rsync, and start only the database with docker compose up -d db, then wait until docker compose ps shows it as healthy. Restore your latest dump with gunzip -c backup.sql.gz piped into docker compose exec -T db psql -U myapp myapp, unpack any volume archives with a temporary container, then run docker compose up -d. Change your DNS A record to the new IP once the site works there.