sainsburys-tech / next-logger

JSON logging patcher for Next.js
MIT License
171 stars 14 forks source link

Improve documentation for usage with Docker #79

Open william-abboud opened 1 month ago

william-abboud commented 1 month ago

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',
    ],
    ...
    }
  2. 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!

lah-wag commented 1 week ago

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.