Docker匿名Voumes

我已经在Docker docker-compose.yml文件中看到了Docker卷的定义,如下所示:

 -v /path/on/host/modules:/var/www/html/modules 

我注意到, Drupal的官方形象 ,他们docker-compose.yml文件正在使用匿名卷 。

注意评论:

 volumes: - /var/www/html/modules - /var/www/html/profiles - /var/www/html/themes # this takes advantage of the feature in Docker that a new anonymous # volume (which is what we're creating here) will be initialized with the # existing content of the image at the same location - /var/www/html/sites 

有没有办法在容器运行后将匿名卷与主机上的path关联起来? 如果不是,匿名卷有什么意义?

完整的docker-compose.yml例子:

 version: '3.1' services: drupal: image: drupal:8.2-apache ports: - 8080:80 volumes: - /var/www/html/modules - /var/www/html/profiles - /var/www/html/themes # this takes advantage of the feature in Docker that a new anonymous # volume (which is what we're creating here) will be initialized with the # existing content of the image at the same location - /var/www/html/sites restart: always postgres: image: postgres:9.6 environment: POSTGRES_PASSWORD: example restart: always 

匿名卷相当于将这些目录定义为映像的Dockerfile中的VOLUME。 事实上,在Dockerfile 定义为VOLUME的目录匿名卷,如果它们没有明确映射到主机的话。

有他们的点是增加了灵活性。

PD:匿名卷已经驻留在/ var / lib / docker(或者你configuration的任何目录)的某个地方。 要看他们在哪里:

docker inspect --type container -f '{{range $i, $v := .Mounts }}{{printf "%v\n" $v}}{{end}}' $CONTAINER

注意:用容器名称replace$CONTAINER