Best practice - Anonymous volume vs bind mount

前端 未结 2 458
别跟我提以往
别跟我提以往 2021-01-19 05:36

In a container,

anonymous volume can be created

with syntax(VOLUME /build) in Dockerfile

or

below syntax with

2条回答
  •  时光取名叫无心
    2021-01-19 06:20

    You're missing a key third option, named volumes. If you declare:

    version: '3'
    volumes:
      build: {}
    services:
      cache:
        image: ...
        volumes:
          - build:/build
    

    Docker Compose will create a named volume for you; you can see it with docker volume ls, for example. You can explicitly manage named volumes' lifetime, and set several additional options on them which are occasionally useful. The Docker documentation has a page describing named volumes in some detail.

    I'd suggest that named volumes are strictly superior to anonymous volumes, for being able to explicitly see when they are created and destroyed, and for being able to set additional options on them. You can also mount the same named volume into several containers. (In this sequence of questions you've been asking, I'd generally encourage you to use a named volume and mount it into several containers and replace volumes_from:.)

    Named volumes vs. bind mounts have advantages and disadvantages in both directions. Bind mounts are easy to back up and manage, and for content like log files that you need to examine directly it's much easier; on MacOS systems they are extremely slow. Named volumes can run independently of any host-system directory layout and translate well to clustered environments like Kubernetes, but it's much harder to examine them or back them up.

    You almost never need a VOLUME directive. You can mount a volume or host directory into a container regardless of whether it's declared as a volume. Its technical effect is to mount a new anonymous volume at that location if nothing else is mounted there; its practical effect is that it prevents future Dockerfile steps from modifying that directory. If you have a VOLUME line you can almost always delete it without affecting anything.

提交回复
热议问题