2

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?

Cole
  • 676
  • 8
  • 24

2 Answers2

6

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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • This was it. I was using a `VOLUME` within my `Dockerfile` that didn't match the `--volume` I was using for `docker --run`. Deleting the `VOLUME` in the `Dockerfile` and just using the `--volume` flag with `docker run` worked. No more unnamed volumes being created after subsequent `docker run`'s. Thanks! – Cole Jun 30 '20 at 14:01
2

How to work with volumes:

To create a volume:

docker volume create my-vol

To use this volume:

docker run -d  --name devtest -v my-vol:/app   nginx:latest
newuser
  • 53
  • 7
  • How is this different than what the OP did? – BMitch Jul 01 '20 at 11:18
  • OP have edit his Question now. Before he has created his volume by docker volume create --name myvol and this creates an container with the Name myvol and not a volume with this Name. You can see it in Question history – Matthias Reissner Jul 01 '20 at 12:30
  • The `--name` option works, at least on my current version of docker, it's just not documented in the `--help` output. The `docker volume create` command never creates a container, it creates a volume with that name. – BMitch Jul 01 '20 at 12:36
  • Sorry, for me always was --name an eye marker to specify the name of the container - for some other people too ;-) We don't had read the full command carefully – Matthias Reissner Jul 01 '20 at 13:12