Docker Community Forums

Share and learn in the Docker community.

Many ways to do the same thing

Why are there many ways to do the same thing between docker build and run?

docker build --volume /tmp -t tag . is the same exact thing as adding VOLUME in the Dockerfile. But regardless, these methods aren’t actually doing anything but creating a directory because you still need to bind the volume when you do docker run --volume /tmp:/tmp tag.

Also, whenever you run a docker run command with a volume, a new volume is cached (seen with docker volume ls) which eats up a ton of storage over time without cleanup. An extremely common case with container development is reusing a set of volumes. Is there no way to predefine a set of host-to-container mapping and reference it rather than redefine each volume, kind of like docker network create.

When you use VOLUME in your Dockerfile (or build --volume), you are declaring that containers created from the resulting image must have a volume mounted on the specified path. Hence, create and run actually create anonymous volumes if an existing one does not already exist.

You can use volume create to create a named volume ahead of time, and mount it when you create containers. Just like network create.