# How to install Portainer with Docker and Caddy on a VPS | Nuvits

Source: [https://nuvits.net/en/blog/install-portainer-docker-caddy-vps](https://nuvits.net/en/blog/install-portainer-docker-caddy-vps)

Published 2026-11-30, by Fernando Chagas.

This tutorial installs Portainer CE 2.45.4 LTS on an Ubuntu 24.04 VPS with Docker Compose and puts the dashboard behind Caddy 2.11.9 with automatic HTTPS. Only ports 80 and 443 are open: Portainer's 9443, 9000 and 8000 are never published. It also covers the setup token introduced in 2.45, the 5-minute window to create the administrator, the first stack, backup with a restore, and the upgrade. Everything was tested on 30 November 2026 on a clean Ubuntu 24.04.5 LTS machine.

![Installation diagram: the internet reaches Caddy 2.11.9 on ports 80 and 443, Caddy forwards to Portainer CE 2.45.4 on port 9000 of the internal Docker network, Portainer controls Docker through the socket, and ports 9443, 9000 and 8000 are not published on the VPS](https://nuvits.net/blog/install-portainer-docker-caddy-vps/portainer-docker-caddy-vps-architecture.webp)

## What Portainer does and what it can reach

Portainer is a web dashboard for Docker. It shows containers, images, volumes and networks, reads logs, opens a console and deploys stacks, which are Compose files managed from the dashboard.

To do that it gets the Docker socket (`/var/run/docker.sock`). Whoever controls the socket controls the server: they can start a privileged container and read any file on the host. The [Docker documentation](https://docs.docker.com/engine/security/#docker-daemon-attack-surface) says only trusted users should be allowed to control the daemon. In practice, the Portainer administrator password is worth as much as the root password. That is why this guide does not publish the dashboard ports, uses HTTPS from the first login and is careful about how the administrator is created.

## Versions used

| Component      | Version                   | Source                                                                           |
| -------------- | ------------------------- | -------------------------------------------------------------------------------- |
| Portainer CE   | 2.45.4 LTS (18 Nov 2026)  | [Portainer releases](https://github.com/portainer/portainer/releases/tag/2.45.4) |
| Caddy          | 2.11.9                    | [Caddy releases](https://github.com/caddyserver/caddy/releases/tag/v2.11.9)      |
| Docker Engine  | 29.10.1                   | official apt repository                                                          |
| Docker Compose | v5.8.1 (plugin)           | official apt repository                                                          |
| System         | Ubuntu 24.04.5 LTS, amd64 |                                                                                  |

The image tags are pinned in the files. Do not use `latest`: the dashboard that controls the server should not change version on its own.

## What you need

- A VPS running Ubuntu 24.04 and a user with `sudo`. The examples use a user named `deploy`.
- A subdomain with a DNS `A` record pointing to the VPS IP. The examples use `portainer.example.com`.
- Ports 80 and 443 free on the VPS, so Caddy can issue the certificate.

At rest, Portainer used about 90 MiB of RAM and Caddy 64 MiB. The dashboard fits on any plan; what sizes the VPS is the applications you will run on it.

## 1. Install Docker from the official repository

These are the commands from the [Docker documentation for Ubuntu](https://docs.docker.com/engine/install/ubuntu/), which now writes the repository in the `docker.sources` format:

```bash
sudo apt-get update
sudo apt-get 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
sudo tee /etc/apt/sources.list.d/docker.sources >/dev/null <<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-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```

Add your user to the `docker` group to run commands without `sudo`, then log out of the SSH session and back in:

```bash
sudo usermod -aG docker $USER
```

After logging back in, check:

```bash
docker version --format '{{.Server.Version}}'
docker compose version
docker run --rm hello-world
```

> **The `docker` group is also equivalent to root.** Only add the user who administers the VPS.

## 2. Open only what is needed in the firewall

```bash
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 443/udp
sudo ufw enable
sudo ufw status verbose
```

`ufw enable` warns that it may disrupt SSH connections; answer `y`, since port 22 is already allowed. 443/udp is for Caddy's HTTP/3.

## 3. Create the files

Create the folder and a `.env` with your domain:

```bash
mkdir -p ~/portainer && cd ~/portainer
```

```ini .env
PORTAINER_DOMAIN=portainer.example.com
```

The `Caddyfile` has a single block. Caddy requests the certificate, redirects HTTP to HTTPS and forwards everything, including the console WebSockets, to Portainer:

```caddy Caddyfile
{$PORTAINER_DOMAIN} {
	reverse_proxy portainer:9000
}
```

In `docker-compose.yml`, only Caddy has `ports:`. Portainer stays on the internal network and takes no flags:

```yaml docker-compose.yml
name: portainer

services:
  portainer:
    image: portainer/portainer-ce:2.45.4
    restart: unless-stopped
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer_data:/data
    networks:
      - proxy

  caddy:
    image: caddy:2.11.9
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    environment:
      PORTAINER_DOMAIN: ${PORTAINER_DOMAIN:?set PORTAINER_DOMAIN in .env}
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - proxy
    depends_on:
      - portainer

networks:
  proxy:

volumes:
  portainer_data:
  caddy_data:
  caddy_config:
```

Two details of this layout came out of the test:

- **The proxy targets port 9000, over HTTP, inside the Docker network.** With `reverse_proxy https://portainer:9443`, Caddy changes the `Host` header and Portainer rejects the login with `403 Forbidden`, because its CSRF protection compares `Origin` with `Host`. With 9000, Caddy keeps the `Host` and `--trusted-origins` is not needed.
- **Port 8000 is left out.** It serves the Edge agent tunnel, which this guide does not use.

## 4. Validate and start

Validate both files before starting:

```bash
cd ~/portainer
docker compose config -q && echo CONFIG_OK
docker run --rm --env-file .env -v "$PWD/Caddyfile:/etc/caddy/Caddyfile:ro" caddy:2.11.9 caddy validate --config /etc/caddy/Caddyfile
```

The second command ends with `Valid configuration`. Start the stack and check it:

```bash
docker compose up -d
docker compose ps
curl -fsS https://portainer.example.com/api/system/status
```

```text
{"Version":"2.45.4","InstanceID":"c6f29658-6f45-451f-a445-ef9cf40f2732"}
```

In `docker compose ps`, only Caddy shows `0.0.0.0:80->80/tcp` and `0.0.0.0:443->443/tcp`. Portainer is listed with `8000/tcp, 9000/tcp, 9443/tcp` and no arrow: those are container ports, not host ports.

## 5. Create the administrator with the setup token

Version 2.45 prints a token in the log on first start. Without it, nobody can create the administrator or restore a backup, even if they reach the screen before you do. Get the token:

```bash
docker compose logs portainer | grep setup_token
```

```text
setup_token=<64 hexadecimal characters>
```

Open `https://portainer.example.com`. On the **New Portainer installation** screen, fill in `Username` (prefilled with `admin`), `Password` and `Confirm password` with a password of at least 12 characters, paste the token into `Setup token` and click **Create user**.

> **You have 5 minutes.** If the administrator is not created within 5 minutes of startup, Portainer locks itself and shows "Your Portainer instance timed out for security purposes". In the test, the lock came exactly 5 min 00 s after start. The container stays `Up`, so `restart: unless-stopped` does not help. Run `docker compose restart portainer` and get the token again, because it changes on every restart.

Keep the password in a password manager and do not reuse it.

## 6. Connect the local environment and deploy the first stack

After **Create user** comes the **Set up Edge Compute** screen; click **Skip**. Next, the **Environment Wizard** shows "We have connected your local environment of Docker to Portainer". Click **Get Started**: the `local` environment is the Docker of the VPS itself.

To deploy a stack:

1. On **Home**, click the `local` environment.
2. In the side menu, open **Stacks** and click **Add stack**.
3. Name it `whoami` and keep the **Web editor** build method.
4. Paste the Compose below and click **Deploy the stack**.

```yaml
services:
  whoami:
    image: traefik/whoami:v1.11.0
    restart: unless-stopped
```

The stack takes a few seconds to go from deploying to active. Check from the terminal:

```bash
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}'
```

```text
NAMES                   IMAGE                           STATUS
whoami-whoami-1         traefik/whoami:v1.11.0          Up 8 seconds
portainer-caddy-1       caddy:2.11.9                    Up 6 minutes
portainer-portainer-1   portainer/portainer-ce:2.45.4   Up 6 minutes
```

The stack list also shows `portainer`, with **Limited** control. That is the Compose from this guide: Portainer can see it but not edit it, because it was created from the terminal. Keep changing that stack through the files in `~/portainer`.

To open a terminal inside a container, go to **Containers**, click the container and then **Console**. The **Command** field defaults to `/bin/bash`. On Alpine images, such as Caddy's, switch it to `/bin/sh` before clicking **Connect**, or the connection opens and drops right away.

> **A stack with `ports:` opens the port to the internet.** Docker publishes ports outside the ufw rules. Before adding `ports:` to a stack, read [The Docker port stays open with ufw](https://nuvits.net/en/blog/docker-port-still-open-with-ufw).

## 7. Check what is exposed

On the VPS, list what is listening:

```bash
sudo ss -tulpn | grep -E ':(22|80|443|8000|9000|9443)\b'
```

Only SSH on 22 and `docker-proxy` on 80 and 443 should appear. From your own computer, test the Portainer ports against the VPS IP:

```bash
curl -m 5 -k https://VPS-IP:9443
curl -m 5 http://VPS-IP:9000
```

Both commands must time out. In the test, 9443, 9000 and 8000 did not answer from outside, and port 80 returned `308` to HTTPS.

## Backup and restore

The Portainer backup holds the configuration: users, environments, the Compose of each stack and the keys. It does not hold containers, images or the volumes of your applications. The screen itself says so: "This will back up your Portainer server configuration and does not include containers".

In **Settings > General > Back up Portainer**, turn on **Password Protect**, enter a password and click **Download backup**. The file is named `portainer-backup_YYYY-MM-DD_HH-MM-SS.tar.gz.encrypted`, encrypted with AES-256-GCM and stamped in UTC. Keep the file and the password off the VPS.

To automate it, the API does the same. This block asks for the administrator password and writes the backup to the current folder. The password goes straight into the JSON: if it contains double quotes or a backslash, type those characters preceded by `\`:

```bash
read -rsp "Admin password: " PW; echo
JWT=$(curl -fsS https://portainer.example.com/api/auth -H 'Content-Type: application/json' \
  -d "{\"Username\":\"admin\",\"Password\":\"$PW\"}" | sed -E 's/.*"jwt":"([^"]+)".*/\1/')
curl -fsS -X POST https://portainer.example.com/api/backup -H "Authorization: Bearer $JWT" \
  -H 'Content-Type: application/json' -d '{"password":"BACKUP-PASSWORD"}' \
  -o "portainer-backup-$(date +%F).tar.gz.encrypted"
```

A restore only works on a new instance with no administrator. In the test, the volume was deleted, Portainer started clean and the backup came back like this:

```bash
TOKEN=$(docker compose logs portainer | grep -o 'setup_token=[0-9a-f]*' | tail -1 | cut -d= -f2)
curl -fsS -X POST https://portainer.example.com/api/restore -H "X-Setup-Token: $TOKEN" \
  -F "file=@portainer-backup-2026-11-30.tar.gz.encrypted" -F "password=BACKUP-PASSWORD"
```

The first screen offers the same thing under **Restore Portainer from backup**, which also asks for the setup token. After the restore, the user, the password, the `local` environment and the `whoami` stack with its Compose were back. The stack's container did not come back on its own. To start it again, open the stack, go to the **Editor** tab, click **Update the stack** and confirm with **Update**. With the wrong password, the API answers `Failed to decrypt the archive` and the instance is left untouched.

Application data needs its own backup. [Automatic VPS backups with restic](https://nuvits.net/en/blog/vps-backup-restic) covers volumes and databases.

## Upgrade Portainer

Read the [release notes](https://github.com/portainer/portainer/releases), take a backup and change the tag in `docker-compose.yml`. The example goes from 2.45.3 to 2.45.4; use the version you have and the one you are installing:

```bash
cd ~/portainer
sed -i 's|portainer/portainer-ce:2.45.3|portainer/portainer-ce:2.45.4|' docker-compose.yml
docker compose config -q
docker compose pull
docker compose up -d
curl -fsS https://portainer.example.com/api/system/status
```

In the test, from 2.45.3 to 2.45.4, only the Portainer container was recreated. The log showed `migrating database from version 2.45.3 to 2.45.4`, and the user, the environment and the stack were still there. The `whoami` stack did not even restart. Before migrating, Portainer keeps a copy of the database at `/data/backups/portainer.db.bak`, inside the volume.

## Common problems

- **"Your Portainer instance timed out for security purposes":** the 5 minutes passed with no administrator. Run `docker compose restart portainer` and use the new token.
- **`Invalid or missing setup token` (403):** the token you pasted is not the current one. It changes on every restart; get it from the log again.
- **`Password does not meet the requirements` (400):** the password has fewer than 12 characters.
- **`Forbidden` on login, with `CSRF check failed ... Origin does not match Host` in the log:** the proxy is changing the `Host` header or points to `https://portainer:9443`. Use `reverse_proxy portainer:9000` as in this guide. If you need a different setup, the `--trusted-origins https://portainer.example.com` flag allows the domain.
- **`unrecognized global option: reverse_proxy` in `caddy validate`:** the `PORTAINER_DOMAIN` variable did not reach Caddy. Check the `.env` and the `--env-file` in the command.
- **`required variable PORTAINER_DOMAIN is missing a value`:** the `.env` does not exist or is in another folder.
- **The console connects and drops:** the image has no `/bin/bash`. Choose `/bin/sh`.

## What was not tested

The test ran on a virtual machine with no public IP. Real DNS, the public Let's Encrypt certificate and HTTP/3 were left out; in place of the domain, Caddy used its internal certificate authority with the same `Caddyfile`. Restoring through the browser screen, restoring a backup taken on a different version and an upgrade larger than a patch were not tested either. The memory figures come from that virtual machine and are an order of magnitude.

## Next steps

- For a dashboard that also builds and deploys from Git, compare with [How to install Coolify on a VPS securely](https://nuvits.net/en/blog/install-coolify-ubuntu-vps) and [Coolify vs Dokploy](https://nuvits.net/en/blog/coolify-vs-dokploy-vps-comparison).
- To run n8n as a stack, start from the Compose in [How to install n8n with Docker on a VPS](https://nuvits.net/en/blog/install-n8n-docker-vps).
- To pick a server, see the [Docker VPS](https://nuvits.net/en/docker-vps) and [Linux VPS](https://nuvits.net/en/linux-vps) plans with Ubuntu 24.04.
