Renato Pozzi's Blog

Renato Pozzi's Blog

Manage NEXT_PUBLIC Environment Variables at Runtime with Docker

Manage NEXT_PUBLIC Environment Variables at Runtime with Docker

Subscribe to my newsletter and never miss my upcoming articles

Next.js is definitely a very good solution for making modern web applications, it's fast, simple, and reliable. It works very well also with Docker, you can build a production image with a few lines of Dockerfile, and deploy your app to the world.

However, there is a problem: when you build your docker image, and your app requires some client-side environment variables, (the famous NEXTPUBLIC) env vars, these variables will be set during build time, and you will no longer have a way to change them.

Well, a quite tricky solution is to do the variable replace directly on runtime as docker image entrypoint! Let's see an example:

Suppose you have to set up an API_URL endpoint for your client, obviously, you will set up something like that:


What we can do on the Dockerfile, is something like that:

# Install dependencies only when needed
FROM node:14-alpine AS deps

# Check to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat


COPY package.json package-lock.json ./

RUN npm ci

# Rebuild the source code only when needed
FROM node:14-alpine AS builder


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


# Production image, copy all the files and run next
FROM node:14-alpine AS runner


ENV NODE_ENV production

COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/ ./

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /app/.next

USER nextjs


RUN npx next telemetry disable

ENTRYPOINT ["/app/"]

CMD npm run start

This is a common Next.js dockerfile, but attention must be payed to this row:


The build will be launched with an environment placeholder in this row, so your API_URL will be temporarily set to a string with value: APP_NEXT_PUBLIC_API_URL.

After the image build, we set a custom entrypoint called

ENTRYPOINT ["/app/"]

This file contains a set of specific instructions:


echo "Check that we have NEXT_PUBLIC_API_URL vars"

find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#APP_NEXT_PUBLIC_API_URL#$NEXT_PUBLIC_API_URL#g"

echo "Starting Nextjs"
exec "$@"

When the docker image starts, the entrypoint will replace all the previously set environment placeholders, with the real values, passed by the NEXT_PUBLIC_API_URL environment variable!

So you can pass your value directly for example in your docker-compose.yml:

version: "3.7"
      - "3000:3000"
      NEXT_PUBLIC_API_URL: http://localhost:5000

Or also in your command line interface:

docker run -e NEXT_PUBLIC_API_URL="http://localhost:5000"

This is all you need to do to accomplish this solution!

A couple of things to remember:

  • This is a tricky solution, so use it if you don't have any other alternative.
  • Using this technique, the image will be prepared on runtime, but if you need to change again the value, you need to delete the currently running container and run another, because the entrypoint will not find again the placeholder in the current container!

Thank you for reading this article, I really appreciate it. Please leave a reaction if the article helped you.

If you want you can follow me on Twitter


Share this