Skip to content

Commit

Permalink
docker image optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
avukalov committed Oct 6, 2024
1 parent 8a503fe commit 1987580
Show file tree
Hide file tree
Showing 10 changed files with 667 additions and 333 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
node_modules
.dockerignore
Dockerfile
docker-compose.local.yml
docker-compose.yml
.git
.next

.env

.env.local
.env.example
2 changes: 1 addition & 1 deletion .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ HASURA_URL="https://safe-sparrow-62.hasura.app/v1/graphql"

# AUTH0
AUTH0_AUDIENCE="https://hasura.io/learn"
AUTH0_BASE_URL="http://localhost:3000/"
AUTH0_BASE_URL="http://localhost:3001/"
AUTH0_SECRET="2ddc1f2f1d75f11d845f007dca103726709dfe8c78a02ac6cbfbbb815f73a867"
AUTH0_ISSUER_BASE_URL="https://avdevelopment.eu.auth0.com"
AUTH0_CLIENT_ID="Ai36nBSeAhPOG8C2tyMnjV5x0TpKNway"
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Docker Image CI
on:
push:
branches: ['develop']
pull_request:
branches: ['develop']

jobs:
build:
Expand All @@ -21,6 +19,9 @@ jobs:
docker build . \
--file Dockerfile \
--tag avukalov/next-dashboard:latest
--build-arg NEXT_PUBLIC_HASURA_WS=${{ secrets.NEXT_PUBLIC_HASURA_WS }} \
--build-arg HASURA_URL=${{ secrets.HASURA_URL }} \
--build-arg HASURA_SECRET=${{ secrets.HASURA_SECRET }} \
- name: Push the Docker image
run: |
Expand Down Expand Up @@ -49,9 +50,9 @@ jobs:
# echo 'AUTH0_CLIENT_SECRET=${{ secrets.AUTH0_CLIENT_SECRET }}' >> .env.local
# echo 'POSTGRES_URL=${{ secrets.POSTGRES_URL }}' >> .env.local

# --build-arg NEXT_PUBLIC_HASURA_WS=${{ secrets.NEXT_PUBLIC_HASURA_WS }} \
# --build-arg HASURA_SECRET=${{ secrets.HASURA_SECRET }} \
# --build-arg NEXT_PUBLIC_HASURA_WS=${{ secrets.NEXT_PUBLIC_HASURA_WS }} \
# --build-arg HASURA_URL=${{ secrets.HASURA_URL }} \
# --build-arg HASURA_SECRET=${{ secrets.HASURA_SECRET }} \
# --build-arg AUTH0_AUDIENCE=${{ secrets.AUTH0_AUDIENCE }} \
# --build-arg AUTH0_BASE_URL=${{ secrets.AUTH0_BASE_URL }} \
# --build-arg AUTH0_SECRET=${{ secrets.AUTH0_SECRET }} \
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ yarn-error.log*
# local env files

.env
.env.local

#Docker
Dockerfile.local
docker-compose.local.yml

# vercel
.vercel
Expand Down
76 changes: 42 additions & 34 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
# Use the official Node.js image
FROM node:18-alpine AS builder
# Stage 1: Base image
FROM node:18-alpine AS base

# Install libc6-compat for compatibility if needed
RUN apk add --no-cache libc6-compat

# Set working directory
WORKDIR /app

# Install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application code
# Stage 2: Install dependencies based on the preferred package manager
FROM base AS deps

COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./

# Conditional installation of dependencies based on the lockfile present
RUN \
if [ -f yarn.lock ]; then yarn install --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm install; \
else echo "Lockfile not found." && exit 1; \
fi

# Stage 3: Rebuild the source code only when needed
FROM base AS builder

WORKDIR /app

# Copy dependencies from the previous stage
COPY --from=deps /app/node_modules ./node_modules

# Copy application source code
COPY . .

ARG HASURA_URL_ARG
ARG HASURA_SECRET_ARG
ARG NEXT_PUBLIC_HASURA_WS_ARG

ENV HASURA_URL=$HASURA_URL_ARG
ENV HASURA_SECRET=$HASURA_SECRET_ARG
ENV NEXT_PUBLIC_HASURA_WS=$NEXT_PUBLIC_HASURA_WS_ARG

# Build the application
RUN npm run build

# Stage 2: Run
FROM node:18-alpine
FROM base AS runner

WORKDIR /app

COPY --from=builder /app/package*.json ./
RUN npm install --production

COPY --from=builder /app/.next ./.next
COPY --from=builder /app/package.json .
COPY --from=builder /app/package-lock.json .
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

EXPOSE 3000

CMD ["npm", "run", "start"]



# Set environment variables for Hasura, Auth0, and Postgres
# ENV NEXT_PUBLIC_HASURA_WS=${NEXT_PUBLIC_HASURA_WS}
# ENV HASURA_SECRET=${HASURA_SECRET}
# ENV HASURA_URL=${HASURA_URL}
# ENV AUTH0_AUDIENCE=${AUTH0_AUDIENCE}
# ENV AUTH0_BASE_URL=${AUTH0_BASE_URL}
# ENV AUTH0_SECRET=${AUTH0_SECRET}
# ENV AUTH0_ISSUER_BASE_URL=${AUTH0_ISSUER_BASE_URL}
# ENV AUTH0_CLIENT_ID=${AUTH0_CLIENT_ID}
# ENV AUTH0_CLIENT_SECRET=${AUTH0_CLIENT_SECRET}
# ENV AUTH_SECRET=${AUTH_SECRET}
# ENV POSTGRES_URL=${POSTGRES_URL}
# ENV POSTGRES_URL_NON_POOLING=${POSTGRES_URL_NON_POOLING}
# ENV POSTGRES_PRISMA_URL=${POSTGRES_PRISMA_URL}
# ENV POSTGRES_USER=${POSTGRES_USER}
# ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
# ENV POSTGRES_HOST=${POSTGRES_HOST}
# ENV POSTGRES_DATABASE=${POSTGRES_DATABASE}
CMD ["node", "server.js"]
Loading

0 comments on commit 1987580

Please sign in to comment.