How Do You Copy Many Files from Your Linux Server into a Docker Container?

Problem scenario
You want to move copies of all the files in a given subdirectory (we'll say /tmp/a/ for an example) on a Docker host to go into a Docker container.  

Solution
From the server use the command "docker ps" to find the container ID.  Now that you have the container ID, you can write a script to move copies of the files into the Docker container.  Write this Bash script (with its final word being "done"):

#!/bin/bash
fir="docker cp "
las=" d387209ce13:/tmp/cont/"

for filename in /tmp/a/*; do
           full=$fir$filename$las
           eval $full
           done

Use this command: chmod u+x nameOfScript.sh
Change the "d387209ce13" your the container ID.  Change the /tmp/cont/ to the desired destination directory path in the Docker container. Run it (e.g., ./nameOfScript.sh) in the directory on the Docker host that has all the files you want copied (the location of the source files).  For future reference, the eval command is very useful.  Shell scripts can compose complex bash expressions and ultimately execute them easily with the eval command.

Leave a comment

Your email address will not be published. Required fields are marked *