Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose named volume: find volume on host machine

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?

like image 960
Maksim Avatar asked Dec 18 '16 13:12

Maksim


People also ask

Where are docker volumes stored on host?

Volumes are stored in a part of the host filesystem which is managed by Docker ( /var/lib/docker/volumes/ on Linux).

How do I find docker volumes?

Volumes are also stored as part of the host file system, which is managed by Docker. On Linux, volumes are stored in “/var/lib/docker/volume”.

Where does Docker-compose create volume?

These volumes are created inside /var/lib/docker/volume local host directory. As we can see, we don't have to specify the host directory. We just need to specify the directory inside the container. If we remove the volume instruction from the docker-compose.

How do I access docked volume docker?

Find out the name of the volume with docker volume list. Shut down all running containers to which this volume is attached to. Run docker run -it --rm --mount source=[NAME OF VOLUME],target=/volume busybox. A shell will open.


1 Answers

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
like image 156
Maksim Avatar answered Dec 10 '22 06:12

Maksim