Skip to main content

Docker build image with Node.js app

Files here

Docker-compose.yaml

version: "3"
services:
nextjs:
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:3000"
environment:
- NODE_ENV=production

Dockerfile

FROM node:20-bullseye
WORKDIR /app
RUN apt update && apt install git
RUN apt update && apt install -y git && rm -rf /var/lib/apt/lists/*

RUN rm -rf /app/* && if [ ! -d "/app/.git" ]; then git clone https://github.com/dedkola/nextjs-docs.git /app; else cd /app && git pull; fi

RUN npm install
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]

Build image

docker build -t nextjs:latest . --no-cache

Run on 5000 port

docker run -p 5000:3000 -d nextjs

Tag image

docker tag image_name:tag username/repo:repo_tag

docker tag nextjs:latest your-username/nextjs:latest

Push to Docker Hub

docker push your-username/nextjs:latest

Uraid pull

Create container and pull it :)

dedkola/nextjs:latest