大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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.1…

问题原文

原帖链接 · kevP · CC BY-SA 3.0

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?

社区高分解答

原帖链接 · amarillion · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-02-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。