Docker – Where is docker volume stored in the host computer for docker-compose

dockerdocker-composepostgresql

Consider the following docker-compose file

version: "2"
services:
  postgres:
    image: postgres:9.6
    volumes:
      - ./vol_folder:/var/lib/postgresql
    ports:
      - "5432:5432"

Here is my command history on my mac

docker-compose up

psql -h 192.168.99.100 -p 5432 -U postgres

create table test (my_id bigserial primary key);
INSERT INTO test (my_id) values (1);
SELECT * FROM test;

\q

Originally I thought the above commands will cause a .sql file to be created in ./vol_folder of the host computer. But I don't see any .sql file in ./vol_folder rather just an empty data directory in ./vol_folder

Furthermore if I docker-compose down and docker-compose up again I can see my data in the database is now gone.

I suspected that when I created the data when the image is running, the data is not stored back to ./vol_folder thus when I reboot, there is nothing to mount from the host.

So I guess my question is where is the volume stored in my host computer? Is docker volume only one way (eg. host data to container) not two way?

Best Answer

The volumes will normally be stored somewhere in /var/lib/docker/volumes/

To find out more, try the following commands

docker volume ls
docker volume inspect <volume identifier>

See also: https://www.linux.com/learn/docker-volumes-and-networks-compose

Related Topic