Skip to content

Commit

Permalink
ci: replace old docker image with next js recommended one
Browse files Browse the repository at this point in the history
  • Loading branch information
Gum-Joe committed Aug 15, 2024
1 parent 25ed508 commit 45bfa51
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,3 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)."
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
75 changes: 53 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,67 @@
# Use the official Node.js image as the base image
FROM node:lts
FROM node:18-alpine AS base

# Set the working directory
# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Make uploads dir
RUN mkdir -p /uploads
RUN mkdir /app/.next
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi


# Copy package.json and package-lock.json
COPY package*.json ./
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Make a user node & group
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

# Install dependencies
RUN npm install
RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi

# Copy the rest of the application code
COPY . .
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Build the Next.js application
RUN npm run build
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

# Expose the port the app runs on
EXPOSE 3000

# Switch to user node
RUN chown -R node:node /app
RUN chown -R node:node /uploads
USER node
ENV PORT=3000

# Start the Next.js application
CMD ["npm", "start"]
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD HOSTNAME="0.0.0.0" node server.js
6 changes: 4 additions & 2 deletions dev.docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
services:
app:
image: edtech/cpp-connect
image: imperial/cpp-connect
pull_policy: never
build:
context: .
dockerfile: Dockerfile
user: node
depends_on:
- database
environment:
- DATABASE_URL=postgres://user:pass@database:5432/cpp-connect
- UPLOAD_DIR=uploads
- NODE_ENV=development
ports:
- "3000:3000"
volumes:
Expand All @@ -33,4 +35,4 @@ services:

volumes:
database-data:
uploads:
uploads:
4 changes: 3 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}
const nextConfig = {
output: "standalone",
}

export default nextConfig

0 comments on commit 45bfa51

Please sign in to comment.