This is a Next.js project bootstrapped with create-next-app.
Add a Dockerfile to your root repository with exactly this name. In this file, we will add instructions for the docker image we will be building.
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD npm run devTo build the docker Image from the Dockerfile we just created, simply run the following command. I name it my-app, but feel free to change it to what ever you want. Also don’t forget the . at last.
docker build -t my-app .
Once the image is created, we can run it by
docker run -p 3000:3000 my-app3000:3000 specify the the port you want to run the app on, I will run mine on 3000. And if you access http://localhost:3000 , you will see your working app!
FROM node:18-alpine as base
RUN apk add --no-cache g++ make py3-pip libc6-compat
WORKDIR /app
COPY package*.json ./
EXPOSE 3000
FROM base as builder
WORKDIR /app
COPY . .
RUN npm run build
FROM base as production
WORKDIR /app
ENV NODE_ENV=production
RUN npm ci
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
USER nextjs
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
COPY --from=builder /app/public ./public
CMD npm start
FROM base as dev
ENV NODE_ENV=development
RUN npm install
COPY . .
CMD npm run devWith Docker Compose, we don’t need to remember all those long commands to build or run containers. We can simply just use docker-compose build and docker-compose up .
Add a docker-compose.yml file to your root directory with the following conent.
version: '3.8'
services:
app:
image: openai-demo-app
build:
context: ./
target: dev
dockerfile: Dockerfile
volumes:
- .:/app
- /app/node_modules
- /app/.next
ports:
- "3000:3000"We finally made our way to building the Docker image. We are going to use Docker’s BuildKit feature for a faster build.
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose buildAfter the build finish, we can run the image and start the app by
docker-compose upAfter that, if you go to http://localhost:3000 on your browser, you will see your app running!!!
-
Python is not set from command line or npm configuration: (As a mac user) I tried to add python to path, alias python to python3, uninstall and reinstall python, etc.etc.etc. NONE OF THOSE worked out for me. So here are the two solutions I found. (1) usenode:18(or any other number you prefer) instead ofnode:18-alpine. (2) addRUN apk add — no-cache g++ make py3-pip libc6-compatbefore you do any of the installations of your packages. -
Could not find a production build in the ‘/app/.next’ directorywhen usingdocker-compose up: Everything worked as expected if I ran the image throughdocker run. There are solutions out there suggesting addCMD ["npm","run","build"]right beforeCMD npm startin the Dockerfile. However, this gave me an error saying no two CMD allowed. My solution was to add— /app/.nexttovolumesinDocker-compose.yml.