DEV Community

Stephan Dilly
Stephan Dilly

Posted on

Speedup Docker Build

So I just learned about the .dockerignore file after realising how uploading my context to the docker deamon got 1.5 GB big and took forever.

Look at the ref docs here:

But the kicker was to find out how to use it as a whitelist and ignore everything but the stuff u need docker to put in your container:

# Ignore everything

# only add prebuild binary
Enter fullscreen mode Exit fullscreen mode

This turns out to be a lifesaver bin node/rust projects where the working directory contains a giant pile of generated/compiled junk!

Discussion (1)

5422m4n profile image
Sven Assmann

Yeah that is very crutial. There is yet another trick when building rust docker images, to keep the build time low, and take full advantage of the docker build cache. That is build a project from scratch, copy only over you Cargo.{lock,toml}, run a build.
That trick will cause a pull and build of all dependencies, so that docker can cache that, and next time when you have changes only on your code, it will skip those steps because at this time you've only copied over the Cargo files and no code at all. See a rough draft below:

FROM rust:latest as builder

RUN apt-get update \
    && apt-get install -y --no-install-recommends \

RUN rustup target add x86_64-unknown-linux-musl
RUN rustup component add clippy

FROM builder as server_builder

WORKDIR /graphql

## doing some overhead here to keep docker build time low
# create a new empty shell project
RUN USER=root cargo new --bin server
WORKDIR /graphql/server

# copy over your manifests
COPY ./server/Cargo.lock ./
COPY ./server/Cargo.toml ./

# this build step will cache dependencies
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build --target x86_64-unknown-linux-musl --release
RUN rm src/*.rs
## done with the overhead

# skip all other waste and copy the code now
COPY ./server/src ./src
RUN rm -f ./target/release/deps/server*
RUN RUSTFLAGS=-Clinker=musl-gcc \
    cargo clippy --target x86_64-unknown-linux-musl --all-features -- -D warnings && \
    cargo test --target x86_64-unknown-linux-musl --verbose && \
    cargo build --target x86_64-unknown-linux-musl --release --verbose

# further things to be done..