forked from kartevonmorgen/openfairdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
100 lines (76 loc) · 2.76 KB
/
Dockerfile
File metadata and controls
100 lines (76 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Dockerfile for creating a statically-linked Rust application using Docker's
# multi-stage build feature. This also leverages the docker build cache to
# avoid re-downloading dependencies if they have not changed between builds.
###############################################################################
# Define global ARGs for all stages
ARG WORKDIR_ROOT=/usr/src
ARG PROJECT_NAME=openfairdb
ARG BUILD_TARGET=x86_64-unknown-linux-musl
ARG BUILD_MODE=release
ARG BUILD_BIN=${PROJECT_NAME}
###############################################################################
# 1st Build Stage
# The tag of the base image must match the version in the file rust-toolchain!
FROM clux/muslrust:nightly-2019-07-19 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} --all \
&& \
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
COPY [ \
"./migrations", \
"./migrations/" ]
COPY [ \
"./src", \
"./src/" ]
COPY [ \
"./openapi.yaml", \
"./" ]
# Test and build the actual project
# The feature "email" is not available in the image, because it requires
# a local sendmail installation.
RUN cargo test --${BUILD_MODE} --target ${BUILD_TARGET} --all \
&& \
cargo build --${BUILD_MODE} --target ${BUILD_TARGET} --no-default-features --features frontend --bin ${BUILD_BIN} \
&& \
strip ./target/${BUILD_TARGET}/${BUILD_MODE}/${BUILD_BIN}
###############################################################################
# 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 ROCKET_PORT ${EXPOSE_PORT}
ENTRYPOINT [ "./entrypoint" ]