Skip to content

Instantly share code, notes, and snippets.

@BraINstinct0
Created December 8, 2022 09:51
Show Gist options
  • Save BraINstinct0/5ff8d62efdcd4bb0931e5def4e353a08 to your computer and use it in GitHub Desktop.
Save BraINstinct0/5ff8d62efdcd4bb0931e5def4e353a08 to your computer and use it in GitHub Desktop.
Bash script to copy file/folder into Docker Volume
# check if docker is running
docker 1> /dev/null
# success?
if [ $? -eq 0 ]; then
# folder/file exist?
if test -f "$2"; then
docker container create --name dummy -v $1:/root tianon/true
docker cp "$2" dummy:"/root/$3"
docker rm dummy
echo "Copied $2 to $3 in Volume $1"
else
echo "$2 does not exist!"
fi
else
echo "Docker is not running!"
fi
@BraINstinct0
Copy link
Author

BraINstinct0 commented Dec 8, 2022

Syntax: . ./CP2DockerVol.sh Volume_Name src dst

  • Checks if Docker is running
  • Checks if file/folder to copy exists (src)
  • Creates Volume of given name if absent
  • Remove temporary container used for the task

TO DO

  • Change dummy to random, unused container name (currently unwanted behavior if container dummy already exist)
  • Use flags instead of positional params
  • Use better error handling
  • Use better Docker run status check (other than exit code)
  • Verbose output for large file/folder copy (progress bar, etc.)
  • Integrate WSL usage
  • Add pwsh script for usage outside WSL (directly from powershell)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment