Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #33: Should You Use the Volume or Mount Flag?


For passing in a host's folder into a container both flags do basically the same thing. So which one should you use?

If you’ve played with Docker a bit, you’ve probably seen the -v (--volume) flag. It comes in very handy for passing in the current directory into a running container.

For example, if you were developing a web application you would want to mount in your source code so that you can see your changes without having to rebuild the Docker image.

Setting up a volume the old way with docker run:

docker run ... -v "$(pwd)":/myapp

The left side of the : is the current directory on the host and the right side of the : is where it gets mounted into the container.

Setting up the same volume using the mount flag with docker run:

docker run ... --mount type=bind,source="$(pwd)",target=/myapp

This does the same thing but it’s more explicit. Without knowing anything else about the command we can see it’s a bind mount that has both a source and a target.

This flag was introduced for non-Swarm based containers in v17.06.

How are they different?

The only difference is, using -v will create the bound folder on the Docker host if it doesn’t already exist where as using --mount will throw an error.

I actually like the behavior of --mount here because chances are when used in the above scenario, you would always expect the folder to exist on the Docker host.

Which one should you use?

Docker recommends using the --mount flag because it’s more explicit. I would tend to agree but in real world use cases, you’re probably using Docker Compose.

How can you use the new style with Docker Compose?

Compose doesn’t (yet?) have a mounts property but you can do it with the volumes property:

  - type: "bind"
    source: "."
    target: "/myapp"

With the old way you would have just set volumes: [".:/myapp"].

I’m going to start using the explicit style from this day forward in both my own projects as well as my Docker training courses.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.