# Use the official Node.js 18 image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Install dependencies separately to leverage Docker caching
COPY package*.json ./
# Copy the rest of your app's source code
COPY . .
# Build the Next.js app
RUN npm run build
# Expose the port Next.js runs on
EXPOSE 3000
# Start the Next.js app
CMD ["npm", "start"]
1️⃣ For production deployment: This Dockerfile creates a lightweight container to build and run a Next.js app in production.
2️⃣ Creates a build image: It installs dependencies, copies the code, and builds the Next.js app to create a clean, production-ready image.
3️⃣ Runs the app efficiently: It exposes port 3000 and executes npm start to serve the pre-built Next.js app to users.
✅ Step 2: Add .dockerignore
This prevents unnecessary files from being copied into the Docker image.
# Dependency folders
node_modules
.pnp
.pnp.*
.yarn
*.tsbuildinfo
# Build output
.next
out
build
dist
# Logs and debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Test coverage
coverage
# Local environment variables
.env*
.DS_Store
# Vercel/hosting
.vercel
# Ignore lockfiles (optional: you can include this if you want reproducible builds)
package-lock.json
yarn.lock
# Misc
*.pem
next-env.d.ts
Open your terminal in the project root and run:
docker build -t trainable-app .
• trainable-app is the name of your image. You can name it anything.
docker run -p 3000:3000 trainable-app