I'm trying to setup a build flow in my monorepo with pnpm and turbo.
My goal is to have a structure like this:
apps/
web/
Dockerfile
api/
Dockerfile
Dockerfile
package.json
pnpm-lock.yaml
turbo.json
Where the root Dockerfile looks like this:
FROM --platform=linux/amd64 node:18-alpine as builder
WORKDIR /usr/src/app
# Prerequisites
RUN apk add --no-cache libc6-compat
RUN apk update
RUN npm install -g pnpm@7.16.0
# Copy dependency definitions
COPY package.json ./
COPY pnpm*.yaml ./
COPY .npmrc ./
RUN pnpm fetch
# Set CI to true to disable lefthook hooks
ENV CI=true
# Copy all other files
COPY . ./
And then the /api Dockerfile looks like this (based on this example):
FROM me/base as base
FROM node:18-alpine as api-builder
WORKDIR /usr/src/app
# Prerequisites
RUN apk add --no-cache libc6-compat
RUN apk update
RUN npm install -g turbo
COPY --from=base /usr/src/app /usr/src/app/
RUN turbo prune --scope=api --docker
FROM node:18-alpine as api-installer
WORKDIR /usr/src/app
# Prerequisites
RUN apk add --no-cache libc6-compat
RUN apk update
RUN npm install -g pnpm@7.16.0
COPY --from=api-builder /usr/src/app/out/json ./
COPY --from=api-builder /usr/src/app/pnpm-lock.yaml ./pnpm-lock.json
RUN pnpm install --filter=api --offline
COPY --from=api-builder /usr/src/app/out/full .
COPY --from=api-builder /usr/src/app/turbo.json ./turbo.json
FROM node:18-alpine as api-runner
WORKDIR /usr/src/app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 runner
USER runner
COPY --from=api-installer /app/apps/api/package.json .
COPY --from=api-installer --chown=runner:nodejs /app/apps/api/build ./
ENTRYPOINT [ "node", "apps/api/build/server.js" ]
I start with building the base image like this:
docker build . -t me/base
And I double checked the image is effectively build (docker image ls
).
Next, I try to run the Dockerfile inside /apps/api
But there it fails with:
[+] Building 0.7s (4/4) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.08kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> ERROR [internal] load metadata for docker.io/me/base:latest 0.6s
=> CANCELED [internal] load metadata for docker.io/library/node:18-alpine 0.6s
------
> [internal] load metadata for docker.io/me/base:latest:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
I got my inspiration from Docker Multi-Stage: How to split up into multiple Dockerfiles
How can I make sure it doesn't pull from docker.io
but from my local built image instead?
NOTE: I know there can be some optimizations but as a first version I first want to have this setup working.
Ok, my issue here was that I had to use --platform=linux/amd64
in the base image to make sure the prisma
dependency still worked.
This means that I also have to use this in the followup images, because otherwise it can't find the local (then ARM64 since I'm on a m1 mac) image.