“a bind mount won't copy the container contents to the host automatically, unlike a named volume”

北城余情 提交于 2021-01-24 09:44:07

问题


Need clarity on a comment here:

The only 'problem' with a bind mount is that it won't copy the container contents to the host automatically, unlike a named volume. docs.docker.com/compose/compose-file/#volumes

Is this accurate? If yes, then:

  1. how does one get the container's "new data" (e.g. a growing database) into the host when using a bind mount (to persist the data in case of a container restart)?
  2. how did Docker persist data across container restarts before there were named volumes?

回答1:


I think @BMitch having the accepted answer is correct, but I will just try to add in some details with the hope of being useful.

Is this accurate? If yes, then:

Given it is my claim being scrutinised - I totally defer to @BMitch here :)! However I would also add:

  • https://github.com/docker/compose/issues/4581#issuecomment-389559090
    • Provides a layman explanation of how named volumes / host volumes behave
    • My explanation needs updated to reflect the notion of 'initialization'
  • https://stackoverflow.com/a/40030535/3080207
    • This is how I would recommend setting up volumes in docker-compose at the moment, courtesy of @kaiser

how does one get the container's "new data" (e.g. a growing database) into the host when using a bind mount (to persist the data in case of a container restart)?

Both host volumes and named volumes can achieve this.

I think the point of contention is what you want to happen on the:

  • first run of the container
  • subsequent runs of the container and
  • the location/accessibility of the volume on the host system.

Once a volume is attached to a container (be it a named volume or bind mount), whatever is stored to that volume should be persisted between restarts - that effectively comes for free. This assumes the same docker-compose config, and no manual removal of volumes.

Previously it was a bit limiting using a named volume, as you couldn't tail logs, or edit code directly from the host as easily as you could with a bind mount - but it seems that problem is resolved / has a work around now.

Bind mounts are able to persist data between restarts. I personally find that bind volumes do what I want 99% of the time, that being said, named volumes can now 'do it all' and I'd be using those moving forward.

There are differences between them though, and I'm sure they'll still bite people occasionally, requiring them to reach out to actual experts, instead of users like me :).




回答2:


The only 'problem' with a bind mount is that it won't copy the container contents to the host automatically, unlike a named volume.

Is this accurate?

Close to accurate, but I can see the confusion. Host volumes, aka bind mounts, do not have an initialization feature from docker. With anonymous and named volumes, docker will initialize the volume with the contents of the image at that path. This initialization includes ownership and permissions which helps avoid permission errors. This initialization only runs when the container is created and the volume is new or empty, so subsequent containers will not pickup changes to the image made in newer image versions.

If yes, then:

  1. how does one get the container's "new data" (e.g. a growing database) into the host when using a bind mount (to persist the data in case of a container restart)?

Reads and writes from the app in the container will continue through to the host filesystem used in the bind mount as expected. It's only the initialization step that doesn't run.

  1. how did Docker persist data across container restarts before there were named volumes?

There were data containers, mounting volumes from other containers, but this was inflexible (all volume paths were fixed to the path in the data container) and mixed management of persistent data with ephemeral containers, and has therefore been phased out.

Volumes are used to handle data persistence between containers. A single container restarting (rather than being replaced) will still have all the container specific filesystem changes. The docker rm command deletes these filesystem changes, along with container logs and metadata/configuration of the container.

The container specific changes are the read/write top layer of an overlay filesystem used by docker. Volume mounts are all separate mounts into subdirectories of this overlay filesystem (just like /home or /var are often separate filesystem mounts in the / filesystem of a Linux host, all reads and writes to those other paths go to a separate underlying filesystem).




回答3:


If you're going to mount a volume into a container, and you want that volume to reliably contain some content from the image, you need to manually copy it there at container startup time. One way to do this is with an entrypoint wrapper script:

#!/bin/sh
# Copy data into a possibly-mounted location
cp -a /app/static /var/www
# Then run the image's CMD
exec "$@"

You'd include this in your image's Dockerfile

# Must use JSON-array syntax
ENTRYPOINT ["/app/entrypoint.sh"]
CMD same as it was before

There are two important details about Docker named volumes' initialization behavior to be aware of here. The first, which you note, is that Docker only copies content into a volume for Docker named volumes; it doesn't happen for bind mounts, and it doesn't happen in other environments like Kubernetes.

The second, more subtle detail is that the initialization only happens the first time the container runs. If there's already content in a volume that you mount into a container, it will hide what was already there. In other SO questions you can see this manifest as, for example, "I added a package to my Node package.json file, but when I put the node_modules directory in a volume, it ignores the update" or "I'm using a volume to export content to an nginx proxy but it doesn't update".



来源:https://stackoverflow.com/questions/65176940/a-bind-mount-wont-copy-the-container-contents-to-the-host-automatically-unlik

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!