Why some volumes are already created inside docker engine? - docker

Whenever I run the below command:
docker volume ls
I can see some volumes already created in my docker engine.
DRIVER VOLUME NAME
local 5df9458932cd504e10b2b37856c434cbdf3876733684b100cbf390c965ac9581
local 6f7037bc33861a5e42a9f8bcd699f8184ff1916a297a718ccc4df5f369d07530
local 8a86c462020f35f1051b47c48555228a1df359251f2496c32ed45a9081bb1872
local 85ed838d2e081eddc672fd8ddb15bbb3eecc73adb270678c98b7c50a03ecb2fc
Why are those volume created ?
How can I find for what purpose they exists ?

If you started a Docker container with a volume that doesn't have a name or host mount point, Docker will create a unique name for them. These docs briefly mention anonymous volumes like this. Most likely, a Dockerfile had a VOLUME section and wasn't run with a corresponding --mount or -v flag to bind some local volume to the container's volume.
Also see this devops stack exchange answer.
Here's an example of when an anonymous volume is created:
Dockerfile with anonymous volumes:
FROM alpine:3.9
VOLUME ["/root", "/test"]
Building/running container without mounting or otherwise naming the /root, /test volumes:
$ docker volume ls
DRIVER VOLUME NAME
$ docker build -t test .
$ docker run -it --rm -d --name volume-test test:latest sh
$ docker volume ls
DRIVER VOLUME NAME
local 5b332abd25b77c1ac324a0e3c00dc9a554cfe80c996a20bd77ef10c35c8ef98a
local 05c903f47f3f3666e03ee06154ff54b23547a5cc65750ca18bb40be40ed4049c
local 6f595aada6ae7c9fb16831996c2bdd8d652bec55a7cedf96afef95aec8f4e6e1
local 7f54c9dbbec46acc5a843499c65a50e23a78baa884facd026704d0dcb0362c9e
local 47a791197d6164757b015df1e2aba48bac3999720ead6b5981820a3aaece4113
local 214155fe63200cc859c1eddd2b31aa990fd6eb7c8614aa02bd8b57690b0fe53e
Of course, you can always inspect the volumes to try to find out where they came from but this may or may not be useful for you:
docker inspect 5b332abd25b77c1ac324a0e3c00dc9a554cfe80c996a20bd77ef10c35c8ef98a

Related

Strange issue about applying docker named volume for new container

As I understand from all the information I found, the docker host volume can be created from three ways:
1- by ignoring the host-path (it will automatically create a directory with random ID)
2- by specify the host-path (it will also automatically create a directory with random ID)
3- by named volume and specify to a host-path
So I was trying first 2 ways:
$ docker run --name mongo-docker -v /data/db -p 27017:27017 -d mongo
$ docker run --name mongo-docker2 -v $(pwd)/data/:/data/db -p 27222:27017 -d mongo
And I look at the docker volume list:
$ docker volume ls
DRIVER VOLUME NAME
local 5b829a731245cb7fe3a1f28aca4c4c3c3791105be228182ccb9b2f72319180c8
local fb058e804412fb56b2096e2cb903e3ae73647ef6ca076ad9003708b80f94ffc5
It looks just what I was expected.
But when I tried the last one, by created first a volume:
$ docker volume create mongoVol
mongoVol
$ docker volume ls
DRIVER VOLUME NAME
local mongoVol
and use it for host-volume path, came up like this:
$ docker run --name mongo-docker3 -v mongoVol:/data/db -p 27322:27017 -d mongo
86bea0e52c9f395268665e191edc59f795d07266f17667502c7fa32879a6e021
$ docker volume ls
DRIVER VOLUME NAME
local 0de25c92be504d0a6b9bb9c83aa8a6fe17bf9bc195562314ca49edb1c4cf4377 <=== create a new one for new container?
local mongoVol
Why is this create a new directory for it? Shouldn't it be just the "mongoVol" volume?
I can’t find answers to related questions on any forum, post nor any videos....
The mongo image's Dockerfile has two directories named in a VOLUME statement. You're mounting content on /data/db but not on /data/configdb.
If the Dockerfile declares a directory as a VOLUME and nothing is explicitly mounted there, Docker automatically creates an anonymous volume (your first case). That's what results in the additional volume appearing in the docker volume ls listing.

Docker different volume path for specific container

By default docker uses /var/lib/docker/volumes/ for any started container.
Is there any way to launch a new container and have it consume all the required disk on a different specified path on the host?
Basically have the root volume different.
For a specific container only, the simplest way i think would be to use docker volumes, Create docker volume and then attach the volume to the container. So the process running on the container uses up the share, so this is using the disk you would like to use.
More information on the following webpage,
https://docs.docker.com/storage/volumes/
you can define the volume path.
docker run -it --rm -v PWD$:/MyVolume ubuntu bash
This command will use the current folder where you execute the command from.
In the container you'll find your file under /MyVolume.
jens#DESKTOP:~$ docker run -it --rm -v $PWD:/MyVolume ubuntu bash
root#71969d68099e:/# cd /MyVolume/
root#71969d68099e:/MyVolume# ls
But you can define any path:
docker run -it --rm -v /home/someuser/somevolumepath:/MyVolume ubuntu bash
Almost the same is available in docker compose.
ports:
- "80:8080"
- "443:443"
volumes:
- $HOME/userhome/https_cert:/etc/nginx/certs
Jens

Docker inside docker : volume is mounted, but empty

I am running a docker container with docker mounted inside using :
docker run -v /Path/to/service:/src/service -v /var/run/docker.sock:/var/run/docker.sock --net=host image-name python run.py
This runs a python script that creates a data folder in /src and fills it. When printing os.listdir('/src/data'), I get a list of files.
I then run a container from within this container, mounting the data folder, using docker-py.
volumes = {'/src/data': {'bind': '/src', 'mode': 'rw'}}
client.containers.run(image, command='ls data', name=container_key, network='host', volumes=volumes)
And it prints :
Starting with UID: 0 and HOME: /src\n0\n'
Which means data is mounted, but empty. What am I doing wrong ?
So- mounting docker inside the container means that containers started from in there are running on your HOST machine.
The end result is you have two containers on host- one with
/Path/to/service:/src/service
and one with
/src/data:/src
If you want to share a volume between two containers you should usually use a "named" volume like
docker run -v sharedvolume:/src/data and docker run -v sharedvolume:/src

Docker Created Volume Does Not Exist With Inspect

I am new to volumes in Docker.
Following Creating and mounting a data volume container, I created a volume called mochawesome with:
docker create -v /mochawesome-reports --name mochawesome dman777/vista-e2e-test-runner
I see it existing:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
273b14f7e0ea dman777/vista-e2e-test-runner "./run_test.sh" 3 minutes ago Created mochawesome
However if I do docker volume inspect mochawesome I get:
Error: No such volume: mochawesome
Why is this?
The argument --name in docker create specifies the name of the container (not the name of the volume). Therefore docker volume inspect cannot find this name.
To create a named volume use docker create -v my-named-volume:/mochawesome-reports --name mochawesome dman777/vista-e2e-test-runner. Then you can use docker volume inspect my-named-volume.

Docker how to get volumes used by a container

I'm using Docker version 1.10. How can I get the volumes used by a container?
I know I can get the containers by:
docker ps
And I can inspect them with:
docker inspect $containerID
I also know that the volume API is available, so I can also do:
docker volume ls
and
docker volume inspect $volumeID
But I can't find any link information between them. What should I use?
You can get the detail volume information of a container by
docker inspect --format="{{.Mounts}}" $containerID
If I create a volume named "volumehello", and start a container named "hello" which use "volumehello":
docker volume create --name volumehello
docker run -it -d --name=hello -v volumehello:/tmp/data hello-world
Then we can get the volume information of "hello" container by running:
docker inspect --format="{{.Mounts}}" hello
We will get:
[{volumehello /var/lib/docker/volumes/volumehello/_data /tmp/data local z true rprivate}]
volumehello is the volume name
/var/lib/docker/volumes/volumehello/_data is the host location of the volume
/tmp/data is the mapped location of the volume within the container

Resources