15

I am trying to run following:

  1. start a container in background

docker run -dit -p 8090:80 --name container repository:dockerfile bash

  1. I want to exclude sub directory /data from /test

docker cp /Users/$USER/test container:/test

  1. I thought of using rsync for this docker exec rsync -avP --exclude /Users/$USER/test/data /Users/$USER/test/ container:/test/

I get below error:

rsync: Failed to exec ssh: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(85) [sender=3.1.0]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(226)
[sender=3.1.0]

How do I rsync from host to container?

5 Answers 5

8

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/
0
8

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/
3
  • protocol version mismatch -- is your shell clean? (see the rsync man page for an explanation) rsync error: protocol incompatibility (code 2) at compat.c(178) [sender=3.1.3] Should I add bin/bash following?
    – Kevin wang
    Commented Oct 26, 2022 at 1:03
  • How would you do this if the container is running on a remote server? Let's say, I use this command, to transfer files directly to the server: "rsync -avP /source/ user@remote_server:/destination" and the container name is "container".
    – Irina S.
    Commented Feb 1, 2023 at 16:09
  • 2
    This is great! I would put more emphasis on "Make sure your Docker container has rsync installed". Without that, you'll get the cryptic error: "protocol version mismatch -- is your shell clean?" Commented Aug 26, 2023 at 14:31
4

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.

2
  • 1
    rsync has a server mode, you dont need ssh for that Commented Apr 16, 2019 at 11:28
  • You have to worry about protocol version mismatch if you don't install ssh. I ended up installing ssh too. Commented Mar 16, 2021 at 2:14
1

This should mirror the command you attempted in the OP...

rsync -avPe 'docker exec -i' --exclude /Users/$USER/test/data /Users/$USER/test/ container:/test/

It works similarly to get files out of the container.

Credit to https://github.com/moby/moby/issues/13660#issuecomment-165500701

0

The

rsync: Failed to exec ssh: No such file or directory (2)

is caused because ssh is not installed for you container. You install it manual or add it to your requirements file to install it automaticly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.