Docker compose named volume: find volume on host machine

試著忘記壹切 提交于 2021-02-04 15:18:05

问题


I have a docker-compose.yml:

version: '2'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:

When I run this docker-compose up -d and then do docker inspect -f '{{ (index .Mounts 0).Source }}' ef8be254a08b for the running db container to get the Source which is specifying the volume location on the host, I always get "No such file or directory" if I ls the directory (ls /var/lib/docker/volumes/test_db_data/_data: No such file or directory). How is that possible to get the real location for the volume?


回答1:


So I got the answer. The main point is that Docker on Mac is still running inside a VM. So the system paths are still relative to the VM, and not to your Mac. All the containers are stored within VM and located at ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2

To go inside the VM use:

screen  ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

Then you can see contents of the Source folder given in docker inspect {container_id}:

ls /var/lib/docker/volumes/test_db_data/_data


来源:https://stackoverflow.com/questions/41208870/docker-compose-named-volume-find-volume-on-host-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!