简体   繁体   中英

Is there a way to copy the contents of the directory into the Docker container?

Pretty much the title says it all. I know I can copy the file (from the host) into a docker container. I also know I can copy the directory into a docker container. But how to copy the contents of a directory (preserving all subdirectories) into a directory in a docker container?

On my host I have a directory called src . On the docker container I have a directory /var/www/html . That src has both files and directories . I need all of them to be copied (with the command) into the container; not bound, not mounted, but copied.

It sounds like a trivial operation, but I've tried so many ways and couldn't find anything online that works! Ideally, it would be best if that copy operation would work every time I run the docker-compose up -d command.

Thanks in advance!

I found the solution. There is a way of specifying the context directory explicitly; in that case the dockerfile also need to be specified explicitly too.

In the docker-compose.yml one should have the following structure:

services:
  php:
    build:
      context: .
      dockerfile: ./php/Dockerfile

In this case the src is "visible" because it is inside the context! Then in that Dockerfile the COPY command will work!

Update: There is another way to achieve this via the command as well. However for me it started to work when I've added the ./ at the end. So the full command is:

docker cp ./src/./ $(docker-compose ps|grep php|awk '{print $1}'):/var/www/html/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM