Skip to content

Instantly share code, notes, and snippets.

@VSharapov
Last active September 16, 2025 15:20
Show Gist options
  • Select an option

  • Save VSharapov/462a837249c719918e74f982f6cc1ad0 to your computer and use it in GitHub Desktop.

Select an option

Save VSharapov/462a837249c719918e74f982f6cc1ad0 to your computer and use it in GitHub Desktop.
Will docker cp overwrite stuff on the destination as expected?
#!/usr/bin/env bash
for test in "bare file" "directory"; do
prefix=""
container_path="/foo"
if [ "$test" = "directory" ]; then
mkdir -p "/tmp/foodir"
prefix="/foodir"
container_path="/foodir"
fi
echo "foo" > "/tmp${prefix}/foo"
original_content=$(cat "/tmp${prefix}/foo")
docker rm -f cp-sanity-test 2>/dev/null >/dev/null
docker create --name cp-sanity-test busybox:latest sleep infinity
docker cp "/tmp${container_path}" cp-sanity-test:$container_path
docker start cp-sanity-test >/dev/null
docker exec cp-sanity-test sh -c "echo 'bar' > ${prefix}/foo"
docker cp cp-sanity-test:$container_path "/tmp${container_path}"
output=" ✅"
[ "$(cat "/tmp${prefix}/foo")" = "foo" ] && output=" NOT ❌"
echo "\`docker cp\` DOES$output overwrite a $test"
echo "Original content: $original_content"
echo " Current content: $(cat "/tmp${prefix}/foo")"
echo "Expected content: $(docker exec cp-sanity-test cat ${prefix}/foo)"
docker kill cp-sanity-test >/dev/null
docker rm -f cp-sanity-test 2>/dev/null >/dev/null
rm -rf "/tmp${container_path}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment