skip to Main Content

I have a bash script that does a few things, one of which is to docker cp a file to the host.

When I issue that command (without variable expansion) on the command line, the command works. It works with variable expansion on the command line as well. But the command fails in a script; even if that script is on the command line.

The command in the script is:

docker cp ${container_variable}:/some/path/file_name-${date_tag}.zip /host/path

When i try this in a command line script:

for file in `docker exec container ls -1 file_name*`
do
   docker cp container:/some/path/$file .
done

I get this (there are four such files that match that wildcard):

Error response from daemon: Could not find the file file_name-1.zip in container container
Error response from daemon: Could not find the file file_name-2.zip in container container
Error response from daemon: Could not find the file file_name-3.zip in container container
Error response from daemon: Could not find the file file_name-4.zip in container container

I just cannot figure out what the issue is here. Any thoughts?

2

Answers


  1. Chosen as BEST ANSWER

    I resolved this by just mounting a directory into the container and accessing the file directly on the host. Thanks to David Maze for the suggestion.


  2. docker cp command uses root directory. From the documentation:

    The docker cp command assumes container paths are relative to the
    container’s / (root) directory.

    While docker exec uses current working directory (WORKDIR).

    So the solution should be to look up what is the workdir in your docker container and add that to your copied path:

    for file in `docker exec container ls -1 file_name*`
    do
       docker cp container:/some/path/$file .
    done
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search