Skip to content

Commit

Permalink
Optimize docker build via caching the dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielCosme committed Feb 18, 2024
1 parent cc267ce commit f60bcf7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.env
target/
tests/
Dockerfile
scripts/
migrations/
29 changes: 26 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
FROM rust:1.72.0

FROM lukemathwalker/cargo-chef:latest-rust-1.72.0 as chef
WORKDIR /app
RUN apt update && apt install lld clang -y


FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json


FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
ENV SQLX_OFFLINE true
RUN cargo build --release


FROM debian:bookworm-slim AS runtime
WORKDIR /app

RUN apt-get update -y \
&& apt-get install -y --no-install-recommends openssl ca-certificates \
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/target/release/curious_ape curious_ape
COPY configuration configuration
ENV APP_ENVIRONMENT production
ENTRYPOINT ["./target/release/curious_ape"]
ENTRYPOINT ["./curious_ape"]
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ async fn main() -> Result<(), std::io::Error> {
let cfg = get_configuration().expect("Failed to read configuration");

// Setup Database Connection.
let conn_pool =
PgPool::connect_lazy(cfg.database.connection_string().expose_secret())
.expect("Failed to connect to Postgres.");
let conn_pool = PgPool::connect_lazy(cfg.database.connection_string().expose_secret())
.expect("Failed to connect to Postgres.");

// Setup TCP connection.
let addr = format!("{}:{}", cfg.application.host, cfg.application.port);
Expand Down

0 comments on commit f60bcf7

Please sign in to comment.