Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker volume ls lists empty

I'm using docker version 18.09.2, build 6247962.

I run a container and mounted a host directory to the container.

docker run -it -p 4444:8000 -v c:/py:/azima 27b4b21eeb64 /bin/sh

This created a container and the host directory c:/py has been mounted to /azima.

I can check, create, read files and it is working.

But from another powershell when I run this command docker volume ls.

This shows empty.

Inspecting the container gives this info (if this helps):

"Mounts": [
        {
            "Type": "bind",
            "Source": "/host_mnt/c/py",
            "Destination": "/azima",
            "Mode": "",
            "RW": true,
            "Propagation": "rprivate"
        }
    ],

Why is the volume not listed?

like image 733
Azima Avatar asked Jan 27 '23 01:01

Azima


2 Answers

This is because -v c:/py:/azima option will mount c:/py onto /azima directory inside container using bind-mounts.

Bind mounts are basically just binding a certain directory or file from the host inside the container, which you did.

That's why when you inspect the container you see "Type": "bind"

Whereas docker volume will create Named volumes which you create manually with docker volume create VOLUME_NAME. They are created in /var/lib/docker/volumes and can be referenced to by only their name.

These named volumes where only get listed in docker volume ls command.

And when you inspect container attached to such volume you will see "Type": "volume"

More info here.

Hope this helps.

like image 78
mchawre Avatar answered Jan 30 '23 08:01

mchawre


Docker volume and Docker mount are two different things.

In bind mounts, we specify Host Path and it does not show in docker volumes ls and volume we specify docker volume name and we can see docker volume using docker volumes ls

Volumes: Volumes are the preferred way to store persistent data Docker containers create or use. The host filesystem also stores volumes, similar to bind mounts. However, Docker completely manages them and stores them under ~/docker/volumes by default.

 docker volume create alpine_test

Now run the container

docker run --rm  -v alpine_test:/root alpine ash -c "touch /root/test.txt"
 

This will just create a file using volume alpine_test and the container will terminate.

Now if we run another container and list file

docker run  -v alpine_test:/root alpine ash -c "ls /root/"

Still, we are able to see our last created file.

If the container is terminated the data still persists in the volume and available for later use.

If you inspect the volume attached container, It will show the docker volume path

docker inspect container_id

 "Mounts": [
                {
                    "Type": "volume",
                    "Name": "alpine_test",
                    "Source": "/var/snap/docker/common/var-lib-docker/volumes/alpine_test/_data",
                    "Destination": "/root",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                }
            ],

enter image description here

Bind mounts: A bind mount is a file or folder stored anywhere on the container host filesystem, mounted into a running container. The main difference a bind mount has from a volume is that since it can exist anywhere on the host filesystem, processes outside of Docker can also modify it.

docker run -v /home/test/:/test -it --rm alpine

For mount, if you inspect the container you will the host mount path location.

docker inspect container_id

 "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/test/",
                "Destination": "/root",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
like image 30
Adiii Avatar answered Jan 30 '23 09:01

Adiii