npm install step of the dockerfile seems to run each time, and takes a lot of time. even though package.json or lockfile remains unmodified.
I want to eventually move the dockerfile into a full dagger config soon. But just to get builds going first I'm using the Dockerfile still. Great that you added an dockerBuild() easy transitioning option 👏
Would you please also comment if I'm setting up dagger properly? I have several things that I'm not sure of. For example how to port .dockerignore files, how to utilize caching properly. should I mount the cache, where does the cache live, how does it get invalidated.
One gotcha was that even though Dockerfile has FROM --platform=linux/amd64 the dagger's result did not run on cloud run (which they require linux/amd64 and I'm building on an arm64 machine). Adding platform: 'linux/amd64' as Platform, to createBuild arg fixed it.
Here's part of my dagger script;
...
connect(
async daggerClient => {
const source = daggerClient
.host()
.directory(path.resolve(process.cwd(), '..'), {
exclude: [
'node_modules/',
'ci/',
'npm-debug.log',
'README.md',
'.next',
'.git',
'.env',
'gcp-dagger-service-account.json',
],
})
.dockerBuild({
platform: 'linux/amd64' as Platform,
dockerfile: 'apps/web/Dockerfile',
})
...
and the dockerfile
FROM node:18-alpine AS base
FROM --platform=linux/amd64 base AS builder
RUN apk add --no-cache libc6-compat
RUN apk update
# Set working directory
WORKDIR /app
RUN npm install -g turbo
COPY . .
RUN turbo prune --scope=web --docker
# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk add --no-cache libc6-compat python3 make g++
RUN apk update
WORKDIR /app
# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/package-lock.json ./package-lock.json
RUN npm ci install
# Build the project
COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json
RUN npm run build --filter=web...
FROM base AS runner
WORKDIR /app
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=installer /app/apps/web/next.config.js .
COPY --from=installer /app/apps/web/package.json .
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public
CMD node apps/web/server.js