Build NextJS Docker image with nginx server

  docker, next.js, nginx

I am new to docker and trying to learn it by it’s documentation. AS i need to create a NextJS build using docker image for nginx server i have followed the below process.

  1. Install the nginx
  2. Seeding the port 80 to 3000 in the default config.
  3. Symlink the out directory to base nginx directory
  4. CMD to take care the production build and symlinking of the out directory.
FROM node:alpine AS deps

RUN apk add --no-cache libc6-compat git
RUN apt-get install nginx -y

WORKDIR /sample-app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

FROM node:alpine AS builder
WORKDIR /sample-app
COPY . .
COPY --from=deps /sample-app/node_modules ./node_modules
RUN yarn build

FROM node:alpine AS runner
WORKDIR /sample-app

ENV NODE_ENV production

RUN ls -s /sample-app/out /usr/share/nginx/html -f
RUN -p 3000:80 -v /sample-app/out:/usr/share/nginx/html:ro -d nginx
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /sample-app/out
USER nextjs

CMD ["nginx -g daemon=off"]

While running the docker build shell script command as sudo docker build . -t sample-app it throws the error The command '/bin/sh -c apt-get install nginx -y' returned a non-zero code: 127

Source: Docker Questions