Skip to content

Commit

Permalink
docker: 🐬 Optimize Dockerfile for smaller image size
Browse files Browse the repository at this point in the history
- Use multi-stage builds to reduce final image size
- Update base image to node:18.17.0-alpine for better reproducibility
- Implement best practices for layer caching and dependency management
  • Loading branch information
ad956 committed Oct 10, 2024
1 parent 414dc16 commit 1a00237
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-alpine AS base
FROM node:18.17.0-alpine AS base

# Install dependencies only when needed
FROM base AS deps
Expand All @@ -9,7 +9,7 @@ WORKDIR /app
COPY package*.json ./

# Install dependencies
RUN npm ci
RUN npm ci --only=production

# Rebuild the source code only when needed
FROM base AS builder
Expand Down Expand Up @@ -39,16 +39,20 @@ ENV NODE_ENV=production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Copy necessary files
# Copy only necessary files
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

# Optimize for production
RUN npm prune --production

USER nextjs

EXPOSE 3000

ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

# Use a multi-stage build to reduce layers
CMD ["node", "server.js"]
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ services:
- NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME=${NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME}
- NEXT_PUBLIC_CLOUDINARY_API_KEY=${NEXT_PUBLIC_CLOUDINARY_API_KEY}
- CLOUDINARY_API_SECRET=${CLOUDINARY_API_SECRET}
- NODE_ENV=development
ports:
- "3000:3000"

0 comments on commit 1a00237

Please sign in to comment.