# syntax=docker/dockerfile:1
# Build:  docker build --ssh default -f crates/voelin-gateway/Dockerfile -t tsgw .
#         (--ssh default hands the build your ssh-agent, which must hold a key that
#         can read the private VoelinProtocol repository: docs/building.md,
#         "Depending on VoelinProtocol". The gateway itself does not use it, but
#         Cargo resolves the whole workspace, which includes voelin-identity.)
# Run:    docker run -d --name tsgw --init --restart unless-stopped \
#           -v ./tsgw.toml:/etc/tsgw/tsgw.toml:ro -v tsgw-data:/var/lib/tsgw \
#           -e TSGW_QUERY_PASSWORD=… -p 7788:7788 tsgw
# Logs:   docker logs -f tsgw (more: -e TSGW_LOG_LEVEL=debug; also to a file in
#         the volume: -e TSGW_LOG_FILE=logs/tsgw.log). See docs/gateway-admin.md.
FROM rust:1.94-bookworm AS build
WORKDIR /src
RUN apt-get update && apt-get install -y --no-install-recommends cmake git libasound2-dev openssh-client && rm -rf /var/lib/apt/lists/*
# voelin-identity comes from the private VoelinProtocol repository, fetched
# over SSH by Cargo through the system git, with the agent that
# `docker build --ssh default` forwards (scripts/docker-build.sh passes it):
# the key stays in the agent and never reaches a layer. The host key is the
# reviewed line from .gitea/known_hosts. Cargo resolves the whole workspace, so
# every cargo command needs this, although this crate does not use
# voelin-identity.
COPY .gitea/known_hosts /etc/ssh/ssh_known_hosts
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
COPY . .
RUN --mount=type=ssh,required=true \
	cargo build --release --locked -p voelin-gateway && cp target/release/tsgw /tsgw && mkdir /data

FROM gcr.io/distroless/cc-debian12:nonroot
COPY --from=build /tsgw /usr/local/bin/tsgw
# The database (and a log file, if set) live here; owned by the image's user
# (nonroot, 65532), so a new volume mounted here is writable too.
COPY --from=build --chown=65532:65532 /data /var/lib/tsgw
ENV TSGW_CONFIG=/etc/tsgw/tsgw.toml
WORKDIR /var/lib/tsgw
EXPOSE 7788
ENTRYPOINT ["/usr/local/bin/tsgw"]
