To keep Docker data across restarts and rebuilds, store it in named volumes or bind mounts, never only inside the container; then back those volumes up off the server. This guide sets that up on an Ubuntu VPS with Docker Compose and a Traefik reverse proxy.

A container's own filesystem is thrown away when the container is removed or recreated. Anything you need to keep, such as database files or uploads, must live in a volume.

Bind mounts vs. volumes

  • Bind mounts map a host folder (for example /opt/app/data) into the container. Handy for config files you edit by hand.
  • Named volumes are managed by Docker under /var/lib/docker/volumes. They're the usual choice for database data such as PostgreSQL or MySQL.

Step 1: Install Docker

Install Docker Engine and the Compose plugin with Docker's official convenience script:

curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh 

You can read get-docker.sh before running it. Docker also documents a manual apt repository install, which gives you more control on production servers.

Step 2: Write a Compose file with persistent storage

This compose.yml runs Traefik as a reverse proxy in front of a web app whose data lives in a named volume:

services: traefik: image: traefik:v3 command: - "--providers.docker=true" - "--providers.docker.exposedbydefault=false" - "--entrypoints.web.address=:80" ports: - "80:80" volumes: - /var/run/docker.sock:/var/run/docker.sock:ro # Lets Traefik discover containers  restart: unless-stopped webapp: image: my-web-app:latest labels: - "traefik.enable=true" - "traefik.http.routers.webapp.rule=Host(`example.com`)" - "traefik.http.routers.webapp.entrypoints=web" volumes: - webapp_data:/app/persistent_data # Named volume: survives container rebuilds  restart: unless-stopped volumes: webapp_data: 

Start it with sudo docker compose up -d. Add HTTPS with Traefik's Let's Encrypt support before going live; see the Traefik documentation.

Warning: Don't publish Traefik's dashboard (--api.insecure=true on port 8080) on a public server: it has no login. Also note that ports published by Docker bypass ufw rules, so only publish ports you mean to expose, or bind them to 127.0.0.1.

Step 3: Back up your volumes

Volumes survive container restarts, not disk failures or an accidental docker volume rm. On an unmanaged VPS the provider runs the hardware, network and power, and you own the OS, containers and application data, so backups are your job.

Back up a volume to a compressed archive in the current folder with a temporary container:

sudo docker run --rm -v webapp_data:/volume:ro -v "$(pwd)":/backup ubuntu \ tar czf /backup/webapp_data-$(date +%F).tar.gz -C /volume . 

Schedule it nightly with cron and copy the archives off the server, for example to S3-compatible storage.

Note: For databases, back up with the database's own tool (pg_dump, mysqldump) or stop the container first. Copying a live database's files can produce a backup that won't restore.

Many VPS providers also offer server-level snapshots or backups. Use them as a second layer, not a replacement for volume backups you can restore on any host.

I work on FlashRDP, a Windows RDP and Linux VPS host. This guide was first published in our docs: Persistent Docker Deployments on Ubuntu VPS. Everything above works on any Ubuntu server.