-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
67 lines (48 loc) · 2.43 KB
/
Dockerfile
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
######################################-Variables-##########################################
# version can be latest or specific like 1.78.0-slim
ARG RUST_VERSION="1.78.0-slim"
ARG APP_NAME="rustful"
ARG PORT=5001
######################################-Chef-##########################################
# We only pay the installation cost once,
# it will be cached from the second build onwards
FROM rust:${RUST_VERSION} as chef
WORKDIR /app
RUN apt update && apt upgrade --no-install-recommends -y && apt install --no-install-recommends pkg-config libssl-dev libpq-dev -y
# Install diesel CLI for migration
RUN cargo install diesel_cli --no-default-features --features postgres
# Install cargo-chef for Docker layer caching
RUN cargo install cargo-chef
######################################-Planner-##########################################
FROM chef as planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
######################################-Cacher-##########################################
# Build the application. Leverage a cache to which will speed up subsequent builds.
FROM chef as builder
COPY --from=planner /app/recipe.json recipe.json
# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release
######################################-Migration-##########################################
# If your service have db connection and required to perform a db migration then
# use this section, otherwise coment it out.
FROM builder as migration
CMD ["diesel", "migration", "run"]
######################################-Application-##########################################
# Create a new stage for running the application that contains the minimal
# runtime dependencies for the application. This often uses a different base
# image from the build stage where the necessary files are copied from the build
# stage. We do not need the Rust toolchain to run the binary!
FROM debian:stable-slim as final
WORKDIR /app
RUN apt update && apt upgrade --no-install-recommends -y && apt install libpq-dev -y
COPY --from=builder /app/.env /app/.env
COPY --from=builder /app/target/release/${APP_NAME} /app/${APP_NAME}
# Expose the port that the application listens on.
EXPOSE ${PORT}
# What the container should run when it is started.
CMD ["./rustful"]
#------------------------------------------End-----------------------------------------------