Sync Hacks: Deploy Resilio Sync (BitTorrent Sync) with Docker

homepage-docker-logovia Docker

In Sync Hacks, we spotlight cool uses of Sync from the creative minds of our users. Sync is our free, unlimited, and secure file-syncing application. If you have an interesting use or how-to, shoot us an email at sync[at]bittorrent.com. Can’t wait to hear what you guys cook up.

In this week’s Sync Hacks, Bill Thornton (@billt2006) shows you how to build a Docker image that runs BitTorrent Sync, so that you can deploy BitTorrent Sync on any machine with Docker installed. Read on for Bill’s full tutorial.

The code for this project is available on GitHub.

Why Resilio Sync

I discovered Resilio Sync (formerly BitTorrent Sync) when trying to find a cross-platform solution to sync files to a server on a LAN. Resilio Sync fit my needs because:
• It’s cross-platform
• It’s encrypted using a unique session key (no master key)
• It’s peer-to-peer (no central “cloud” server)
• There are official mobile apps

Why Docker?

Docker uses Linux Containers (LXC) to provide you with an environment similar to a virtual machine but without the overhead of actually virtualizing hardware. This allows you to create a lightweight encapsulated instance of an application that is easy to reproduce.

Because the application is encapsulated, access to the host system is limited to what you explicitly allow. It also makes running multiple instances of the application on a single host trivial.

Steps

Install Docker

Docker has two dependencies:
• Linux Kernel 3.8 for LXC support
• AUFS file system support

Docker can also be ran in operating systems other than Linux using VirtualBox and Vagrant. Full installation instructions for your system are available on the Docker website.

Write the Dockerfile

A Dockerfile is used to define a set of commands to build a Docker image.

1. Create an empty file named Dockerfile in a directory named docker-btsync.

2. The first line in a Dockerfile must define what base image is to be used. Our image will be based on Ubuntu, so it should start with this:

FROM ubuntu

3. Next define some commands that will be run when building the new image. We need to install curl, use curl to download the BitTorrent Sync client and extract the client to /usr/bin/.

RUN apt-get update && apt-get install -y curl
RUN curl -o /usr/bin/btsync.tar.gz http://download-lb.utorrent.com/endpoint/btsync/os/linux-x64/track/stable
RUN cd /usr/bin && tar -xzvf btsync.tar.gz && rm btsync.tar.

4. The BitTorrent Sync client needs to communicate over two ports, one for the web ui and one for the listening port. We will use EXPOSE to make two ports in our Docker image public and later we will configure BitTorrent Sync to use the same ports.

EXPOSE 8888
EXPOSE 55555

5. BitTorrent Sync should be ran by default when we run the Docker container, so we use ENTRYPOINT to accomplish this and CMD to specifty some default parameters for BitTorrent Sync.

ENTRYPOINT ["btsync"]
CMD ["--config", "/btsync/btsync.conf", "--nodaemon"]

Configure BitTorrent Sync

1. If you have BitTorrent Sync installed on your host system, you can start with the sample config. Otherwise, you can find one in my project repository here.

btsync --dump-sample-config > btsync.conf

2. Change the listening_port to match the port that is exposed in the Dockerfile.

"listening_port" : 55555

3. Set the storage_path so it will save to a directory that is mounted to the host system.

"storage_path" : "/btsync/.sync"

4. Make sure that inside "webui": {} the port matches what was exposed in the Dockerfile for the web ui.

"listen" : "0.0.0.0:8888"

5. Make any other changes you desire (disable the web ui, limit transfer rates, etc.).

Build and Run

1. To build the Docker image, enter the following command in the terminal.

docker build -t="btsync" .

Where -t="btsync" sets the name of the image you are building and . is the path to the directory that contains the Dockerfile.

2. Now that you have built the image, you can run an instance of the image with this command.

docker run -d -p 8888:8888 -p 55555:55555 -v /srv/btsync/:/btsync/ btsync

-d runs the Docker container in detached mode.
-p exposes a container port [public-port]:[container-port]. The container ports were set to 8888 for the webui and 55555 for the listening port in btsync.conf. If a public port is not specified, docker will assign exposed ports to a random open port.
-v mounts a local directory in the container [host-dir]:[container-dir]. The btsync.conf file should be located in a directory mounted to the /btsync/ in the container. Also mount any directory you wish to sync if it is not already in the same directory as the btsync.conf file.
btsync is the name of the image.

3. To verify that the container is running, run docker ps. This command will list all running Docker containers.

Success

Now with a simple Dockerfile and BitTorrent Sync config file, it is possible to deploy BitTorrent Sync to any machine that has Docker installed. Also, multiple instances of BitTorrent Sync can run on the same host by issuing multiple docker run commands. This could allow you to sync to multiple directories on a single host or have dedicated web interfaces for multiple users.

Bill Thornton (@billt2006) is a web developer and Linux enthusiast from West Virginia. He develops enterprise level websites for Exclamation Labs of Cumberland, MD.

Please follow and like us: