next-logger
next-logger copied to clipboard
Improve documentation for usage with Docker
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.
- Make sure
next-logger.config.jsfile is located in the root of the project (alongside package.json for example) - Add
NEXT_RUNTIME=nodejsENV variable in Docker - Sincenext-loggeris required ininstrumentation.ts|jsfile typically the library will only be loaded if the runtime isnodejs. 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
- Add
next-loggerandpinotoserverComponentsExternalPackagesproperty in Next configuration -
// next.config.js
...
experimental {
...
serverComponentsExternalPackages: [
'next-logger',
'pino',
],
...
}
- Copy the
next-logger.config.jsfile from the root of the project and place it in the runtime image in the same folder asserver.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!
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.
I do copy the whole of the node_modules:
COPY --from=deps /app/node_modules ./node_modules
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... :)
@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
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.
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