Volumes in docker-compose not persisting

I think I do not understand how Docker volumes work. I have a docker compose file with an app service running a laravel app, a postgres service, and a memcached service. I have named volumes for the laravel storage and for postgres, but nothing is persisted to those volumes. If I look in my file finder there are no files in the folder and when I shut down the db container the data is lost.

docker-compose.yaml:

services:
  app:
    build:
      context: .
    entrypoint: php artisan octane:frankenphp
    ports:
      - "80:8000"
      - "443:443"
      - "443:443/udp" # HTTP/3
      - "5173:5173"
    depends_on:
      - db
      - cache
    volumes:
      - storage:/path/to/laravel/base/storage
      - ../:/app
    networks:
      - storefront
      - backroom
  db:
    image: postgres:latest
    restart: unless-stopped
    volumes:
      - data:/path/where/I/want/db/data
    environment:
      POSTGRES_USER: user
      POSTGRES_DB: db
      POSTGRES_PASSWORD: "password"
    networks:
      - backroom

  cache:
    image: memcached:latest
    restart: unless-stopped
    networks:
      - backroom

networks:
  storefront:
  backroom:

volumes:
  data:
  storage:

This line appears to work:

  • …/:/app
    When I do things inside the app service container, I can see the changes in my local file system. For instance, if I use composer to add a package, I can look at composer.json in VS Code later and the new package is there. But the two named volumes do not appear to do anything.

The containers communicate - php artisan migrate and php artisan db:seed create tables and records in postgres - but the data does not persist.

Thank youPreformatted text

If by “shut down” you mean “stop”, that’s not possible. Stopped containers are still existing containers so data wouldn’t be deleted even if you save it to the container’s filesystem.

If you mean you run docker compose down which deletes the containers, then your mount point in the container could be wrong.

If you run docker compose down -v, that deletes named volumes, so you will indeed lose data.

1 Like

Sorry for my lack of precision:

docker-compose down

Could you expand on “mount point in the container”?

Where you mount the host folder in the container. Like this

So that is the path in the container? How would I map that folder to a folder in my actual file system for persistence? I just don’t understand how the volumes work.

Volumes are automatically populated with the data in the container. Not for mounting data from the host or share with the host, although there is a way. Search for “Custom volume path” in my following blogpost:

And also read the documentation

1 Like

I think I am understanding more now, but it seems to me that “source” and “destination” labels are used backwards, at least to the way my mind works. A volume binds the “destination” folder in the CONTAINER with the “source” in the VOLUME, and takes the files from the “destination” to populate the “source”. I would label them the other way around, but I’m sure they have a very good reason that I am just unaware of.

So, I guess what I need to do is use the postgres data directory as the “destination” for my volume. That is what I am going to try.

Thanks man! It’s working now and I understand how it works now. You led me there. When you highlighted the mount point from my file, that was the key that unlocked the whole thing for me.

Hi, how did you solve it? what was the final volume mapping?