To deploy a Rust web server with Docker, build a release binary, create a minimal Dockerfile using the rust image to compile and alpine to run, then build and run the container.
- Build the release binary with
cargo build --release. - Create a
Dockerfilewith the following content:
FROM rust:1.90 as builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/target/release/my-server .
CMD ["./my-server"]
- Build the Docker image with
docker build -t my-rust-server .. - Run the container with
docker run -p 8080:8080 my-rust-server.