# All-in-one image for running irvis-go locally: MySQL + the compiled Go app,
# optionally fronted by nginx. Source code is NOT copied in here - the
# entrypoint clones it fresh from git every time the container starts.
FROM golang:1.24-bookworm

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
    default-mysql-server \
    default-mysql-client \
    nginx \
    git \
    openssh-client \
    tini \
    curl \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Trust GitHub's host keys up front so `git clone` over SSH doesn't prompt.
RUN mkdir -p /etc/ssh && ssh-keyscan -H github.com >> /etc/ssh/ssh_known_hosts 2>/dev/null

RUN mkdir -p /var/lib/mysql /var/run/mysqld /opt/irvis-src \
    && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld

COPY entrypoint.sh /entrypoint.sh
COPY nginx/irvis.conf.template /etc/nginx/templates/irvis.conf.template
COPY mysql-init/schema.sql /opt/irvis-docker/schema.sql
RUN chmod +x /entrypoint.sh

# App port (local mode) / nginx port (web mode). Mapped to a host port chosen
# by scripts/run.sh (8080 if free, otherwise the next free port).
EXPOSE 8080

VOLUME ["/var/lib/mysql"]

HEALTHCHECK --interval=5s --timeout=3s --start-period=90s --retries=20 \
    CMD curl -fs "http://127.0.0.1:8080/healthz" || exit 1

ENTRYPOINT ["/usr/bin/tini", "--", "/entrypoint.sh"]
