vmstan / gravity-sync

πŸ’« The easy way to synchronize the DNS configuration of two Pi-hole 5.x instances.
https://vmstan.com/gravity-sync/
GNU General Public License v3.0
2.29k stars 128 forks source link

Run gravity-sync in a docker container #345

Closed niawag closed 2 months ago

niawag commented 2 years ago

Hi, gravity-sync seems to be exactly what I need but I'm trying to run it on a CoreElec install and the install script is failing (no sudo). Being able to run gravity-sync in a docker container could be great to help users (me included) on weird systems. I'm used to docker as a user but not as an image creator, anyway I've started to create a Dockerfile to build my own image and I think I might succeed but I've also seen that a Dockerfile is available in this repo.

Are you planning to create a docker image and offer it as one of the installation system ? If so I'd be glad to help you develop it and/or test it. If not I will keep trying on my side and "offer" my Dockerfile here so other can do the same.

vmstan commented 2 years ago

Yes I intended to build a Docker version, but I've just not had time the last couple weeks. Happy to take contributions.

niawag commented 2 years ago

Great! I'll report my progress here and I may ask for your help!

niawag commented 2 years ago

So, I've been making progress on this, here is my Dockerfile:

FROM debian

MAINTAINER niawag

RUN echo "nameserver 8.8.8.8" > /etc/resolv.conf \
    && echo "search companydomain" >> /etc/resolv.conf \
    && apt-get update \
    && apt-get install -y systemctl git rsync curl sudo \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN echo "nameserver 8.8.8.8" > /etc/resolv.conf \
    && echo "search companydomain" >> /etc/resolv.conf \
    && curl -sSL https://gravity.vmstan.com | GS_DOCKER=1 bash

CMD tail -f /dev/null

There are lines related to dns (first 2 lines of RUN cmd) that may not be necessary on other system or if built by githib. The CMD command is to keep the container running

Here is the config file:

# REQUIRED SETTINGS ##########################

REMOTE_HOST='192.168.0.44'
REMOTE_USER='pi'

# CUSTOM VARIABLES ###########################

# Pi-hole Folder/File Customization - Only need to be customized when using containers
LOCAL_PIHOLE_DIRECTORY='/etc/pihole'                        # Local Pi-hole data directory
REMOTE_PIHOLE_DIRECTORY='/home/pi/dockers/pihole/pihole'                            # Remote Pi-hole data directory
LOCAL_DNSMASQ_DIRECTORY='/etc/dnsmasq.d/'                # Local DNSMASQ/FTL data directory
REMOTE_DNSMASQ_DIRECTORY='/home/pi/dockers/pihole/etc-dnsmasq.d'               # Remote DNSMASQ/FTL data directory
LOCAL_FILE_OWNER='999:999'                       # Local file owner for Pi-hole
REMOTE_FILE_OWNER='999:999'                      # Remote file owner for Pi-hole

# Pi-hole Docker/Podman container name - Docker will pattern match anything set below
# LOCAL_DOCKER_CONTAINER=''                                 # Local Pi-hole container name
# REMOTE_DOCKER_CONTAINER=''                            # Remote Pi-hole container name

# HIDDEN FIGURES #############################
# See https://github.com/vmstan/gravity-sync/wiki/Hidden-Figures

And the docker-compose.yml file:

services:
  gravitysync:
    build:
      context: /storage/gravity/
    container_name: "gravitysync"
    restart: "unless-stopped"
    volumes:
      - "/storage/dockers/gravity-sync/gravity-sync.conf:/etc/gravity-sync/gravity-sync.conf:rw"
      - "/storage/dockers/gravity-sync/gravity-sync.rsa:/etc/gravity-sync/gravity-sync.rsa:rw"
      - "/storage/dockers/pihole/pihole:/etc/pihole/:rw"
      - "/storage/dockers/pihole/etc-dnsmasq.d:/etc/dnsmasq.d/:rw"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

When running gravity-sync info from the container I'm getting:

root@899faae51831:/# /etc/gravity-sync/.gs/gravity-sync info
∞ Initializing Gravity Sync (4.0.4)
βœ“ Loading gravity-sync.conf
βœ— Detecting local Pi-hole installation
βœ“ Detecting remote Pi-hole installation
βœ“ Gravity Sync falling back to peerless mode
! Please configure Gravity Sync on remote host
βœ“ Evaluating arguments: INFO
Local Software Versions
Pi-hole
Debian GNU/Linux 11
Linux 4.9.269 aarch64
bash 5.1.4(1)-release
OpenSSH_8.4p1 Debian-5, OpenSSL 1.1.1n  15 Mar 2022
rsync  version 3.2.3  protocol version 31
Sudo version 1.9.5p2
git version 2.30.2

Global Instance Settings
SSH Port: 22 (default)
SSH Key: /etc/gravity-sync/gravity-sync.rsa
ERROR:systemctl:Unit gravity-sync.timer could not be found.
Automated Replication: Disabled

Local Instance Settings
Local Hostname: 899faae51831
Local Pi-hole Type: none
Local Pi-hole Config Directory: /etc/pihole
Local DNSMASQ Config Directory: /etc/dnsmasq.d/
Local Gravity Sync Binary: /usr/local/bin/gravity-sync
Local Gravity Sync Config Directory: /etc/gravity-sync
Local File Owner Settings: 999:999

Remote Instance Settings
Remote Hostname/IP: 192.168.0.44
Remote Username: pi
Remote Pi-hole Type: docker
Remote Pi-hole Config Directory: /home/pi/dockers/pihole/pihole
Remote DNSMASQ Config Directory: /home/pi/dockers/pihole/etc-dnsmasq.d
Remote Pi-hole Container Name: pihole
Remote Docker Binary Directory: /usr/bin/docker
Remote File Owner Settings: 999:999
∞ Gravity Sync INFO exited after 2 seconds

So almost everything seems ok, I'll get back to the local pihole detection later. And here is when I'm trying a push:

root@899faae51831:/# /etc/gravity-sync/.gs/gravity-sync push
∞ Initializing Gravity Sync (4.0.4)
βœ“ Loading gravity-sync.conf
βœ— Detecting local Pi-hole installation
βœ“ Detecting remote Pi-hole installation
βœ“ Gravity Sync falling back to peerless mode
! Please configure Gravity Sync on remote host
βœ“ Evaluating arguments: PUSH
Β» Remote target pi@192.168.0.44
βœ“ Validating pathways to Pi-hole
βœ“ Validating pathways to DNSMASQ
βœ“ Hashing the remote Gravity Database
βœ“ Comparing to the local Gravity Database
! Differences detected in the Gravity Database
βœ“ Hashing the remote DNS Records
βœ“ Comparing to the local DNS Records
! Differences detected in the DNS Records
βœ“ Hashing the remote DNS CNAMEs
βœ“ Comparing to the local DNS CNAMEs
! Differences detected in the DNS CNAMEs
βœ“ Hashing the remote Static DHCP Addresses
βœ“ Comparing to the local Static DHCP Addresses
! Differences detected in the Static DHCP Addresses
! Replication of Pi-hole settings is required
βœ“ Performing backup of remote Gravity Database
βœ— Integrity check has failed for the remote Gravity Databaseity Database copy integrity
∞ Removing failed copies Gravity Databaserm: cannot remove '/etc/pihole/gravity.db.gsb': No such file or directory
βœ— Removing failed copies Gravity Database

So it seems to be failing when creating the backup, it may be a problem with rights but I'm not sure. About βœ— Detecting local Pi-hole installation, I've checked the code and it's normal that it fails as the docker command won't work inside the container. Is there a way to improve this ?

DKeppi commented 2 years ago

There is already a Docker Image available -> https://hub.docker.com/r/nhmike94/gravity-sync-docker

vmstan commented 2 years ago

It hasn't been updated for 4.x and I haven't seen @nh-mike around lately.

DKeppi commented 2 years ago

Thatβ€˜s why i opened an issue today -> https://github.com/nh-mike/gravity-sync-docker/issues/9 ;)

nh-mike commented 2 years ago

Hi all Apologies I've been travelling a lot for work lately to places where internet is little more than a memory and have been unable to maintain the project lately.

I'll get back to you all in a few days. I know there's been a lot of large changes I will need to accommodate for.

Cheers.

karan commented 2 years ago

To add to this, it would be great to publish a docker container for use with unRAID: https://forums.unraid.net/topic/87144-ca-application-policies-notes/

vmstan commented 2 months ago

Effective July 26, 2024, this project has been retired. Thank you for your use and enthusiasm for a project that began as a few lines of bash in a Slack channel and envolved into something far more complex, and used by many thousands of Pi-hole enthusiasts over the last four years.