# Stage 1: Build the application
FROM rust:1.65 as builder
# Set the working directory
WORKDIR /app
# Copy the manifests
COPY Cargo.toml Cargo.lock ./
# Create a new empty shell project to cache dependencies
RUN cargo new --bin myapp
WORKDIR /app/myapp
# Copy the source code
COPY src ./src
# Build the project in release mode
RUN cargo build --release
# Stage 2: Create the runtime image
FROM debian:buster-slim
# Install necessary packages
RUN apt-get update && apt-get install -y \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Copy the compiled binary from the builder stage
COPY --from=builder /app/myapp/target/release/myapp /usr/local/bin/myapp
# Expose the port specified by the environment variable
ENV PORT 8080
EXPOSE ${PORT}
# Set the binary as the entry point
CMD ["myapp"]