Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker volume data does not get saved to host folder in mac

Here is my docker compose file

version "2"
services:
    my_postgres:
        image: postgres:9.6
        volumes:
          - /Users/my_user_name/test_docker/my_volume_space:/var/lib/postgresql
        ports:
          - "5432:5432"

I entered the following command in mac

docker-machine start
docker-machine env
evcal "$(docker-machine env default)"

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 ./my_volume_space of the host computer. But I don't see any .sql file in ./my_volume_space rather just an empty data directory in ./my_volume_space

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 ./my_volume_space thus when I reboot, there is nothing to mount from the host.

Can someone tell me what I am doing wrong?

Thanks

like image 964
user445670 Avatar asked Mar 18 '26 11:03

user445670


1 Answers

Path volumes do not work on docker-machine (macOS) with postgres image, source.

The work-around is to use named volumes. Example docker-compose.yaml:

services:
  test-postgres-compose :
    ...
    volumes :
      - pgdata:/var/lib/postgresql/data
...
volumes :
  pgdata :

Docker compose volumes info

like image 82
Jose V Avatar answered Mar 20 '26 09:03

Jose V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!