next-logger icon indicating copy to clipboard operation
next-logger copied to clipboard

Improve documentation for usage with Docker

Open william-abboud opened this issue 1 year ago • 3 comments

After struggling for a few hours I finally got next-logger and pino to work within Docker but the docs here need to be improved. I hope by raising this issue it saves people time. Here are the necessary steps I took in order to get it to work correctly.

  1. Make sure next-logger.config.js file is located in the root of the project (alongside package.json for example)
  2. Add NEXT_RUNTIME=nodejs ENV variable in Docker - Since next-logger is required in instrumentation.ts|js file typically the library will only be loaded if the runtime is nodejs. This variable is set by Vercel automatically but if you are not deploying on Vercel you have to do it manually.
// instrumentation.ts file
export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    await require("pino");
    await require("next-logger");
  }
}
// Dockerfile
ENV NEXT_RUNTIME=nodejs
  1. Add next-logger and pino to serverComponentsExternalPackages property in Next configuration -
// next.config.js
...
experimental {
  ...
  serverComponentsExternalPackages: [
    'next-logger',
    'pino',
  ],
  ...
}
  1. Copy the next-logger.config.js file from the root of the project and place it in the runtime image in the same folder as server.js. Thank you: https://github.com/sainsburys-tech/next-logger/issues/26#issuecomment-2224101208 for this
// Dockerfile
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy the next-logger.config.js
COPY --from=builder /app/next-logger.config.js ./

Thank you to all the developers making this project!

william-abboud avatar Sep 27 '24 07:09 william-abboud

I'm on next 14, and I can' get serverComponentsExternalPackages to work.

Do you need to copy next-logger and pino from node_modules into the resulting Docker-container for this to work?

COPY --from=deps --chown=nextjs:nodejs /node_modules/pino ./

Or similar.

lah-wag avatar Nov 06 '24 14:11 lah-wag

I do copy the whole of the node_modules:

COPY --from=deps /app/node_modules ./node_modules

william-abboud avatar Nov 20 '24 09:11 william-abboud

I'm running into a similar issue.

When I configure next-logger without any custom config (no next-logger.config.js file) I can get it working with docker. (Doing what william-abboud describes here) But once I copy the config file, that works when running in dev mode, it seems like next-logger is not used at all... I can see log traces from the instrumentation file, so that seems to still work correct.

Does anyone have any idea why a loggerfile might work fine from dev mode, but not in a docker container?

EDIT: Leaving this up as a reminder to myself to read more carefully in the future... I thought I already did the copy of node modules, but that was just in the builder layer of docker, while it should be added in the runner layer too. (When using the example docker config from nextjs) The only thing that is weird is that it does work without the config file, even when not copying the node_modules... :)

Styn avatar Mar 03 '25 10:03 Styn

@william-abboud god bless you. I spent two days squeezing my brain why this was working just fine in local, even with NEXT_OUTPUT=standalone and didn't get it working on docker. Definitely guys this entry is a must for README.md

jhbarrantes avatar Oct 17 '25 17:10 jhbarrantes

Has anyone got it to run in a pnpm mono repo with 2 nextjs apps? I'm trying to get it to work with a custom config but running into issues.

rubenmamo avatar Nov 17 '25 09:11 rubenmamo

Has anyone got it to run in a pnpm mono repo with 2 nextjs apps? I'm trying to get it to work with a custom config but running into issues.

I got it to work, just in case anyone needs help on this, below is what I did slightly different to what @william-abboud described:

Assuming a pnpm mono repo with the following structure:

  • packages
    • admin-portal
    • website

I added this as the last step in the dockerfile for admin-portal

# Copy node_modules for next-logger and pino to work in Docker
COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --from=deps --chown=nextjs:nodejs /app/packages/admin-portal/node_modules ./packages/admin-portal/node_modules
COPY --from=builder --chown=nextjs:nodejs /app/packages/admin-portal/next-logger.config.js ./packages/admin-portal/next-logger.config.js

and this as the last step in the dockerfile for website

# Copy node_modules for next-logger and pino to work in Docker
COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --from=deps --chown=nextjs:nodejs /app/packages/website/node_modules ./packages/website/node_modules
COPY --from=builder --chown=nextjs:nodejs /app/packages/website/next-logger.config.js ./packages/website/next-logger.config.js

rubenmamo avatar Nov 17 '25 10:11 rubenmamo