Skip to content

Commit

Permalink
chore: Speed up docker builds
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Nov 17, 2023
1 parent 1ac5c3a commit b77a7cc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
84 changes: 62 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
#
# Base node image with project dependencies
# Base node image with just the build tools
#
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
FROM node:18.16.0 as dependencies
FROM node:18.16.0 as base

ARG NPM_TOKEN
ENV YARN_CACHE_FOLDER=/tmp/yarn_cache

# Update the system packages
Expand All @@ -18,16 +17,56 @@ RUN apt-get install -y \
# Get the envsubst command (see below)
gettext-base

# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
#
# Image with pruned source code
#
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
FROM base as builder

# The name of the package we're building here
#
# e.g. @layerzerolabs/ua-utils
ARG PACKAGE

WORKDIR /app

# Get the source code
# We'll only use this turbo to prune the workspace, we don't care what version we use here
RUN yarn global add turbo

COPY . .

# We'll use turbo prune to remove the unneeded packages
# and separate the package.json / yarn.lock from the source code
#
# Oh god why yarn, paleolithic technology
# This allows us to cache the yarn install step
#
# This would normally only copy package.json and/or lockfile
# and install the dependencies from lockfile in order not to break the cache
# everytime a file changes
COPY . .
# See more here https://turbo.build/repo/docs/reference/command-line-reference/prune
# And here https://turbo.build/repo/docs/handbook/deploying-with-docker
RUN turbo prune $PACKAGE --docker

# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
#
# Image with all dependencies installed
#
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
FROM base as dependencies

ARG NPM_TOKEN

WORKDIR /app

# In this step we'll only use the package.json / yarn.lock files generated by turbo prune
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock

# Get the .npmrc under a different name since envsubst will not work in place
# due to how pipes work on linux
Expand All @@ -53,31 +92,32 @@ RUN \
NPM_TOKEN=$NPM_TOKEN envsubst < .npmrctemplate > .npmrc && \
# Install dependencies (fail if we forgot to update the lockfile)
yarn install --prefer-offline --frozen-lockfile --non-interactive && \
# Remove .npmrc immediately
rm -rf .npmrc
# Remove .npmrc/.npmrctemplate immediately
rm -rf .npmrc .npmrctemplate

# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
#
# Image that builds the project
# Image that builds the package
#
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
FROM dependencies as build

# Let's now add some fancy shit to compensate for the yarn fail above
# The name of the package we're building here
#
# Turborepo allows us to be specific about the scope of the scripts
# using the --filter flag - see more here https://turbo.build/repo/docs/core-concepts/monorepos/filtering
ARG FILTER
# e.g. @layerzerolabs/ua-utils
ARG PACKAGE

WORKDIR /app

# Since our FILTER arg can be empty, we only want to pass the --filter
# flag to turborepo if FILTER is actually used
#
# This is nicely accomplished by the + alternate value substitution
# and results in something like --filter=my-filter
RUN yarn build ${FILTER:+--filter=$FILTER}
# For some reason we're missing tsconfig.json when using turbo prune
COPY tsconfig.json ./

# Now we grab the full source code from the builder step
COPY --from=builder /app/out/full/ .

# And finally we build the package
RUN yarn build --filter=$PACKAGE...
6 changes: 5 additions & 1 deletion docker-compose.templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
version: "3.9"

services:
# Service that can build the whole project
# Service that contains the whole project
#
# It requires the PACKAGE argument that specifies
# the target package it should build (e.g. @layerzerolabs/hardhat-utils)
project:
build:
context: .
args:
PACKAGE:
NPM_TOKEN: $NPM_TOKEN
2 changes: 1 addition & 1 deletion packages/ua-utils-test-v2/docker-compose.templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
package:
build:
args:
FILTER: ua-utils-test-v2...
PACKAGE: "@layerzerolabs/ua-utils-test-v2"
extends:
file: ../../docker-compose.templates.yaml
service: project
Expand Down

0 comments on commit b77a7cc

Please sign in to comment.