Copying files to a container with Docker Compose

混江龙づ霸主 提交于 2019-11-27 05:21:24

问题


I have a Dockerfile where I copy an existing directory (with content) to the container which works fine:

Dockerfile

FROM php:7.0-apache
COPY Frontend/ /var/www/html/aw3somevideo/
COPY Frontend/ /var/www/html/

RUN ls -al /var/www/html
RUN chown -R www-data:www-data /var/www/html 
RUN chmod -R 755 /var/www/html 

But when I use a docker-compose.yml file there is only the directory aw3somevideo and inside aw3somevideo there is nothing.

docker-compose.yml:

 php:
    build: php/
    volumes:
      - ./Frontend/ :/var/www/html/
      - ./Frontend/index.php :/var/www/html/
    ports:
      - 8100:80

Maybe I do not understand the function of volumes and if that's the case please tell me how to copy my existing files to the container via a docker-compose.yml file.


回答1:


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



来源:https://stackoverflow.com/questions/39176561/copying-files-to-a-container-with-docker-compose

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