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)