Skip to content

Instantly share code, notes, and snippets.

@ragboyjr
Created August 13, 2018 06:39
Show Gist options
  • Save ragboyjr/9bc14e4f32ebe6366275480a3a92a61f to your computer and use it in GitHub Desktop.
Save ragboyjr/9bc14e4f32ebe6366275480a3a92a61f to your computer and use it in GitHub Desktop.
Docker Sync Volume Directory
#!/usr/bin/env bash
container_name=$1
container_tar_path=$2
local_lock_path=$3
vol_path=$4
if [[ -z "$container_name" ]] || [[ -z "$container_tar_path" ]] || [[ -z "$local_lock_path" ]] || [[ -z "$vol_path" ]]; then
echo "usage: $0 <container-name> <container-tar-path> <local_lock_path> <vol_path>" && exit 1;
fi
if [[ ! -f ${local_lock_path} ]]; then
touch ${local_lock_path}
fi
if [[ "$(docker-compose exec ${container_name} md5sum ${container_tar_path})" == "$(cat ${local_lock_path})" ]]; then
exit;
fi
echo "Changes detected in ${vol_path} loading into local"
docker_container_name=$(docker-compose ps | tail -n +2 | awk '{ print $1 }' | grep "_${container_name}_") || exit $?
tar_name=$(basename ${container_tar_path})
local_dir=$(dirname ${local_lock_path})
local_tar_path="${local_dir}/${tar_name}"
# copy the container tar to local
docker cp $docker_container_name:$container_tar_path $local_tar_path
rm -rf ${vol_path}
tar -xzf $local_tar_path -C $local_dir
rm $local_tar_path
docker-compose exec $container_name md5sum $container_tar_path > $local_lock_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment