Home > Enterprise >  How to copy a file between docker containers?
How to copy a file between docker containers?

Time:02-11

I have two different docker containers, each of them runs a PHP application. The problem that I have to solve is copy a list of files (using the PHP copy command) from container 1 to container 2.

Eg:

copy('var/www/html/uploads/test.jpg', 'var/www/html/site/uploads/test.jpg');

Now, the container 1 doesn't have access to container 2 which is site.

What is the best way to fix this?

CodePudding user response:

Use a shared volume to transfer data. So mount

-v filetransfer:/var/www/html/transfer
or
--mount type=volume,source=filetransfer,destination=/var/www/html/transfer

to both containers. If both containers running different non-root users you have to ensure file permissions are set accordingly. If you want to avoid file corruption use :ro (read-only) for all but one containers or ensure that by code.

Other comments:

  • docker cp is used to copy files from host to container or vice versa.
  • Building a REST-API for copying a single file is in my opinion a little bit over-engineered..
  • Related