Public/static directory lost in multistage docker build. What build assets to copy? #13304
-
Using a simple next js set up (same as custom-server-typescript but with an added FROM node:latest
WORKDIR /app
COPY ./package.json ./package.json
COPY ./yarn.lock ./yarn.lock
RUN yarn install
COPY . .
RUN yarn build
CMD ["yarn", "start"] However my app requires a multistage build so I am trying to copy over the build assets with this Dockerfile ## BUILD PHASE ##
FROM node:latest as builder
WORKDIR /app
COPY ./package.json ./package.json
COPY ./yarn.lock ./yarn.lock
RUN yarn install
COPY . .
RUN yarn run build
## RUN PHASE ##
FROM node:lts-alpine
WORKDIR /app
COPY --from=builder /app/package.json .
COPY --from=builder /app/yarn.lock .
RUN yarn install --production
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/dist ./dist
# Do I need to copy something else from the build output here???
ENV NODE_ENV production
EXPOSE 3000
CMD ["yarn", "start"] This appears to work just fine at first but files within my |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Solved my issue. I needed to copy over the ## BUILD PHASE ##
FROM node:latest as builder
WORKDIR /app
COPY ./lerna.json .
COPY ./package.json .
COPY ./yarn.lock .
COPY ./tsconfig.json .
COPY ./packages/www/package.json ./packages/www/package.json
COPY ./packages/shared/package.json ./packages/shared/package.json
# Will use cache unless any of the above files have changed
RUN yarn install
# Copy the source code to be built
COPY ./packages/www/ ./packages/www/
COPY ./packages/shared/ ./packages/shared/
RUN yarn run build:www
## RUN PHASE ##
FROM node:lts-alpine
WORKDIR /app
COPY --from=builder /app/package.json .
COPY --from=builder /app/packages/www/package.json ./packages/www/package.json
COPY --from=builder /app/packages/shared/package.json ./packages/shared/package.json
RUN yarn install --production
COPY --from=builder /app/packages/shared/dist /app/packages/shared/dist
COPY --from=builder /app/packages/www/dist /app/packages/www/dist
COPY --from=builder /app/packages/www/public /app/packages/www/public
COPY --from=builder /app/packages/www/.next /app/packages/www/.next
WORKDIR /app/packages/www
ENV NODE_ENV production
EXPOSE 3000
CMD ["node", "dist/server.js"] |
Beta Was this translation helpful? Give feedback.
-
A note for those seeing this in 2025. When you configure |
Beta Was this translation helpful? Give feedback.
Solved my issue. I needed to copy over the
public
directory as well, i just assumed it would be built with the project. For anyone that may find it helpful, here is my final working Dockerfile (I have a yarn workspace)