Created
April 29, 2024 17:46
-
-
Save Pzixel/7d5d8dc034ee6c46db242f4a0d84fa3f to your computer and use it in GitHub Desktop.
Example rust dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# syntax=docker/dockerfile:1.6 | |
FROM debian:bullseye-slim AS base | |
RUN apt-get update && apt install -y pkg-config libssl-dev openssl ca-certificates | |
FROM rustlang/rust:nightly-bullseye-slim@sha256:e2d04393a78619a97bc8c9d19da15685d3df61d09b5036b0e00759c40c938afe as build | |
RUN apt-get update && apt install -y pkg-config libssl-dev openssl ca-certificates | |
WORKDIR /usr/src/app | |
RUN <<EOF | |
set -e | |
USER=root cargo new myapp --bin | |
USER=root cargo new myapp-core --lib | |
USER=root cargo new myapp-shared-types --lib | |
EOF | |
COPY ./Cargo.toml . | |
COPY ./Cargo.lock . | |
COPY ./myapp/Cargo.toml ./myapp/Cargo.toml | |
COPY ./myapp-core/Cargo.toml ./myapp-core/Cargo.toml | |
COPY ./myapp-shared-types/Cargo.toml ./myapp-shared-types/Cargo.toml | |
RUN RUSTFLAGS="-C target-cpu=x86-64-v3 -C target-feature=+adx,+aes,+avx,+avx2,+bmi1,+bmi2,+cmpxchg16b,+f16c,+fma,+fxsr,+gfni,+lzcnt,+movbe,+pclmulqdq,+popcnt,+rdrand,+rdseed,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+vaes,+vpclmulqdq,+xsave,+xsavec,+xsaveopt" cargo build --profile release-lto | |
COPY ./myapp ./myapp | |
COPY ./myapp-core ./myapp-core | |
COPY ./myapp-shared-types ./myapp-shared-types | |
# https://users.rust-lang.org/t/dockerfile-with-cached-dependencies-does-not-recompile-the-main-rs-file/21577 | |
RUN <<EOF | |
set -e | |
touch ./myapp/src/main.rs | |
touch ./myapp-core/src/lib.rs | |
touch ./myapp-shared-types/src/lib.rs | |
RUSTFLAGS="-C target-cpu=x86-64-v3 -C target-feature=+adx,+aes,+avx,+avx2,+bmi1,+bmi2,+cmpxchg16b,+f16c,+fma,+fxsr,+gfni,+lzcnt,+movbe,+pclmulqdq,+popcnt,+rdrand,+rdseed,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+vaes,+vpclmulqdq,+xsave,+xsavec,+xsaveopt" cargo build --profile release-lto | |
EOF | |
FROM base AS final | |
WORKDIR /app | |
COPY --from=build /usr/src/app/target/release-lto/myapp . | |
ENV RUST_BACKTRACE=full | |
ENTRYPOINT ["/app/myapp"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment