Want to deploy your Next.js app like a pro? This post will help you Dockerize your app and set up a seamless CI/CD pipeline using GitHub Actions, then deploy it to any cloud provider or server.
๐งฑ 1. Start with Your Next.js App
npx create-next-app my-nextjs-app
cd my-nextjs-app
๐ฆ 2. Create a Production-Ready Dockerfile
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node:18-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
EXPOSE 3000
CMD ["npm", "start"]
๐ 3. Add a .dockerignore
node_modules
.next/cache
Dockerfile
.dockerignore
๐งช 4. Test Locally
docker build -t my-nextjs-app .
docker run -p 3000:3000 my-nextjs-app
๐ค 5. GitHub Actions CI/CD Workflow
Create .github/workflows/deploy.yml
:
name: Deploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- run: |
docker build -t myuser/nextjs-app:${{ github.sha }} .
docker push myuser/nextjs-app:${{ github.sha }}
- uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
docker pull myuser/nextjs-app:${{ github.sha }}
docker stop nextjs-app || true
docker rm nextjs-app || true
docker run -d -p 3000:3000 --name nextjs-app myuser/nextjs-app:${{ github.sha }}
โ๏ธ 6. Where Can You Deploy?
- VPS (DigitalOcean, Linode, etc.)
- Render / Railway
- AWS ECS + ECR
- Google Cloud Run
- Fly.io
โ Summary
With this setup:
- You Dockerize your Next.js app
- Automate builds & deployment via CI/CD
- Ensure portability, consistency, and scalability
๐ Read the Full Guide on Medium
This was a quick overview. For detailed steps, visuals, and pro tips:
Let me know in the comments how you deploy your Next.js apps or if you'd like a version with PostgreSQL, Redis, or NGINX reverse proxy! ๐ณ๐ฅ
Top comments (0)