Jabed Zaman
Jabed Zaman


Jabed Zaman

Building and running Nextjs application with Docker

Building and running Nextjs application with Docker

Jabed Zaman's photo
Jabed Zaman
·Oct 28, 2022·

3 min read

Play this article

Table of contents

  • Creating a docker file

Ok the first question is why we need docker? and what is it actually? Docker is actually a container. To understand properly let us take an example of person A working in a group of 4 people.The other three can be related as person B, C and D.

So Person A creates a nextjs application with nextjs version that requires version of node 14 + to be installed on your local machine to run that app. He shares the source code repo with the other three person. Person B and D had node 14 + and person C had node 12. Now person C wont be able to test the application on his system. To resolve this issue we take the help of docker.

Docker creates a container with the respective dependencies to run the application without changing the dependencies on your local host machine.

Docker image is the file which contains all the necessary dependency and configuration which are required to run an application (executable format of an application), we can find docker images in public repositories (docker hub, ECR, …)

Creating a docker file

  • Make sure you have docker desktop installed
  • First create a file in the root directory of your application with the file name Dockerfile. Add the following code to it (This is particular to nextjs application which uses npm)

// Dockerfile
# Install dependencies only when needed
FROM node:16-alpine AS deps
RUN apk add --no-cache libc6-compat
COPY package.json package-lock.json ./
RUN npm install --frozen-lockfile

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


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

COPY . .

RUN npm run build

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

ENV NODE_ENV production

RUN addgroup --system --gid 1001 bloggroup
RUN adduser --system --uid 1001 bloguser

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

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=bloguser:bloggroup /app/.next/standalone ./
COPY --from=builder --chown=bloguser:bloggroup /app/.next/static ./.next/static

USER bloguser



CMD ["node", "server.js"]
  • create a file with file name '.dockerignore'
  • If you have the docker extension then you can directly create the docker image by right-clicking on the docker image and selecting Build Image...


You can now build using the docker build [OPTIONS] PATH | URL | - command

You can push the docker image to docker hub. By signing up there and then creating a repo. you can push the image by either using docker desktop or the docker push [OPTIONS] NAME[:TAG] command.


And thats all you are done

Share this