Setting up Syncthing and Configuring Relay/Discovery Server - Using Docker - Zhihu#
#Omnivore
Important!!! Relay servers and discovery servers are not mandatory. If you don't have a server, you can directly use default. The protocol listening address must have default! The protocol listening address must have default! The protocol listening address must have default!
Introduction#
Syncthing is an open-source peer-to-peer file synchronization software. It allows users to sync files and folders between different devices without relying on third-party servers. Users can easily share files and folders, keep data synchronized and backed up, and it also offers high security. Syncthing has a simple interface and easy-to-use features, making it a powerful and reliable file synchronization tool.
Uses#
- Sync data from multiple phones to NAS
- Backup NAS data to other locations
Directory Organization#
Using backup tools requires a source and a destination. The source can be another device or a different local path. My Unraid has already used Duplicati to back up data and virtual machine data, all data is encrypted and stored in /unraid/backup_by_duplicati, which is a source path that needs to be backed up to another location. Another source is data synced from other devices, requiring a destination path to receive it, named /unraid/other_transfer_in. For data security, it is recommended to create a user backup_only and grant permissions to the two directories mentioned above. Use the command id backup_only to obtain its PUID, PGID. However, some directories may already be in use, and reassigning permissions may be troublesome; you can directly use administrator: PUID=0, PGID=0.
Get the docker-compose script from the official website:
---
version: "3"
services:
syncthing:
image: syncthing/syncthing
container_name: syncthing
hostname: my-syncthing
environment:
- PUID=0 #1000 is the default user which may not read other docker directories, set to 0 for administrator
- PGID=0
volumes:
- /unraid/backup_by_duplicati:/var/syncthing/backup_by_duplicati
- /unraid/other_transfer_in:/var/syncthing/other_transfer_in
#ports:
# - 8384:8384 # Web UI
# - 22000:22000/tcp # TCP file transfers
# - 22000:22000/udp # QUIC file transfers
# - 21027:21027/udp # Receive local discovery broadcasts
network_mode: "host" # Host mode. No need to pre-map ports
restart: unless-stopped
You can also search and install from the Unraid applications, configure it in privileged mode, set PUID, PGID to 0, add mapping paths, etc.
Start NAS Side#
Access: port 8384 or access the domain name configured through frp to successfully open the homepage:
There are three prompts, which are not configured with a password, not configured with a password, and the container should not be run with administrator privileges. Go set a password.
Configure Relay and Discovery Server (Optional)#
There is an article that I wrote when I set it up before, and looking back now it really is a mess haha. After "many years," it should be renovated, and I just saw that there is a relay and discovery server image on Docker Hub.
Create Relay#
Relay and discovery servers need a public IP. It is recommended to set up a server. The author has written it in detail, so I will copy and paste it here:
# Pull the image
docker pull johngong/syncthing-relay-discosrv:latest
# Create the image
docker create \
--name=syncthing-relay-discosrv \
-p 22067:22067 \
-p 22070:22070 \
-p 8443:8443 \
-e PROVIDED_BY=" www.xxdl.xyz" \
-e GLOBAL_RATE=2000000 \
-e POOLS="https://relays.syncthing.net/endpoint" \
--restart unless-stopped \
johngong/syncthing-relay-discosrv:latest
22067 : Relay data port, used for data connection
22070 : Relay status port used to report data
# Run
docker start syncthing-relay-discosrv
# Stop
docker stop syncthing-relay-discosrv
# Delete
docker rm syncthing-relay-discosrv
# View logs
docker logs syncthing-relay-discosrv
Note: When running the above command, it will prompt: Joining default relay pools, this relay will be available for public use. Use the -pools="" command line option to make the relay private.
This means you have joined the public relay pool, which can be discovered and used by others. If you do not want to join the public, you need to set -e POOLS="" to do so. Copying the command from the variables below may cause errors, so check the format and indentation.
Variables:#
Parameter | Description |
---|---|
--name=syncthing-relay-discosrv | Container name |
-p 22067:22067 | Sync relay server protocol listening port |
-p 22070:22070 | Sync relay server service status listening port |
-p 8443:8443 | Sync discovery server listening port |
-v /config/file/location:/config | Optional, /config/discosrvdb is the database location for the sync discovery server; /config/certs is the server certificate location. After setting, the certificate remains unchanged, and the device ID does not change after reinstallation. |
-e UID=1000 | UID setting, default is 1000 |
-e GID=1000 | GID setting, default is 1000 |
-e GLOBAL_RATE=100000000 | Global rate limit in bytes/s |
-e PER_SESSION_RATE=10000000 | Per session rate limit in bytes/s |
-e MESSAGE_TIMEOUT=1m30s | Maximum time to wait for related messages to arrive |
-e NATWORK_TIMEOUT=3m0s | Operation timeout between client and relay server |
-e PING_INTERVAL=1m30s | Frequency of ping sending |
-e PROVIDED_BY="strelaysrv" | Relay provider |
-e POOLS= | List of relay server addresses. If not filled, it will be a private relay. |
-e DISCO_OTHER_OPTION= | Other self-added options for the sync discovery server, -debug -http -listen -metrics -listen -replicate -replication-listen. For option descriptions, see: https://docs.syncthing.net/users/stdiscosrv.html |
-e RELAY_OTHER_OPTION= | Other self-added options for the sync relay server, -debug -ext-address -listen -nat -nat-lease -nat-renewal -nat-timeout -protocol -status-srv. For option descriptions, see: https://docs.syncthing.net/users/strelaysrv.html |
Bind Domain Name#
Use Baota to add two domain names: stdiscosrv.devitem.top reverse proxy 8443 port strelaysrv.devitem.top reverse proxy 22067 port.
Use Baota's SSL to generate HTTPS for stdiscosrv.devitem.top and remember to allow ports 8443, 22067, and 22070.
Using Relay and Discovery Servers#
- First, obtain deviceID
docker logs syncthing-relay-discosrv
# Server device ID is DG5LEG6-5YUOBMY-33IYXFE-EIRQW4E-MADRY5R-XRSCBKX-H7QSTCB-6M2KXAW
- Go to the client configuration section, open the Interface - Operations - Settings - Connections page.
Name | Example | Actual |
---|---|---|
Protocol Listening Address | relay://ip or domain: local port 1/?id=deviceID | relay://strelaysrv.devitem.top:22067/?id=DG5LEG6-5YUOBMY-33IYXFE-EIRQW4E-MADRY5R-XRSCBKX-H7QSTCB-6M2KXAW |
Global Discovery Server | https://ip or domain: local port 3/?id=deviceID, | https://stdiscosrv.devitem.top:8443/?id=DG5LEG6-5YUOBMY-33IYXFE-EIRQW4E-MADRY5R-XRSCBKX-H7QSTCB-6M2KXAW |
The image information is outdated; the text prevails.
Important Notes!!!
If you only want to use your own discovery server, just delete default from the global discovery server. Otherwise, fill in default, discovery server address. However, the protocol listening address must include default, because deleting it will cause devices even in the local area network to forward traffic through the relay server!!! Both discovery servers and relays support multiple entries, separated by commas.
The image information is outdated; the text prevails. After correct configuration, the effect is as shown:
After joining the public relay pool, you can find yourself in the official relay pool information.
Mobile Configuration#
Add Device#
After installing the APK, enter the web management page and fill in the relay and discovery servers:
Copy the device ID from the phone.
In the NAS management page, add the remote device and enter the phone's device ID:
The mobile side will prompt that there is a device request to connect:
Device added successfully.
Configure Directory Synchronization#
Add a directory to sync.
After saving, exit the mobile app first, then the NAS side will automatically prompt that there is a directory shared.
Select the Docker mapped directory in NAS and save the data synced from the phone to the desired location.
Conclusion#
After completing the configuration above, you can happily sync data. With a relay, you can sync even over the internet, and the recycling mechanism is quite clear, detailed on the page.
Others#
About Filling in Protocol Listening Address and Global Discovery#
The listening program (listening address) is, in my understanding, the link for incoming/outgoing connections. Only if others know this link can data be transmitted. The purpose of global discovery is an address book that records user listening addresses. Only when both devices are in the address book and can find each other based on the address can data transmission occur. The transmission speed depends on whether it connects directly (e.g., local area network) or via relay (relay server strelaysrv). The default state is as follows:
In the default state, the listening program and device discovery should be 3/3 and 4/5 (one device discovery cannot connect), but sometimes if the network is poor, the listening program can only listen to two local 22000 ports, which means that the external network cannot connect to you through the relay pool.
If only the relay pool is configured, do not write default, the listening is as follows: The listener only listens to its relay pool and does not listen to the local port, so all connections must come from the relay pool, unable to transmit data through the local area network.
However, if only the discovery server is specified, the device discovery status is as follows:
It contains not only its discovery address but also IPv4/6. However, through my testing, according to the above configuration, it is impossible to directly transmit data on the same Wi-Fi, because the listening address does not have default.
Therefore, the correct configuration is as follows:#
This explains: The protocol listening address specifies default, self-built relay pool, directly transmitting in the local area network, and external network environment through the relay. Enable global discovery, only fill in the self-built discovery server, so that only devices using this discovery server can connect with local devices. After this setting, data synchronization can be relatively stable. The issue that has troubled me before, where specifying a relay server could not transmit data over the local area network, has also been resolved.
Reference article: https://zhuanlan.zhihu.com/p/613068091 https://github.com/gshang2017/docker Relay and discovery server Docker images.