Using a docker volume with postgresql to verify it is saving on the hosts filesystem

Using a docker volume with postgresql to verify it is saving on the hosts filesystem



I am trying to get docker volumes working.



I have defined a volume in my Dockerfile as follows:


version: "3"

services:
redis:
image: redis:alpine
ports:
- "6379:6379"

db:
image: postgres:9.4
#container_name: db
volumes:
- "db-data:/var/lib/postgresql/data"


volumes:
db-data:



Now my question is, when I do a docker-compose up I don't see my data persisted on my local laptop (or server).



I just want to test/verify that my data is saving to the host's filesystem, so if I start/stop docker when it restarts it reads from the database file on the host.




1 Answer
1



That construct saves data in a named volume; if you look under /var/lib/docker/volumes as root you should be able to see it there (though mucking around in /var/lib/docker generally isn't advisable; and I believe this is one of the things where Docker Compose will change the name to try to make it unique).


/var/lib/docker/volumes


/var/lib/docker



If you want the data to be saved in a host directory, change the volume declaration to explicitly have a relative or absolute path. You won't need the explicit volume declaration, and for this you can remove the volumes block entirely. That would leave you with a docker-compose.yml that looks like:


docker-compose.yml


version: "3"
services:
db:
image: postgres:9.4
volumes:
- "./db-data:/var/lib/postgresql/data"






could I define a volume base path and then use that for all my services?

– Blankman
Sep 8 '18 at 16:02






Relative paths will be relative to the location of the docker-compose.yml file; or I believe this is a case where variable substitution works.

– David Maze
Sep 8 '18 at 16:54


docker-compose.yml



Thanks for contributing an answer to Stack Overflow!



But avoid …



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Malaysia to Papua New Guinea by motorcycle?

.name

List of Bhojpuri films