# Multi-stage Dockerfile to build and run the micro_traffic_sim gRPC server
#
# Build:
# docker build -f Dockerfile.server -t micro-traffic-sim/server:latest .
#
# Run:
# docker run --rm -it --name micro-traffic-sim-server -p 50051:50051 micro-traffic-sim/server:latest
#
# Docker Hub
# docker tag micro-traffic-sim/server:latest dimahkiin/micro-traffic-sim-server:0.0.1
# docker tag micro-traffic-sim/server:latest dimahkiin/micro-traffic-sim-server:latest
# docker push dimahkiin/micro-traffic-sim-server:0.0.1
# docker push dimahkiin/micro-traffic-sim-server:latest

# GitHub Container Registry
# docker tag micro-traffic-sim/server:latest ghcr.io/lddl/micro-traffic-sim-server:0.0.1
# docker tag micro-traffic-sim/server:latest ghcr.io/lddl/micro-traffic-sim-server:latest
# docker push ghcr.io/lddl/micro-traffic-sim-server:0.0.1
# docker push ghcr.io/lddl/micro-traffic-sim-server:latest

# Builder
FROM rust:1.91-alpine AS builder
WORKDIR /app

# Install build dependencies
RUN apk add --no-cache musl-dev protobuf-dev tini-static

# Cache dependency build
COPY Cargo.toml Cargo.lock ./
COPY src/build.rs ./src/build.rs
RUN mkdir -p src && echo "fn main(){}" > src/lib.rs && \
    cargo fetch && \
    cargo build --release --features server || true

# Copy sources and build
COPY src ./src
COPY protos ./protos
RUN cargo build --release --features server && \
    strip target/release/micro_traffic_sim

# Runtime
FROM scratch
LABEL org.opencontainers.image.source="https://github.com/LdDl/micro_traffic_sim_grpc"
LABEL org.opencontainers.image.description="Microscopic traffic simulation gRPC server"
LABEL org.opencontainers.image.licenses="Apache-2.0"
COPY --from=builder /sbin/tini-static /tini
COPY --from=builder /app/target/release/micro_traffic_sim /micro_traffic_sim
EXPOSE 50051
ENTRYPOINT ["/tini", "--", "/micro_traffic_sim"]
