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.
…/:/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.
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.
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:
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.