docker compose volume type - bind vs volume

When bind mounts are files coming from your host machine, volumes are something more like the nas of docker.

  • Bind mounts are files mounted from your host machine (the one that runs your docker daemon) onto your container.
  • Volumes are like storage spaces totally managed by docker.
    You will find, in the literature, two types of volumes:
    • named volumes (you provide the name of it)
    • anonymous volumes (usual UUID names from docker, like you can find them on container or untagged images)

Those volumes come with their own set of docker commands; you can also consult this list via

docker volume --help

You can see your existing volumes via

docker volume ls

You can create a named volume via

docker volume create my_named_volume

But you can also create a volume via a docker-compose file

version: "3.3"

services:
  mysql:
    image: mysql
    volumes:
      - type: volume
          source: db-data
          target: /var/lib/mysql/data

volumes:
  db-data:

Where this is the part saying please docker, mount me the volume named db-data on top of the container directory /var/lib/mysql/data

- type: volume
    source: db-data
    target: /var/lib/mysql/data

And this is the part saying to docker please create me a volume named db-data

volumes:
  db-data:

Docker documentation about the three mount types:

  • https://docs.docker.com/storage/bind-mounts/
  • https://docs.docker.com/storage/volumes/
  • https://docs.docker.com/storage/tmpfs/

If I understood you correctly, you're asking in other words: What is the difference between Volumes and bind mounts?

Differences in management and isolation on the host

Bind mounts exist on the host file system and being managed by the host maintainer.
Applications / processes outside of Docker can also modify it.

Volumes can also be implemented on the host, but Docker will manage them for us and they can not be accessed outside of Docker.

Volumes are a much wider solution

Although both solutions help us to separate the data lifecycle from containers, by using Volumes you gain much more power and flexibility over your system.

With Volumes we can design our data effectively and decouple it from the host and other parts of the system by storing it dedicated remote locations (Cloud for example) and integrate it with external services like backups, monitoring, encryption and hardware management.

More Volumes advantages over bind mounts:

  1. No host concerns.
  2. Can be managed using Docker CLI.
  3. Volumes can save you some uid/gid issues related permissions which occur in cases like when a container user's uid does not match the host gid.
  4. A new volume’s contents can be pre-populated by a container.

Examples

Lets take 2 scenarios.

Case 1: Web server.
We want to provide our web server a configuration file that might change frequently.
For example: exposing ports according to the current environment.
We can rebuild the image each time with the relevant setup or create 2 different images for each environment. Both of this solutions aren’t very efficient.

With Bind mounts Docker mounts the given source directory into a location inside the container.
(The original directory / file in the read-only layer inside the union file system will simply be overridden).

For example - binding a dynamic port to nginx:

version: "3.7"
services:
  web:
    image: nginx:alpine
    volumes:
     - type: bind #<-----Notice the type
       source: ./mysite.template
       target: /etc/nginx/conf.d/mysite.template
    ports:
     - "9090:8080"
    environment:
     - PORT=8080
    command: /bin/sh -c "envsubst < /etc/nginx/conf.d/mysite.template > 
        /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"

(*) Notice that this example could also be solved using Volumes.

Case 2 : Databases.
Docker containers do not store persistent data: any data that will be written to the writable layer in container’s union file system will be lost once the container stop running.

But what if we have a database running on a container, and the container stops - that means that all the data will be lost?

Volumes to the rescue.
Those are named file system trees which are managed for us by Docker.

For example - persisting Postgres SQL data:

services:    
  db:
    image: postgres:latest
    volumes:
      - "dbdata:/var/lib/postgresql/data"
    volumes:
     - type: volume #<-----Notice the type
       source: dbdata
       target: /var/lib/postgresql/data
volumes:
  dbdata:

Notice that in this case, for named volumes, the source is the name of the volume (For anonymous volumes, this field is omitted).