Search code examples
linuxdockerdocker-composedevopsdocker-swarm

How to copy directories in docker container except any one directory?


I have some directories/files, I want to copy all directories/files except my_data directory from host to Docker container Below command is working fine to copy all the directories/files in the container:

docker cp ./ container_name:/my-dir

How can I copy all the directories/files from host to container except my_data dir.


Solution

  • Currently there is no such way to do a --exclude while doing a docker cp. If you have a Dockerfile, you can achieve this by using .dockerignore.

    Below is what i can suggest now as a quick hack/workaround -

    mkdir /tmp/to_be_copied
    rsync -avzh ./ /tmp/to_be_copied --exclude my_data
    docker cp /tmp/to_be_copied 0132381bc8d6:/my_dir
    rm -rf /tmp/to_be_copied
    

    You copy your folder structure to a different location using rsync(pre-installed in many OS) with the directory my_data excluded. Post this you can easily use docker cp with the newly created directory, don't forget to cleanup at the end. Hope it helps!