Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an optimized Dockerfile for Next js App Router #5

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Base stage (common for both development and production)
FROM node:lts-alpine AS base

WORKDIR /usr/src/app

# Copy package.json and package-lock.json first (better layer caching)
COPY package*.json ./

# Install dependencies using npm ci
RUN npm ci

# Copy the rest of the application code
COPY . .

# Development stage
FROM base AS dev

# Expose the port for development
EXPOSE 3000

# Start the Next.js app in development mode
CMD ["npm", "run", "dev"]

# Build stage (for production)
FROM base AS builder

# Build the Next.js app
RUN npm run build

# Production stage
FROM node:lts-alpine AS runner

WORKDIR /usr/src/app

# Install only production dependencies
COPY --from=builder /usr/src/app/package*.json ./
RUN npm ci --omit=dev

# Copy the built app from the build stage
COPY --from=builder /usr/src/app/.next ./.next
# COPY --from=builder /usr/src/app/public ./public
COPY --from=builder /usr/src/app/next.config.mjs ./
COPY --from=builder /usr/src/app/vercel.json ./
COPY --from=builder /usr/src/app/.env ./.env

# Set the environment variable to production
ENV NODE_ENV=production

# Expose the port for production
EXPOSE 3000

# Run the Next.js app in standalone production mode
CMD ["npm", "run", "start"]
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ LoanManagement implements a comprehensive RBAC system:

6. Open [http://localhost:3000](http://localhost:3000) in your browser to see the application.

## Getting Started with Docker

Ensure you have the [Docker](https://docs.docker.com/get-docker/) installed

- Build the image for development:

```bash
docker build --target dev -t loan_dev .
```
- Start the container for development:
```bash
docker run -p 3000:3000 --env-file .env loan_dev
```
- Build the image for production:
```bash
docker build --target runner -t loan_prod .
```
- Start the container for production:
```bash
ddocker run -p 3000:3000 --env-file .env loan_prod
```
## Deployment

This application can be easily deployed on Vercel, the platform created by the creators of Next.js. For other deployment options, refer to the [Next.js deployment documentation](https://nextjs.org/docs/deployment).
Expand Down
105 changes: 82 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading