Install Portainer on a VPS the right way

Portainer CE is the most popular web UI for Docker. It's also the most popular way to put a backdoor on your server — by deploying it with the default password and on a public port. This guide does the opposite.

Disclosure: contains an affiliate link to Inferno VPS.

Why "the right way" matters

The Portainer container talks to /var/run/docker.sock. Whoever logs into Portainer can spawn privileged containers, mount the host filesystem, and effectively own the box. The first-user setup happens in a 5-minute window after install — if someone scans port 9000 in that window and creates the admin account before you do, your server is gone. Two specific failure modes follow.

The right way is: deploy bound to localhost, put Nginx + HTTPS in front, and never have the admin account creation window open to the public internet.

Prerequisites

Tested on Ubuntu 22.04, Portainer CE 2.21, on a 2 GB Inferno VPS.

Step 1 — The Compose file

mkdir -p ~/portainer && cd ~/portainer
nano docker-compose.yml
name: portainer

services:
  portainer:
    image: portainer/portainer-ce:2.21.5-alpine
    restart: unless-stopped
    ports:
      - "127.0.0.1:9000:9000"
    volumes:
      - portainer_data:/data
      - /var/run/docker.sock:/var/run/docker.sock:ro
    deploy:
      resources:
        limits:
          memory: 256M
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

volumes:
  portainer_data:

Two important details:

  1. 127.0.0.1:9000:9000 — Portainer is only reachable from localhost. Nginx will proxy with TLS. No public port 9000.
  2. /var/run/docker.sock:ro — read-only socket. Portainer CE does not need write access for most read-and-deploy operations. Some advanced features (network ops) need rw; flip to rw only if you actively need them.

Start it:

docker compose up -d
docker compose logs -f portainer

Step 2 — Nginx with admin-only access during setup

Create the Nginx server block. Critical: gate the entire site behind your home IP during the first-user window:

sudo nano /etc/nginx/sites-available/portainer
server {
    listen 80;
    server_name portainer.example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/html;
    }

    location / {
        allow YOUR.HOME.IP.HERE;   # replace with your client IP
        deny all;

        proxy_pass http://127.0.0.1:9000;
        proxy_http_version 1.1;
        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;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Enable and issue the cert:

sudo ln -s /etc/nginx/sites-available/portainer /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d portainer.example.com --redirect

Step 3 — Create the admin account, then lower the gate

Visit https://portainer.example.com/ from your home IP. You'll see the "Create the first admin user" screen. Use a strong, randomly-generated password (a password manager). Pick a non-obvious username — not admin, not root.

Inside Portainer:

  1. Settings → Authentication: enforce a strong password policy.
  2. Settings → Users → invite additional admins only as needed.
  3. If you want OAuth (Google, GitHub) instead of local users, configure it now and disable local accounts after.

Once admin is set up, decide whether to remove the IP allowlist:

To remove the allowlist: delete the allow / deny lines from the Nginx config and reload.

What Portainer is good for

What Portainer is bad for

Backing up Portainer

Portainer stores its data in the portainer_data volume. Back it up the same way you back up any volume:

docker run --rm \
  -v portainer_portainer_data:/data:ro \
  -v "$(pwd)":/backup \
  alpine tar czf /backup/portainer-$(date +%Y%m%d).tar.gz -C /data .

The same pattern as our n8n backup guide.

Upgrading Portainer

cd ~/portainer
# edit docker-compose.yml — bump image tag, e.g. 2.21.5 -> 2.22.0
docker compose pull
docker compose up -d

Portainer's data layer is forward-compatible within a major version — minor version bumps are usually a non-event. Read release notes for majors.

Common Portainer mistakes

"Portainer is on the internet on port 9000"

You used ports: ["9000:9000"] instead of "127.0.0.1:9000:9000". Stop the container, fix the file, recreate. Audit your access logs — if the setup window was open, assume compromise and rebuild.

The admin account password is "portainer"

It's been days, not minutes. Rotate now, audit your container list for unexpected entries.

Two-factor was never enabled

Open Settings → Authentication. Add MFA. This is one minute of setup that significantly raises the cost of credential compromise.

Socket mounted read-write but you only do reads

Switch to :ro. Tiny change, shrinks blast radius.

VPS for a hardened Portainer setup?

Portainer + Nginx + a few containers fits comfortably on a 2 GB Inferno plan. With DDoS included you're not exposed at the network layer either.

View Inferno plans →

What to do next


Author: Ivo Pärnaste — DevOps, Tallinn. Last tested: 2026-05-22 on Portainer CE 2.21.5-alpine, Docker CE 27.3.