Copying files to a container with Docker Compose

﹥>﹥吖頭↗ 提交于 2019-11-28 04:28:06

Updated April 2017

The behaviour has changed since I wrote the original answer. It is now consistent whether the right hand side specifies a named volume like myvolume or a path on the host like /var/lib/myapp. For instance

    volumes:
      - /dir/on/host:/var/www/html

if /dir/on/host doesn't exists, it is created on the host and the empty content is mounted in the container at /var/www/html. Whatever was in /var/www/html before is inaccessible.

---- old answer -----------

The volumes: section in your docker-compose overwrites whatever is in the /var/www/html directory.

There are two mains situations:

  1. The volume exists

    In that case, the content of the volume overshadows whatever is in the dst directory.

    Eg:

    volumes:
      - /dir/on/host:/var/www/html
    
  2. The volume doesn't exist

    If myvolume doesn't exist (a named volume for instance), the content of /var/www/html will be copied to volume the first time around

    volumes:
      - myvolume:/var/www/html
    

In case 2, if you try to mount the same volume again on some container, it will follow case 1.

    volumes:
      - myvolume:/var/www/html

In that case (assuming myvolume was already created), the content of /var/ww/html will be overwritten (shadowed) by whatever is in myvolume.

The official doc goes into more details https://docs.docker.com/compose/compose-file/#/volumes-volume-driver

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