Deploy a Docker Compose app to a VPS with HTTPS
To serve a Docker Compose app over HTTPS on a VPS, point a DNS record at the server, allow only SSH, HTTP and HTTPS through the firewall, and put one reverse proxy container in front of your app. The proxy is the only service that publishes ports 80 and 443; it obtains and renews Let's Encrypt certificates and forwards requests to your app over a private Compose network. Databases and internal services publish no ports at all.
This guide walks through that setup with Caddy as the main example, because its automatic HTTPS needs the least configuration. Traefik and Nginx with Certbot follow the same shape. Every command runs on a server you manage; nothing here configures a DeployifyNow project.
How do I deploy a Docker Compose app to a VPS with HTTPS?
Create an A record (and an AAAA record if the server has IPv6) for your hostname that points to the VPS. Allow inbound SSH, 80 and 443 only. Add a reverse proxy service such as Caddy to your Compose file, publish only its ports 80 and 443, and keep your app and database on networks without published ports. Validate the file with docker compose config --quiet, start it with docker compose up -d --wait, and confirm https:// responds with a valid certificate.
Do I need Nginx, Traefik or Caddy for HTTPS with Docker Compose?
You need exactly one reverse proxy, and any of the three works. Caddy issues and renews certificates automatically from a few lines of configuration. Traefik discovers containers through labels and also has built-in ACME certificate management. Nginx is familiar and flexible, but you run Certbot or another ACME client alongside it and must make sure renewals reload Nginx. Pick the one your team can operate, and let only that container publish ports 80 and 443.
Should I publish my database port in Docker Compose?
No. Services on the same Compose network reach each other by service name, so your app can connect to db:5432 without any ports: entry. Docker's documentation warns that publishing container ports is insecure by default because ports bind to all host addresses, and that published traffic is diverted before ufw rules apply. A 5432:5432 mapping can therefore be reachable from the internet even when ufw appears to deny it. For administrative access, bind to 127.0.0.1 and connect through an SSH tunnel.
Why is my Let's Encrypt certificate not being issued?
The usual causes are DNS that does not yet resolve to the server, port 80 or 443 blocked by a host or provider firewall, or an AAAA record pointing at an address where the proxy is not listening. The HTTP-01 challenge can only be completed on port 80, and Let's Encrypt validates whichever address your DNS returns. Check dig +short for both A and AAAA records, confirm both ports are open from outside the server, and read the proxy logs for the exact validation error before retrying.
Before you start
- A VPS with a public IPv4 address and SSH key access. Let's Encrypt must be able to reach it from the internet.
- A domain where you can edit DNS records.
- Docker Engine with the Compose plugin installed from Docker's official packages.
- An application image that listens on a known port and exposes a health endpoint, for example
/healthz.
The examples use app.example.com, an application listening on port 3000 and a PostgreSQL database. Replace them with your own values.
Step 1: Point DNS at the server
Create the records at your DNS provider. A short TTL during setup makes mistakes cheaper to correct.
| Type | Name | Value | When to add it |
|---|---|---|---|
| A | app | The VPS public IPv4 address | Always |
| AAAA | app | The VPS public IPv6 address | Only if the server has IPv6 and the proxy listens on it |
Check the records with the free DNS record worksheet and then from your terminal:
dig +short app.example.com A
dig +short app.example.com AAAA
Do not continue until the A record returns the server address. If an AAAA record exists, it must also point at this server; otherwise IPv6 clients and certificate validation can reach the wrong host.
Step 2: Open only SSH, HTTP and HTTPS
With ufw on Ubuntu or Debian, allow SSH before enabling the firewall so you do not lock yourself out:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 443/udp # optional, for HTTP/3
sudo ufw enable
Treat ufw as one layer, not the whole boundary. Docker documents that traffic to published container ports is diverted before ufw rules apply, so the reliable rule is to publish only the proxy's ports. If your hosting provider offers a network firewall, apply the same three openings there as a second layer.
Step 3: Write the Compose file and Caddyfile
The proxy joins an edge network with the app. The database lives on an internal network that has no route to the outside, and it has no ports: entry.
services:
caddy:
image: caddy:2
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy_data:/data
- caddy_config:/config
networks: [edge]
depends_on:
app:
condition: service_healthy
app:
image: ghcr.io/example/app:1.4.2
restart: unless-stopped
env_file: .env
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:3000/healthz"]
interval: 10s
timeout: 3s
retries: 5
networks: [edge, internal]
depends_on:
db:
condition: service_healthy
db:
image: postgres:17
restart: unless-stopped
env_file: .env
volumes:
- db_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 5
networks: [internal]
networks:
edge:
internal:
internal: true
volumes:
caddy_data:
caddy_config:
db_data:
The Caddyfile next to it needs only the hostname and the upstream service:
app.example.com {
encode zstd gzip
reverse_proxy app:3000
}
A few details matter here:
- Persist
/data. Caddy stores certificates and ACME account data there. Losing the volume on every redeploy forces new certificate requests and can run into the certificate authority's rate limits. - Use a health command that exists in your image. The example uses
wget; minimal images may needcurlor a small built-in check instead. - Pin image tags. A versioned tag such as
1.4.2makes rollback a one-line change.latestmakes it guesswork. - Keep secrets in
.env, outside Git. Restrict it withchmod 600 .env. The doubled$$in the database health check stops Compose from interpolating those variables, so the container's own environment supplies them.
Need to translate a ports: mapping or decide between loopback and all-interface binding? The Compose port mapping builder shows short and long syntax side by side. Before the first start, compare your .env with a reference file in the env file key checker to catch missing or empty keys.
Step 4: Validate, start and verify
Validate the merged configuration first. --quiet only reports errors:
docker compose config --quiet
Without --quiet, docker compose config prints the fully resolved model, including values interpolated from .env. Read it on the server and do not paste it into tickets or chat.
Start the stack and wait for health checks to pass:
docker compose up -d --wait
docker compose ps
docker compose logs --tail=50 caddy
curl -I https://app.example.com
The first HTTPS request can take a few seconds while Caddy completes the ACME challenge. Then confirm from a different machine that only the proxy is reachable, for example with nc -zv <server-ip> 5432; a refused or timed-out connection is the expected result for the database port.
Step 5: Use health checks for what they can do
A Compose health check runs a command inside the container and marks it healthy or unhealthy. depends_on with condition: service_healthy delays dependent services until that check passes, and docker compose up --wait uses the same signal to decide when startup is complete.
A health check does not restart a container by itself. Docker Engine's restart policies react to the container process exiting, not to an unhealthy status. Keep the check focused on whether the process can serve requests, make the application exit on fatal errors so restart: unless-stopped can act, and send health status to the monitoring you actually watch.
Step 6: Update and roll back deliberately
Change the image tag in compose.yaml, then pull and recreate only the app:
docker compose pull app
docker compose up -d --no-deps --wait app
On a single host, recreating a container briefly interrupts requests to it. Compose does not provide zero-downtime rollouts on its own; plan a short maintenance window or accept the brief gap.
To roll back, restore the previous tag and run the same two commands. This only works cleanly when the database schema still supports the previous version. Prefer backward-compatible migrations that add before they remove, and take a fresh database backup before a migration that cannot be reversed.
Record the current tag, intended tag, configuration changes and post-deploy checks in the deployment preflight checklist before you update. If a release goes wrong, the rollback decision and evidence worksheet helps you separate an image rollback from a data recovery decision.
Step 7: Back up what you cannot rebuild
Images, the Compose file and the Caddyfile can be rebuilt from Git. The data cannot:
| What | How | Notes |
|---|---|---|
| PostgreSQL data | docker compose exec -T db pg_dump -U app -Fc app > app.dump | Copy the file off the server. A copy on the same disk is not a backup. |
| Uploaded files and other named volumes | Archive the volume contents while writes are paused, or use your storage provider's snapshots | Confirm which volumes hold state with docker volume ls. |
.env and other secrets | Store in a password manager or secret store | Never commit them, and keep them separate from data backups. |
A scheduled dump only proves that a file was written. Restore it into a separate database on a regular schedule and check the data your application needs. For a fuller comparison of logical dumps, physical base backups and point-in-time recovery, read PostgreSQL backups: pg_dump vs pg_basebackup and point-in-time recovery.
Troubleshooting
| Symptom | Likely cause | What to check |
|---|---|---|
| Browser shows a certificate warning | Certificate not yet issued, or DNS points elsewhere | docker compose logs caddy, then dig +short for A and AAAA records |
502 Bad Gateway from the proxy | App container not running, unhealthy or on a different network | docker compose ps; confirm the app and proxy share the edge network and the upstream port matches |
| App cannot reach the database | Wrong hostname or the database is not healthy yet | Use the service name db as the host; check docker compose logs db |
| Database port reachable from the internet | A ports: mapping was added to the database service | Remove it, or bind it to 127.0.0.1, then run docker compose up -d |
| New certificate requested after every deploy | The /data volume is not persistent | Use a named volume for caddy_data and avoid docker compose down -v |
When a managed platform fits instead
Running Compose on your own VPS gives you full control, and with it the full operating load: operating-system and Docker updates, proxy configuration, firewall rules, backups, restore tests and monitoring. That trade-off is reasonable for many teams. It is worth comparing with a managed platform when that load is the part you would rather not own.
DeployifyNow is built for that case. Today it lets you:
- Connect a GitHub repository and deploy from the branch you choose, building with a Dockerfile, Nixpacks or Buildpacks. See GitHub Integration.
- Manage environment variables per service with Build, Runtime or Both scopes; values are encrypted at rest. See Environment Variables.
- Add custom domains with automatically provisioned Let's Encrypt certificates and HTTP-to-HTTPS redirects. See Custom Domains.
- Follow build progress and logs on the project's Deployments page.
Be clear about the current scope before you choose. The free browser pilot uses support-assisted GitHub setup and covers one stateless Dockerfile application on shared infrastructure; managed databases are outside the pilot. If your Compose stack depends on a database you must run yourself today, the self-managed setup above remains the practical path. Getting Started describes the pilot, and pricing lists current plan limits.
Sources
- Caddy: Automatic HTTPS — requirements for public names, HTTP-to-HTTPS redirects and default certificate authorities.
- Caddy: Running Caddy with Docker Compose — published ports and the
/dataand/configvolumes. - Let's Encrypt: Challenge types — HTTP-01 runs only on port 80 and cannot issue wildcard certificates.
- Docker: Port publishing and mapping — default all-address binding and loopback-only publishing.
- Docker: Packet filtering and firewalls — published ports and ufw.
- Docker: docker compose config and Control startup order — validation, health checks and
service_healthy.