docker data volume vs mounted host directory

后端 未结 4 1832
悲&欢浪女
悲&欢浪女 2020-11-30 22:31

We can have a data volume in docker:

$ docker run -v /path/to/data/in/container --name test_container debian
$ docker inspect test_container
...
Mounts\": [
         


        
4条回答
  •  伪装坚强ぢ
    2020-11-30 23:37

    Yes, this is quiet different from a few perspectives. Like you wrote in the question's title, it is all about understanding why we need data volumes vs bind mount to host.

    Part 1 - Basic scenarios with 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).

    Part 2 - Comparison

    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 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.

提交回复
热议问题