Brenda Olivia Martis
Brenda Olivia Martis

Reputation: 563

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

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?

Upvotes: 15

Views: 32712

Answers (5)

Milo Chen
Milo Chen

Reputation: 3975

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/

Upvotes: 10

Andrew Sharpe
Andrew Sharpe

Reputation: 78

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

Upvotes: 1

Tobias Ferrari
Tobias Ferrari

Reputation: 9

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.

Upvotes: 0

sauerburger
sauerburger

Reputation: 5148

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.

Upvotes: 4

Robert
Robert

Reputation: 36823

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/

Upvotes: 8

Related Questions