rsync : how to sync between docker and host os

rsync is the most effective utility for efficient file synchronization between your Docker container and the host operating system. With advancements in web development technologies, the way of deployments are also evolved over the period of time. If you are a newbie developer in docker containerization, then this article is for you. Below are the key methods and clear steps for the most common use cases of rsync.

rsync

Prerequisites to follow this article:

  • Both host and container must have rsync installed.
  • The files or directories to be synchronized should not be in use (locked) during sync for best results.
  • For certain approaches, you may need SSH access or open ports.

Method 1: Use Docker Volumes and Sync Inside the Container using rsync

The simplest, most robust solution is to use a mounted Docker volume that is shared between the host and container and run rsync directly:

1. Mount a Host Directory:

docker run -v /host/mydir:/container/mydir -it container_image

2. From the host, push files into the container:

rsync -av /some/source/ /host/mydir/

3. From inside the container, pull/update files:

rsync -av /container/mydir/ /some/dest/

This method works instantly since the host and container share the same files via a mapped directory

Method 2: Direct Host-Container Sync with rsync over docker exec

If you can’t use shared volumes, you can sync using docker exec:

1. Make sure rsync is installed in the container:

docker exec container_name which rsync

If not, install it (e.g., apt update && apt install rsync for Debian-based images).

2. Sync host to container:

rsync -av --progress /local/source/ <container_name>:/container/destination/

Since rsync cannot connect natively to containers, you can create a shell alias:

alias drsync="docker exec -i container_name rsync"

Then use:

rsync -av /local/source/ /container/destination/

Note: This is a workaround and may need adjustment for complex setups

 Use rsync Over SSH Between Host and Container

Set up an SSH service in either the container or host (not generally recommended for short-lived containers):

Expose port 22 from the container:

docker run -p 2222:22 -it container_image

From the host:

rsync -av -e "ssh -p 2222" /local/source/ user@localhost:/container/destination/

From the container to host (if SSH on host):

rsync -av -e "ssh" /container/source/ user@host_ip:/host/dest/

This method allows standard rsync over a network protocol

rsync Points To Remember:

  • Use Docker volumes and host-based rsync for most predictable syncing.
  • Using docker cp for quick, full directory copy is possible, but it does not offer incremental/differential sync.
  • Two-way sync requires careful scripting or volume management to avoid file conflicts.

Summary:

  • For most development purposes, use Docker volumes with rsync running on the host for maximum efficiency and simplicity.
  • When direct volume usage is not possible, set up rsync with docker exec or SSH as appropriate to your setup.

Always ensure rsync is installed on both endpoints you are synchronizing

Leave a Comment

Please disable your adblocker or whitelist this site!