Copy files to Docker volume
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: copy-to-docker-volume SRC_PATH DEST_VOLUME_NAME [DEST_PATH] | |
copy-to-docker-volume() { | |
SRC_PATH=$1 | |
DEST_VOLUME_NAME=$2 | |
DEST_PATH="${3:-}" | |
# create smallest Docker image possible | |
echo -e 'FROM scratch\nLABEL empty=""' | docker build -t empty - | |
# create temporary container to be able to mount volume | |
CONTAINER_ID=$(docker container create -v my-volume:/data empty cmd) | |
# copy files to volume | |
docker cp "${SRC_PATH}" "${CONTAINER_ID}":"/data/${DEST_PATH}" | |
# remove temporary container | |
docker rm "${CONTAINER_ID}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create volume as destination | |
docker volume create my-volume | |
# create directory to copy | |
mkdir my-dir | |
echo "hello file1" > my-dir/my-file-1 | |
# copy directory to volume | |
copy-to-docker-volume my-dir my-volume | |
# list directory on volume | |
docker run --rm -it -v my-volume:/data busybox ls -la /data/my-dir | |
# show file content on volume | |
docker run --rm -it -v my-volume:/data busybox cat /data/my-dir/my-file-1 | |
# create another fil to copy | |
echo "hello file2" > my-file-2 | |
# copy file to directory on volume | |
copy-to-docker-volume my-file-2 my-volume my-dir | |
# list (updated) directory on volume | |
docker run --rm -it -v my-volume:/data busybox ls -la /data/my-dir | |
# check volume content | |
docker run --rm -it -v my-volume:/data busybox cat /data/my-dir/my-file-2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment