0

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!

  • 1
    Create a `Dockerfile` and use the `COPY` command? https://docs.docker.com/engine/reference/builder/#copy – Ilya Jun 25 '22 at 12:55
  • I tried it too, but I did get an error: failed to solve: failed to compute cache key: "/src" not found. I assume this is happening because my `src` directory is outside the Dockerfile scope; but not sure. Any ideas on how to fix that error? – Tomas Baranauskas Jun 25 '22 at 15:38
  • From the link provided above "will be interpreted as relative to the source of the context of the build". You must specify the directory path relative to the build directory. For example you are trying to build from the directory `/Users/user.name/projects/build_directory` and the folder you want to copy is in `/Users/user.name/projects/copy_directory`. You will write `COPY ../copy_directory copy_directory` and it will copy the `copy_directory` to the root of the docer. – Ilya Jun 25 '22 at 16:18
  • 1
    Nope, it will not because the part "../" is outside of that context. Try it yourself. Check this thread: https://stackoverflow.com/questions/27068596/how-to-include-files-outside-of-dockers-build-context – Tomas Baranauskas Jun 25 '22 at 16:29
  • Interesting. Another solution might be creating a shell file that copies the desired directory to the build directory, executes the build process, and finally deletes the folder. That is not a "clean" solution but better than nothing. Overall it is a good practice to create shell files to build dockers as the process usually becomes complicated with time. – Ilya Jun 25 '22 at 16:52

1 Answers1

2

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/