From ead9cd26589862b3d08c95a5d0cc1bd8389a3fdf Mon Sep 17 00:00:00 2001 From: Mikhail Isaev Date: Fri, 26 May 2023 23:42:23 +0300 Subject: [PATCH] Add Dockerfile and build instruction --- .dockerignore | 9 ++++++++ Dockerfile | 7 ++++++ README.md | 41 ++++++++++++++++++++++++++++++++++++ scripts/docker-entrypoint.sh | 41 ++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100755 .dockerignore create mode 100644 Dockerfile create mode 100755 scripts/docker-entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100755 index 0000000..85437c9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +__pycache__ +.mypy_cache +.pytest_cache +.vscode +cvdupdate.egg-info +/build +/dist +/tests +.github diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..07937c6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3-slim +RUN apt-get -y update \ + && apt-get -y --no-install-recommends install cron gosu \ + && rm -rf /var/lib/apt/lists/* +COPY . /dist +RUN pip install --no-cache-dir /dist +ENTRYPOINT [ "/dist/scripts/docker-entrypoint.sh" ] \ No newline at end of file diff --git a/README.md b/README.md index bce6f3b..b020bd5 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,47 @@ You can test it by running `freshclam` or `freshclam.exe` locally, where you've DatabaseMirror http://localhost:8000 ``` +### Use docker + +Build docker image + +```bash +docker build . --tag cvdupdate:latest +``` + +Run image, that will automaticly update databases in folder `/srv/cvdupdate` and write logs to `/var/log/cvdupdate` + +```bash +docker run -d \ + -v /srv/cvdupdate:/cvdupdate/database \ + -v /var/log/cvdupdate:/cvdupdate/logs \ + cvdupdate:latest +``` + +Run image, that will automaticly update databases in folder `/srv/cvdupdate`, write logs to `/var/log/cvdupdate` and set owner of files to user with ID 1000 + +```bash +docker run -d \ + -v /srv/cvdupdate:/cvdupdate/database \ + -v /var/log/cvdupdate:/cvdupdate/logs \ + -e USER_ID=1000 \ + cvdupdate:latest +``` + +Default update interval is `30 */4 * * *` (see [Cron Example](#cron-example)) + +You may pass custom update interval in environment variable `CRON` + +For example - update every day in 00:00 + +```bash +docker run -d \ + -v /srv/cvdupdate:/cvdupdate/database \ + -v /var/log/cvdupdate:/cvdupdate/logs \ + -e CRON='0 0 * * *' \ + cvdupdate:latest + ``` + ## Contribute We'd love your help. There are many ways to contribute! diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh new file mode 100755 index 0000000..544224d --- /dev/null +++ b/scripts/docker-entrypoint.sh @@ -0,0 +1,41 @@ +#!/bin/bash +USER_ID="${USER_ID:-0}" +SCRIPT_PATH=$(readlink -f "$0") +echo "ClamAV Private Database Mirror Updater Cron ${SCRIPT_PATH}" +if [ "${USER_ID}" -ne "0" ]; then + echo "Creating user with ID ${USER_ID}" + useradd --create-home --home-dir /cvdupdate --uid "${USER_ID}" cvdupdate + chown -R "${USER_ID}" /cvdupdate + gosu cvdupdate cvdupdate config set --logdir /cvdupdate/logs + gosu cvdupdate cvdupdate config set --dbdir /cvdupdate/database +else + mkdir -p /cvdupdate/{logs,database} + cvdupdate config set --logdir /cvdupdate/logs + cvdupdate config set --dbdir /cvdupdate/database +fi + +if [ $# -eq 0 ]; then + set -e + + echo "Adding crontab entry" + if [ "${USER_ID}" -ne "0" ]; then + crontab -l | { + cat + echo "${CRON:-"30 */4 * * *"} /usr/sbin/gosu cvdupdate /usr/local/bin/cvdupdate update >/proc/1/fd/1 2>/proc/1/fd/2" + echo "@reboot /usr/sbin/gosu cvdupdate /usr/local/bin/cvdupdate update >/proc/1/fd/1 2>/proc/1/fd/2" + } | crontab - + else + crontab -l | { + cat + echo "${CRON:-"30 */4 * * *"} /usr/local/bin/cvdupdate update >/proc/1/fd/1 2>/proc/1/fd/2" + echo "@reboot /usr/local/bin/cvdupdate update >/proc/1/fd/1 2>/proc/1/fd/2" + } | crontab - + fi + cron -f +else + if [ "${USER_ID}" -ne "0" ]; then + exec gosu cvdupdate "$@" + else + exec "$@" + fi +fi