+3

Build Nextjs 14 Docker Compose

next.config.js

/** @type {import('next').NextConfig} */

import { writeFileSync } from 'fs';

const nextConfig = {
  reactStrictMode: false,
  output: 'standalone',
};

export default nextConfig;

standalone --> CMD ["node", "server.js"] --> chạy port 3000

Dockerfile

node:alpine --> nền tảng linux mới dùng được apk dùng RUN apk add --no-cache libc6-compat clear cache

FROM node:alpine AS base

FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package*.json yarn.lock* ./
RUN yarn --frozen-lockfile

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN yarn build

FROM base AS production
WORKDIR /app
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public

EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]


docker-compose.yml

version: "3.8"

services:
  nextjs:
    ports:
      - 3024:3000
    container_name: websi
    image: websi:latest
    restart: always
    build:
      context: ./
      dockerfile: Dockerfile  
    environment:
      NEXT_PUBLIC_URL_BE: https://xx
      NEXT_PUBLIC_LOGO: ''

.dockerignore

node_modules
.next
.github
.husky
.yarn

Run docker

    - docker-compose down --volumes --remove-orphans
    - docker-compose up -d --force-recreate --build
docker images

REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
abi          latest    b04c775dd088   5 minutes ago    189MB

Dung lượng đã giảm rất nhiều

Gitlab CI/CD runer

stages:
  - build

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - .next

variables:
  DOCKER_BUILDKIT: 1

before_script:
  - git submodule sync --recursive
  - git submodule update --init --recursive

build_dev:
  stage: build
  script:
    - export DOCKER_BUILDKIT=1
    - docker-compose down --volumes --remove-orphans
    - docker-compose up -d --force-recreate --build
  tags:
    - abi
  only:
    - main

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí