0

I need to publish my Next js project on a server. For that, by request of admin, I need to dockerize it (because SWC compiler is not available on the server).

I set up Docker Desktop, created Dockerfile (content below) and docker-compose.yml (content below)

Then I successfully ran "docker-compose build" and then "docker-compose up" - after that website is successfully work on my localhost:3000

What is my next steps? What should I provide to admin? I can push those 2 files on Github, but I guess, it's not enough. I can see in my docker app, that it created image of 723.88 Mb. Maybe I need to sent this one? But how and where it located?

I'm a noob in Docker, any advice is highly welcomed.

My Dockerfile:

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

# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN yarn install --frozen-lockfile

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

WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build && yarn install --production --ignore-scripts --prefer-offline

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

WORKDIR /app

RUN npm install --global pm2

ENV NODE_ENV production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

USER nextjs

EXPOSE 3000

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
ENV NEXT_TELEMETRY_DISABLED 1

# Run npm start script with PM2 when container starts
CMD [ "pm2-runtime", "npm", "--", "start" ]

My docker-compose.yml file:

version: '3'
services:
  next:
    build: ./frontend
    image: dockerhubid/project-webui:latest
    ports:
      - '3000:3000'
  • 1
    Does your target server have docker-compose installed ? Does it have access to the internet to download the base image and build ? Else do you have an internal docker registry available where you could push the built image ? Didn't your admin tell you how he intended to get your image ? – Zeitounator Jan 09 '22 at 13:41
  • In terms of space, this setup still has all of the `devDependencies` in the final image (they are installed in the `deps` stage, copied into the `build` stage, and copied again into the final image); you could adjust the phasing to avoid this. You also probably don't need both `pm2` and Docker managing the application lifecycle ([what is the point of using pm2 and docker together?](https://stackoverflow.com/questions/51191378/what-is-the-point-of-using-pm2-and-docker-together)) and you could potentially remove this. – David Maze Jan 09 '22 at 14:32
  • 1
    Ask your admin how they prefer to get it. My guess is that they have a docker repository that you need to push the image to. – Hans Kilian Jan 09 '22 at 17:42

0 Answers0