build: make project deployable via docker/podman
This commit is contained in:
parent
2ae860f176
commit
9dde52c1cd
7 changed files with 102 additions and 109 deletions
102
Dockerfile
102
Dockerfile
|
|
@ -1,94 +1,8 @@
|
|||
# Global ARGs
|
||||
|
||||
ARG WORKDIR_ROOT=/usr/src
|
||||
|
||||
ARG PROJECT_NAME=learn_axum
|
||||
|
||||
ARG BUILD_TARGET=x86_64-unknown-linux-musl
|
||||
|
||||
ARG BUILD_MODE=release
|
||||
|
||||
ARG BUILD_BIN=${PROJECT_NAME}
|
||||
|
||||
###############################################################################
|
||||
|
||||
FROM clux/muslrust:stable AS build
|
||||
|
||||
# Import global ARGs
|
||||
ARG WORKDIR_ROOT
|
||||
ARG PROJECT_NAME
|
||||
ARG BUILD_TARGET
|
||||
ARG BUILD_MODE
|
||||
ARG BUILD_BIN
|
||||
|
||||
WORKDIR ${WORKDIR_ROOT}
|
||||
|
||||
# Docker build cache: Create and build an empty dummy project with all
|
||||
# external dependencies to avoid redownloading them on subsequent builds
|
||||
# if unchanged.
|
||||
RUN USER=root \
|
||||
cargo new --bin ${PROJECT_NAME}
|
||||
WORKDIR ${WORKDIR_ROOT}/${PROJECT_NAME}
|
||||
|
||||
COPY [ \
|
||||
"Cargo.toml", \
|
||||
"Cargo.lock", \
|
||||
"./" ]
|
||||
|
||||
# Build the dummy project(s), then delete all build artefacts that must(!) not be cached
|
||||
# RUN cargo build --${BUILD_MODE} --target ${BUILD_TARGET}
|
||||
#\
|
||||
# && \
|
||||
# rm -f ./target/${BUILD_TARGET}/${BUILD_MODE}/${PROJECT_NAME}* \
|
||||
# && \
|
||||
# rm -f ./target/${BUILD_TARGET}/${BUILD_MODE}/deps/${PROJECT_NAME}-* \
|
||||
# && \
|
||||
# rm -rf ./target/${BUILD_TARGET}/${BUILD_MODE}/.fingerprint/${PROJECT_NAME}-*
|
||||
|
||||
# Copy all project (re-)sources that are required for building (ordered alphabetically)
|
||||
COPY [ "migrations", "./migrations/" ]
|
||||
COPY [ "src", "./src/" ]
|
||||
|
||||
# Test and build the actual project
|
||||
RUN cargo test --${BUILD_MODE} --target ${BUILD_TARGET} --workspace \
|
||||
&& \
|
||||
cargo build --${BUILD_MODE} --target ${BUILD_TARGET} --bin ${BUILD_BIN} \
|
||||
&& \
|
||||
strip ./target/${BUILD_TARGET}/${BUILD_MODE}/${BUILD_BIN}
|
||||
|
||||
# Switch back to the root directory
|
||||
#
|
||||
# NOTE(2019-08-30, uklotzde): Otherwise copying from the build image fails
|
||||
# during all subsequent builds of the 2nd stage with an unchanged 1st stage
|
||||
# image. Tested with podman 1.5.x on Fedora 30.
|
||||
WORKDIR /
|
||||
|
||||
|
||||
###############################################################################
|
||||
# 2nd Build Stage
|
||||
FROM scratch
|
||||
|
||||
# Import global ARGs
|
||||
ARG WORKDIR_ROOT
|
||||
ARG PROJECT_NAME
|
||||
ARG BUILD_TARGET
|
||||
ARG BUILD_MODE
|
||||
ARG BUILD_BIN
|
||||
|
||||
ARG DATA_VOLUME="/volume"
|
||||
|
||||
ARG EXPOSE_PORT=8080
|
||||
|
||||
# Copy the statically-linked executable into the minimal scratch image
|
||||
COPY --from=build [ \
|
||||
"${WORKDIR_ROOT}/${PROJECT_NAME}/target/${BUILD_TARGET}/${BUILD_MODE}/${BUILD_BIN}", \
|
||||
"./entrypoint" ]
|
||||
|
||||
EXPOSE ${EXPOSE_PORT}
|
||||
|
||||
VOLUME [ ${DATA_VOLUME} ]
|
||||
|
||||
# Bind the exposed port to Rocket that is used as the web framework
|
||||
ENV SERVER_PORT ${EXPOSE_PORT}
|
||||
|
||||
ENTRYPOINT [ "./entrypoint" ]
|
||||
FROM docker.io/rust:1.75.0
|
||||
WORKDIR /app
|
||||
RUN apt update && apt install lld clang -y
|
||||
COPY . .
|
||||
ENV SQLX_OFFLINE true
|
||||
RUN cargo build --release
|
||||
ENV APP_ENVIRONMENT production
|
||||
ENTRYPOINT ["./target/release/learn_axum"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue