8

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?

kevP
  • 81
  • 1
  • 1
  • 3

2 Answers2

11

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

amarillion
  • 1,409
  • 2
  • 16
  • 25
3

On mac

docker volume ls  
docker volume inspect <volume identifier>

Will give you the mount point within the Docker virtual filesystem, not on your mac filesystem.

Docker is not natively compatible with macOS, so Hyperkit is used to run a virtual image. Its virtual image data is located in:

~/Library/Containers/com.docker.docker/Data/vms/0

You can investigate your Docker root directory by creating a shell in the virtual environment:

screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
cd /var/lib/docker/volumes/<volume identifier>/_data
ls

You can kill this session by pressing Ctrl+a, followed by pressing k and y.

Credit to: https://www.freecodecamp.org/news/where-are-docker-images-stored-docker-container-paths-explained/