Search code examples
dockerdocker-volume

Docker run --volume keeps creating random volumes and not using the one specified


Docker keeps creating random volumes instead of using the I specify when running docker run....

I'll start out with no volumes.

$ docker volume ls
DRIVER              VOLUME NAME

I'll create one

docker volume create myvol

It'll get created

$ docker volume ls
DRIVER              VOLUME NAME
local               myvol

I'll start a container using the volume

$ docker run -d \
  --name myapp \
  --publish 1337:1337 \
  --volume myvol:/my-work-dir/.tmp \
  foo/bar:tag

I'll go and check my volumes again and I have the one I created and a new one.

$ docker volume ls
DRIVER              VOLUME NAME
local               9f7ffe30c24821c8c2cf71b4228a1ec7bc3ad6320c05451e42661a4e3c2c0fb7
local               myvol

Why isn't myvol being use? Why is a new volume being created?


Solution

  • This happens when the image you are using defines a VOLUME in the Dockerfile to a container path that you do not define as a volume in your run command. Docker creates the guid for the volume name when you have a volume without a source, aka an anonymous volume. You can use docker image inspect on the image to see the volumes defined in that image. If you inspect the container (docker container inspect), you'll see that your volume is being used, it's just that there's a second anonymous volume to a different path also being used.