user445670
user445670

Reputation: 179

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

Upvotes: 3

Views: 1240

Answers (2)

Jose V
Jose V

Reputation: 1854

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

Upvotes: 1

Daniel Sont
Daniel Sont

Reputation: 34

I believe the volume path on the container is to /var/lib/postgresql/data not /var/lib/postgresql

Upvotes: 0

Related Questions