how to rsync from a host computer to docker container using docker cp

When you run docker exec rsync [...] you execute the rsync command in your container. The path /Users/$USER/test/ corresponds to a directory on you host system, so rsync has a hard time finding it in your container.

There are basically two ways to use rsync to transfer files into a container:

  1. You can install an ssh server on you host system and use rsync to connect from within your container to the host system from the outside. If you have a running ssh server on you host and the host is reachable under the name host you can do

    docker exec rsync -avP --exclude /Users/$USER/test/data host:/Users/$USER/test /test
    
  2. You can install an ssh server inside the container and use rsync on your host system to connect to the container from the outside. I assume your container is reachable under the name container, then you can do

    rsync avP --exclude /Users/$USER/test/data /Users/$USER/test container:/test
    

    In this case you have to make sure that the ssh port (default is 22) is published by the docker daemon.


The way to use rsync to copy files into a Docker container

Make sure your Docker container has rsync installed, and define this alias:

alias drsync="rsync -e 'docker exec -i'"

Now, you can use rsync with containers as if they are remote machines:

drsync -av /source/ container:/destination/

Map the host directory first, into the container:

docker run -v /Users/$USER/test:/temp-test -dit -p 8090:80 --name container repository:dockerfile bash

Then use the rsync as follows:

docker exec container rsync -avP --exclude /temp-test/data /temp-test/ /test/