Copying files to a container with Docker Compose

2019-02-01 05:02发布

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 

Screenshot of directory listing with docker exec

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

Screenshot of empty directory listing

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条回答
爷的心禁止访问
2楼-- · 2019-02-01 05:25

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

查看更多
登录 后发表回答