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

chore: optimize docker image size #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions .github/workflows/build-publish-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:

steps:
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Checkout repository
uses: actions/checkout@v3

Expand Down
82 changes: 56 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,67 @@
################################################################################
# BASE
# This is the stage that the other stages in this file are based on.
# - defines the Node version
# - set global configuration
# - set default work dir
################################################################################
FROM node:20.10-alpine as base
# Base image
FROM node:22-bookworm-slim AS base

RUN apk add --update --no-cache git python3 make g++
# Set the working directory
WORKDIR /workspace

RUN npm install -g pnpm
# Configure pnpm so it can be cached
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

ENV HUSKY=0
# Install pnpm with corepack
RUN corepack enable
RUN corepack prepare [email protected] --activate

# Create app directory
WORKDIR /workspace
# Install openssl for Prisma. Add sqlite3 here if you need it.
RUN apt-get update && apt-get install -y openssl && rm -rf /var/lib/apt/lists/*

# Setup development node_modules
FROM base AS dev-deps

ADD package.json pnpm-lock.yaml ./

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

# Build the app
FROM base AS build

# Get the node_modules from the dev-deps stage
COPY --from=dev-deps /workspace/node_modules /workspace/node_modules

## Add and generate the Prisma client
#ADD prisma prisma
#RUN pnpx prisma generate
#
# Add the rest of the app
ADD . .

# Build the app
RUN pnpm run build

# Show the contents of the workspace
RUN find /workspace -type f

# Setup production node_modules
FROM base AS prod-deps

ENV NODE_ENV=production

# Copy package.json and the lock file
COPY package.json pnpm-lock.yaml /workspace/
COPY --from=build /workspace/dist/apps/api/package.json /workspace
COPY --from=build /workspace/dist/apps/api/pnpm-lock.yaml /workspace

# Install app dependencies
RUN pnpm install --frozen-lockfile --prefer-frozen-lockfile
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --no-frozen-lockfile --no-optional
#RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install prisma

# Copy source files
COPY . .

# Build apps
RUN pnpm build
# Build the production image
FROM base

COPY prisma/schema.prisma /workspace/prisma/schema.prisma
ENV NODE_ENV=production

# Expose default port
EXPOSE 3000
COPY --from=prod-deps /workspace/node_modules /workspace/node_modules
COPY --from=prod-deps /workspace/package.json /workspace/package.json
COPY --from=build /workspace/node_modules/.prisma /workspace/node_modules/.prisma
COPY --from=build /workspace/dist /workspace/dist
COPY --from=build /workspace/prisma /workspace/prisma

# Start server
CMD pnpm prestart; pnpm start
CMD [ "node", "dist/apps/api/main.js" ]
5 changes: 5 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"prisma": "5.15.1"
}
}
3 changes: 2 additions & 1 deletion apps/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"tsConfig": "apps/api/tsconfig.app.json",
"assets": ["apps/api/src/assets"],
"isolatedConfig": true,
"webpackConfig": "apps/api/webpack.config.js"
"webpackConfig": "apps/api/webpack.config.js",
"generatePackageJson": true
},
"configurations": {
"development": {},
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
version: '3'
services:
api:
build:
context: .
dockerfile: Dockerfile
ports:
- 3000:3000
environment:
DATABASE_URL: postgresql://pubkey-stack:pubkey-stack@postgres:5432/pubkey-stack?schema=public
DATABASE_PROVISION: true
HOST: 0.0.0.0
JWT_SECRET: 1dfe5003518560c6362eab48f8220edf8708bbc67efbd4ed8cdf621965e938ee
PORT: 3000
SESSION_SECRET: 1dfe5003518560c6362eab48f8220edf8708bbc67efbd4ed8cdf621965e938ee
WEB_URL: http://localhost:4200
postgres:
image: postgres:15-alpine
# command: [ "postgres", "-c", "log_statement=all", "-c", "log_destination=stderr" ]
Expand Down
7 changes: 6 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"workspaceLayout": { "projectNameAndRootFormat": "as-provided" },
"workspaceLayout": {},
"targetDefaults": {
"build": { "dependsOn": ["^build"], "cache": true },
"e2e": { "cache": true },
Expand All @@ -13,6 +13,11 @@
"@nx/eslint:lint": {
"inputs": ["default", "{workspaceRoot}/.eslintrc.json", "{workspaceRoot}/.eslintignore"],
"cache": true
},
"@nx/js:tsc": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["default", "^default"]
}
},
"generators": { "@nx/react": { "application": { "babel": true }, "library": { "unitTestRunner": "none" } } },
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,5 @@
"pnpm nx format --uncommitted"
]
},
"packageManager": "pnpm@9.6.0+sha256.dae0f7e822c56b20979bb5965e3b73b8bdabb6b8b8ef121da6d857508599ca35"
"packageManager": "pnpm@9.7.1+sha256.46f1bbc8f8020aa9869568c387198f1a813f21fb44c82f400e7d1dbde6c70b40"
}
3 changes: 3 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
generator client {
provider = "prisma-client-js"
// This is needed to make the Prisma Client work in a Docker container built using pnpm.
// Found here: https://github.com/prisma/prisma/issues/6603#issuecomment-1054227614
output = "../node_modules/.prisma/client"
}

datasource db {
Expand Down
Loading