I almost lost the data of my Portainer 😨

What happend

After updating Portainer, I noticed that my account and related data were lost.

Why it happend

I updated Portainer to the latest version following the official documentation.

I’ll show you how I set up Portainer in VPS.

I ran the following command:

docker run -d -p 9000:9000 --restart always -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer-ce:latest

To update Portainer, I stopped the container and then removed it. After that, I ran the same command I used previously to create the container, which started the updated version.

docker run -d -p 9000:9000 --restart always -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer-ce:2.27.0

But Immidiately after I opened the Portainer page, I noticed my account and data were lost.

Why?

My understanding was that even if I removed the image, I should have still been able to log in as the same user since I hadn’t removed the volume, where Portainer’s settings were stored.

The reason why I lost my data

This was because I didn’t specify the volume for the Portainer’s container.

Since I didn’t specify the volume, the data was written /data directory in the container. So the data had been removed when as I removed old container.

The data you create inside a container persist as long as you don’t delete the container.

Docker: How a container persists data without volumes in the container?

So I should have specified the volume like below:

# docker-compose.yaml 
services:
  portainer:
    image: portainer/portainer-ce:lts
    container_name: portainer
    ports:
      - 9000:9000
    volumes:
      - portainer_data:/data
      - /var/run/docker.sock:/var/run/docker.sock
    restart: always
volumes:
  portainer_data:

By the way, I lost my data but I could restore it from a backup file.

Conclution

  1. Specify a volume for the Docker container if persistent data is needed.
  2. Docker Compose can simplify container setup and management.
  3. Backup your data !!