Whale

Portfolio Project • Things I Wish I Knew Beforehand • Next.js + Docker

This year I wished. Overkill? You bet!

DateFirstnameLastnamePhone #
12/25/2020DeanWatts443 758 7382
12/26/2020NicoleSattler443 255 6412
Setting up

The goal was to have a barebones Next.js Development Environment with Hot Reload Support + Production Images

This year I wished. Overkill? You bet!

Production Config

Here is the production config. You'll note that Dockerfile is mostly the same as Dockerfile.dev, with the exception of environment variables and the command that gets ran (npm run dev vs npm start). If anyone has a better method for handling Development vs Production commands please let me know!

Dockerfile
FROM node:alpine

RUN mkdir -p /app

ENV NODE_ENV production
ENV PORT 3000

EXPOSE 3000

WORKDIR /app

COPY package.json /app
COPY package-lock.json /app

RUN npm install

COPY . /app

RUN npm run build

RUN npx next telemetry disable

CMD [ "npm", "start" ]
docker-compose.yml
version: '3'

services:
  nextjsprod:
    ports:
      - 3000:3000
    build:
      context: .
      dockerfile: Dockerfile
Development Config
Dockerfile.dev
FROM node:alpine

RUN mkdir -p /opt/app

ENV PORT 3000

EXPOSE 3000

WORKDIR /opt/app

COPY package.json /opt/app
COPY package-lock.json /opt/app

RUN npm install

COPY . /opt/app

RUN npx next telemetry disable

CMD [ "npm", "run", "dev" ]
docker-compose.dev.yml
version: '3'

services:
  nextjs:
    ports:
      - 3000:3000
    build:
      context: .
      dockerfile: Dockerfile.dev
    volumes:
      - ./:/app
      - /app/node_modules
      - /app/.next
next.config.js
module.exports = {
  webpackDevMiddleware: config => {
    config.watchOptions = {
      poll: 1000,
      aggregateTimeout: 300,
    }
    return config
  },
}
Scripts
start-dev.sh
if [ -z "$1" ]; then
  docker-compose -f docker-compose.dev.yml up -d
else
  docker-compose -f docker-compose.dev.yml up -d --build
fi
stop-dev.sh
docker-compose down

Docker Running

Localhost Running