Skip to content

Instantly share code, notes, and snippets.

@Type1J
Created August 6, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Type1J/e1b6a7706c2f5ee841ce3bd6d4f6f456 to your computer and use it in GitHub Desktop.
Save Type1J/e1b6a7706c2f5ee841ce3bd6d4f6f456 to your computer and use it in GitHub Desktop.
Build (with Cargo) and run a statically linked (to run on Alpine Linux) web app written in Rust with Docker with no dependency rebuilds on further `docker build` commands. I hope this helps somebody.
FROM rust:1.45-alpine as builder
WORKDIR /usr/src/app
RUN apk add --no-cache musl-dev
RUN USER="builder" cargo init
COPY Cargo.toml .
COPY Cargo.lock .
RUN mkdir .cargo && cargo vendor > .cargo/config
RUN cargo build --release --color always 2>&1
COPY ./src src
RUN touch src/main.rs && cargo build --release --color always 2>&1
RUN cargo install --path . --color always 2>&1
FROM alpine:3.12 as runner
COPY --from=builder /usr/local/cargo/bin/your_web_app_exe_name /usr/local/bin/your_web_app_exe_name
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser
ENV PORT 8000
CMD ["your_web_app_exe_name"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment