how to copy files from one docker service to another, inside of docker bash

I am trying to copy a file from one docker-compose service to another while in the service's bash environment, but I cannot seem to figure out how to do it.

Can anybody provide me with an idea?

Here is the command I am attempting to run:

(docker cp ../db_backups/latest.sqlc pgadmin_1:/var/lib/pgadmin/storage/mine/)

The error is simply:

bash: docker: command not found


Solution 1:

There's no way to do that by default. There are a few things you could do to enable that behavior.

The easiest solution is just to run docker cp on the host (docker cp from the first container to the host, then docker cp from the host to the second container).

If it all has to be done inside the container, the next easiest solution is probably to use a shared volume:

docker run -v shared:/shared --name containerA ...
docker run -v shared:/shared --name containerB ...

Then in containerA you can cp ../db_backups/latest.sqlc /shared, and in containerB you can cp /shared/latest.sqlc /var/lib/pgadmin/storage/mine.

This is a nice solution because it doesn't require installing anything inside the container.

Alternately, you could:

  • Install the docker CLI inside each container, and mount the Docker socket inside each container. This would let you run your docker cp command, but it gives anything inside the container complete control of your host (because access to docker == root access).

  • Run sshd in the target container, set up the necessary keys, and then use scp to copy things from the first container to the second container.