tar-piping a dir in/out of a docker container

The bottom of the docker cp web page has some examples of tar-piping into and out of a container. I couldn't get them to work. I guess there are different varieties of tar. The following is what worked for me on Alpine Linux. It assumes
  • ${container} is the name of the container
  • ${src_dir} is the source dir
  • ${dst_dir} is the destination dir

copying a dir out of a container

docker exec ${container} tar -cf - -C $(dirname ${src_dir}) $(basename ${src_dir}) | tar -xf - -C ${dst_dir}

copying a dir into a container

tar -cf - -C $(dirname ${src_dir}) $(basename ${src_dir}) | docker exec -i ${container} tar -xf - -C ${dst_dir}

setting files uid/gid

A tar-pipe can also set the destination files owner/uid and group/gid:
tar --owner=UID --group=GID -cf - -C $(dirname ${src_dir}) $(basename ${src_dir}) | docker exec -i ${container} tar -xf - -C ${dst_dir}
This is useful because unlike [docker run] the [docker cp] command does not have a [--user] option.

Alpine tar update

The default tar on Alpine Linux does not support the --owner/--group options. You'll need to:
apk --update add tar

Hope this proves useful!

the design and evolution of cyber-dojo

I've talked about the design and evolution of cyber-dojo at two conferences this year. First at NorDevCon in Norwich and then also at Agile on the Beach in Falmouth. Here's the slides.