# Kymoscope collector (ignore rules: Dockerfile.dockerignore). Build context is the repo root:
#   docker build -f packages/collector/Dockerfile -t ghcr.io/javijur/kymoscope-collector .
#   docker run -d --restart unless-stopped -v /path/to/site:/data ghcr.io/javijur/kymoscope-collector
# The container runs as uid 10001: a bind-mounted host folder must be writable by it (sudo chown -R 10001:10001 /srv/kymoscope).
# /data holds collector.yaml, tags.csv and the collector's state (set state_dir: /data/state).
FROM python:3.12-slim
LABEL org.opencontainers.image.source="https://github.com/Javijur/kymoscope" \
      org.opencontainers.image.title="Kymoscope collector" \
      org.opencontainers.image.description="Site collector: OPC UA, logger files and custom sources to end-to-end encrypted Kymoscope streams."
COPY packages/python /src/python
COPY packages/collector /src/collector
# gcc only while installing: some readers (asammdf's zstd) ship no arm64 wheel
RUN apt-get update && apt-get install -y --no-install-recommends gcc libc6-dev  && pip install --no-cache-dir /src/python "/src/collector[files]"  && apt-get purge -y gcc libc6-dev && apt-get autoremove -y && rm -rf /var/lib/apt/lists/* /src
RUN useradd --create-home --uid 10001 collector && mkdir -p /data && chown collector /data
RUN printf '%s\n' '#!/bin/sh' \
    '[ -w /data ] || { echo "kymoscope-collector: /data is not writable by uid $(id -u). On the host run: sudo chown -R 10001:10001 <the folder mounted at /data> (or start the container with --user)." >&2; exit 1; }' \
    'exec kymoscope-collector "$@"' > /usr/local/bin/collector-entrypoint && chmod +x /usr/local/bin/collector-entrypoint
USER collector
WORKDIR /data
VOLUME /data
ENTRYPOINT ["collector-entrypoint"]
CMD ["run", "--config", "/data/collector.yaml"]
