GithubHelp home page GithubHelp logo

docker-demo-nextjs's Introduction

Nextjs-Docker-Demo

Create Next-app

yarn create next-app next-docker-demo
cd next-docker-demo

Create Dev Dockerfile

FROM node:16-alpine

WORKDIR /app

COPY package.json yarn.lock ./

RUN yarn install

COPY next.config.js ./next.config.js

COPY pages ./pages
COPY public ./public
COPY styles ./styles

CMD ["yarn","dev"]

Create Dev Docker-compose.yml

version: '3'

services:
  app:
    image: nextjs-docker-dev
    build: .
    ports:
      - 3000:3000
    volumes:
      - ./pages:/app/pages
      - ./public:/app/public
      - ./styles:/app/styles

Build/Run dev Docker Container

docker-compose up --build --force-recreate

Create production Dockerfile

# Step1
FROM node:16-alpine AS deps

ENV NODE_ENV=production

RUN apk add --no-cache libc6-compat

WORKDIR /app

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

# Step2
FROM node:16-alpine AS builder

ENV NODE_ENV=production

WORKDIR /app

COPY next.config.js ./
COPY package.json yarn.lock ./
COPY --from=deps /app/node_modules ./node_modules

COPY pages ./pages
COPY public ./public
COPY styles ./styles

RUN yarn build

# Step3
FROM node:16-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

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

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

CMD ["node","server.js"]

Add Setting in next.config.js

const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  experimental: {
    outputStandalone: true,
  },
};

Create docker-compose.production.yml

version: '3'

services:
  app:
    image: nextjs-docker-production
    
    build: 
      context: ./
      dockerfile: Dockerfile.production
    
    ports:
      - 3000:3000

Build/Run Docker Contrainer

docker-compose -f docker-compose.production.yml up --build --force-recreate

Source

youtube

docker-demo-nextjs's People

Contributors

ademkao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.