Skip to content

Deploy a small web app on an Ubuntu VPS with Docker and HTTPS

A complete two-container example: your app stays on a private Docker network while Caddy handles the public HTTPS connection.

What this example deploys

This guide deploys a minimal Node.js HTTP app and a Caddy reverse proxy on one fresh Ubuntu 24.04 LTS server. It is a deployment smoke test: no database, accounts or customer data. Once the route works, replace the demonstration app with your own tested application image. This setup is self-administered; it does not establish high availability or include application management.

  • A Linux VPS with sudo access, an Ubuntu image supported by Docker, and resources appropriate to your application and builds.
  • A domain or subdomain you control, with its A record pointing to the VPS public IPv4 address. Add an AAAA record only if IPv6 routing and firewall access work.
  • TCP ports 80 and 443 available to the internet, plus SSH access limited to your administration network where possible. Keep your existing SSH session open while adjusting access.
  • A clean server or a reviewed port-allocation plan. An existing web server may already occupy 80 or 443.

1. Install Docker and prepare a project directory

Use Docker’s official Ubuntu repository installation instructions, including the Docker Compose plugin. Resolve any existing package conflicts before changing an established server. The commands here use sudo instead of adding a user to the Docker group; Docker administration grants powerful access to the host.

Run on your VPS after the official Docker installation.
sudo docker version
sudo docker compose version
sudo apt update
sudo apt install nano curl
sudo install -d -m 0755 /opt/web-app
cd /opt/web-app
sudo nano server.mjs

Paste this demonstration app into server.mjs. It listens on the container interface, answers /health separately and returns a plain response at /. Other paths return 404.

server.mjs — a smoke test, not a production application.
import {createServer} from 'node:http';

const server = createServer((request, response) => {
  if (request.url !== '/' && request.url !== '/health') {
    response.writeHead(404, {'Content-Type': 'text/plain'});
    response.end('Not found\n');
    return;
  }
  response.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
  response.end(request.url === '/health' ? 'ok\n' : 'Deployment OK\n');
});

server.listen(3000, '0.0.0.0');

2. Build the app and define the private network

Create /opt/web-app/Dockerfile with the following content. Node.js 24 is an LTS release at this guide’s publication. The example builds the source into the image and runs it as the image’s unprivileged node user. Recheck supported versions when maintaining the deployment.

Dockerfile
FROM node:24-alpine
WORKDIR /app
COPY --chown=node:node server.mjs .
USER node
EXPOSE 3000
CMD ["node", "server.mjs"]

Create /opt/web-app/compose.yaml. Only Caddy publishes host ports. The app is addressed as app:3000 on the Compose network; EXPOSE in the Dockerfile does not publish that port to the internet. The local logging driver rotates container output so a stream of logs has a defined retention limit.

compose.yaml — keep application and administration ports unpublished.
name: web-app
services:
  app:
    build: .
    restart: unless-stopped
    read_only: true
    cap_drop:
      - ALL
    security_opt:
      - no-new-privileges:true
    logging:
      driver: local
      options:
        max-size: "10m"
        max-file: "3"
  caddy:
    image: caddy:2-alpine
    restart: unless-stopped
    ports:
      - "80:80/tcp"
      - "443:443/tcp"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    logging:
      driver: local
      options:
        max-size: "10m"
        max-file: "3"
volumes:
  caddy_data:
  caddy_config:

Caddy’s named data volume persists certificate state across container recreation. Do not publish its administration port, mount the Docker socket into these containers, or add a public mapping for a future database. The app is read-only because this demonstration writes no data; a real app may need specific writable mounts or temporary storage.

3. Configure your domain and start HTTPS

Create /opt/web-app/Caddyfile and replace app.example.com with your real hostname. Use the bare hostname shown below. Caddy can request and renew a public certificate when DNS, network access and persistent writable certificate storage meet its requirements. An incorrect IPv6 record can break validation even when IPv4 works.

Caddyfile — replace the example hostname before starting.
app.example.com {
    reverse_proxy app:3000
}
Validate the files, then build and start both containers.
cd /opt/web-app
sudo docker compose config --quiet
sudo docker compose run --rm --no-deps caddy caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile
sudo docker compose up -d --build
sudo docker compose ps
sudo docker compose logs --tail=100 caddy app

4. Verify the public route and isolate failures

Run these checks from your own computer or another external network, replacing the hostname. The HTTP request should redirect to HTTPS; the HTTPS request to /health should return 200 and ok. A running container alone does not verify the public route or certificate.

Run externally with your actual hostname.
curl -I http://app.example.com
curl -i https://app.example.com/health
curl -i https://app.example.com/
SymptomCheck first
Timeout before any responseDNS A/AAAA records, provider firewall, host rules, and whether Caddy owns ports 80/443.
Certificate or validation errorThe exact hostname, public DNS, both IP families if advertised, outbound access and Caddy logs. Do not repeatedly retry bad DNS.
502 from CaddyApp logs, matching internal port 3000, and binding to 0.0.0.0 inside the app container. localhost in Caddy would refer to Caddy itself.
App restarts or disappearsContainer exit messages, memory pressure and free disk space. Check the application before changing the server size.
Read-only capacity and runtime checks on the VPS.
sudo docker compose ps
sudo docker compose logs --tail=100 app caddy
sudo docker stats --no-stream
df -h
sudo docker system df

5. Replace the demo safely and make recovery repeatable

For your real application, build a tested release image, keep its previous image available for rollback, and direct Caddy to its internal listening port. Pin deployed image versions or digests after testing. Do not run a development server, publish a database, or bake production credentials into an image. Configure the application’s trusted-proxy behavior according to its own documentation.

  • Protect deployment files and secrets with appropriate filesystem permissions. Keep credentials out of source control and logs.
  • Copy configuration and application releases securely off the VPS. Back up persistent volumes and use application-aware database dumps or snapshots with a documented consistency procedure.
  • Test restoration on a separate environment, including secrets, uploaded files and DNS. A same-disk copy cannot recover a lost VPS.
  • Monitor disk usage, application health and certificate renewal. Log rotation limits container logs, not uploads, database growth or accumulated images.
  • Schedule operating-system and image updates, verify after a restart, and retain a rollback path. Single-server updates can interrupt service.

For this demonstration, update deliberately with docker compose build --pull app, docker compose pull caddy, then docker compose up -d. Re-run the external checks afterward. Review changes and backups first on a real app. Avoid docker compose down -v or blind volume pruning: removing volumes can delete persistent data.

Common questions

How much RAM does a Docker VPS need?

Use your application’s supported requirements and representative peak measurements, including build jobs, databases and backups. This small demonstration is not a capacity benchmark or a guarantee that a particular plan will handle production traffic.

Should I open port 3000 on the VPS?

Not for this topology. Caddy reaches app:3000 inside the Compose network. Only the reverse proxy publishes web ports; the application port and any database or administration ports remain unpublished.

Is Docker Compose on one VPS highly available?

No. A host, disk or network failure can interrupt the whole stack. Restart policies help with some process failures; they do not replace independent backups, monitoring or a multi-host recovery design.

Can I use an existing domain with VPSRay?

Yes, a domain can point to a server through DNS regardless of where it is registered. Configure the record at your authoritative DNS provider, verify the exact server address and review any proxy settings before following this guide.

Official documentation

Documentation reviewed October 26, 2026. Check current product terms and the linked documentation before choosing a setup. The examples in this guide have not been run on your VPS.