Skip to content

Commit 6dbc56a

Browse files
authored
dev: faster docker builds (#222)
- [x] cache build dependencies in Dockerfile so we don't have to pull and compile them every time - [x] better `.dockerignore` - [x] Docker Buildkit doesn't seem supported on google cloud build, so removed that option - [x] use a `E2_HIGHCPU_8` machine type to build the images, which should compile things faster
1 parent 9d634cb commit 6dbc56a

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

.dockerignore

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
/target
2-
.git
1+
target/
2+
.git/
3+
.gitignore
4+
.env
5+
*.md
6+
!README.md

Dockerfile

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
11
FROM rust:1.80-alpine3.20 as builder
22

3-
ARG UPTIME_CHECKER_GIT_REVISION
4-
ENV UPTIME_CHECKER_GIT_REVISION=$UPTIME_CHECKER_GIT_REVISION
5-
3+
# Install system dependencies
4+
RUN apk add --no-cache \
5+
libc-dev \
6+
cmake \
7+
make \
8+
g++ \
9+
pkgconfig \
10+
openssl-dev
11+
12+
# Configure cargo
613
RUN mkdir -p ~/.cargo && \
714
echo '[registries.crates-io]' > ~/.cargo/config && \
815
echo 'protocol = "sparse"' >> ~/.cargo/config
916

10-
RUN apk add --no-cache libc-dev cmake make g++
11-
RUN apk add --no-cache pkgconfig openssl-dev
12-
13-
RUN cargo new --bin /app
1417
WORKDIR /app
1518

16-
# Just copy the Cargo.toml files and trigger a build so that we compile our
17-
# dependencies only. This way we avoid layer cache invalidation if our
18-
# dependencies haven't changed, resulting in faster builds.
19+
# Copy only the files needed for dependency caching
1920
COPY Cargo.toml Cargo.lock ./
2021
COPY redis-test-macro redis-test-macro/
2122

23+
# Create a dummy main.rs to build dependencies
24+
RUN mkdir src && \
25+
echo "fn main() {}" > src/main.rs && \
26+
# Build dependencies only
27+
export RUSTFLAGS="-Ctarget-feature=-crt-static" && \
28+
export PKG_CONFIG_ALLOW_CROSS=1 && \
29+
cargo build --release && \
30+
rm -rf src/
31+
32+
# Set environment variables for the final build
2233
ENV RUSTFLAGS="-Ctarget-feature=-crt-static"
2334
ENV PKG_CONFIG_ALLOW_CROSS=1
24-
RUN cargo build --release && rm -rf src/
35+
ARG UPTIME_CHECKER_GIT_REVISION
36+
ENV UPTIME_CHECKER_GIT_REVISION=$UPTIME_CHECKER_GIT_REVISION
2537

26-
# Copy the source code and run the build again. This should only compile the
27-
# app itself as the dependencies were already built above.
28-
COPY . ./
29-
RUN rm target/release/deps/uptime_checker* && cargo build --release
38+
# Copy the actual source code and build
39+
COPY . .
40+
RUN cargo build --release
3041

3142
FROM alpine:3.20
3243

3344
COPY --from=builder /app/target/release/uptime-checker /usr/local/bin/uptime-checker
3445

35-
RUN apk add --no-cache tini libgcc
46+
RUN apk add --no-cache tini libgcc && \
47+
addgroup -S app && \
48+
adduser -S app -G app
3649

37-
RUN addgroup -S app && adduser -S app -G app
3850
USER app
3951

4052
ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/uptime-checker"]

cloudbuild.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ steps:
99
'us-central1-docker.pkg.dev/$PROJECT_ID/uptime-checker/image:$COMMIT_SHA',
1010
'--build-arg',
1111
'UPTIME_CHECKER_GIT_REVISION=$COMMIT_SHA',
12-
'--build-arg',
13-
'BUILDKIT_INLINE_CACHE=1',
1412
'--cache-from',
1513
'us-central1-docker.pkg.dev/$PROJECT_ID/uptime-checker/image:latest',
1614
'.',
1715
]
18-
env: [DOCKER_BUILDKIT=1]
1916

2017
- name: 'gcr.io/cloud-builders/docker'
2118
entrypoint: 'bash'
@@ -29,3 +26,6 @@ steps:
2926
images: [
3027
'us-central1-docker.pkg.dev/$PROJECT_ID/uptime-checker/image:$COMMIT_SHA',
3128
]
29+
30+
options:
31+
machineType: 'E2_HIGHCPU_8'

0 commit comments

Comments
 (0)