docker-compose和Dockerfile的volumes

背景:

项目在使用Dockerfile的volumes持久化容器内数据时,发现创建新的容器时会重新创建新的volume,并不会使用之前的volume。

查阅资料:

https://segmentfault.com/q/1010000021756556?utm_source=tag-newest
https://stackoverflow.com/questions/40567451/dockerfile-vs-docker-compose-volume
https://docs.docker.com/engine/reference/builder/
https://blog.csdn.net/The_Time_Runner/article/details/106748713

结论:

我理解 dockerfile的 volume 是从容器映射到宿主机,由内到外的, 但是不能由外到内;
所以每次 创建新容器, 它会自动把 内部的数据映射出来,但是不会使用之前创建的volume数据。
所以做容器内数据映射到宿主机并且双向映射时推荐使用docker-compose的volumes

原文地址:https://www.cnblogs.com/liuwei0824/p/14790260.html