Skip to content

Commit 2a815c9

Browse files
committed
reduce docker image size
1 parent cef9b5c commit 2a815c9

File tree

4 files changed

+99
-27
lines changed

4 files changed

+99
-27
lines changed

.dockerignore

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
1+
# Dependencies (will be installed fresh in container)
12
node_modules/
2-
npm-debug.log
3-
test/
3+
4+
# Database and config (mounted as volumes)
45
db/
56
conf/
7+
8+
# Git
69
.git/
710
.github/
11+
.gitignore
12+
13+
# IDE and editor
14+
.idea/
15+
.vscode/
16+
*.swp
17+
*.swo
18+
.DS_Store
19+
20+
# Testing
21+
test/
22+
23+
# Documentation
24+
doc/
25+
*.md
26+
!README.md
27+
28+
# Development config files
29+
.babelrc
30+
.husky/
31+
.nvmrc
32+
.prettierrc
33+
.prettierignore
34+
eslint.config.js
35+
36+
# Docker files (not needed inside container)
37+
Dockerfile
38+
docker-compose.yml
39+
docker-test.sh
40+
.dockerignore
41+
42+
# Logs
43+
*.log
44+
npm-debug.log
45+
46+
# Build artifacts (built fresh in container)
47+
dist/

Dockerfile

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,58 @@
1-
FROM node:22-slim
1+
# ================================
2+
# Stage 1: Build stage
3+
# ================================
4+
FROM node:22-alpine AS builder
5+
6+
WORKDIR /build
7+
8+
# Install build dependencies needed for native modules (better-sqlite3)
9+
RUN apk add --no-cache python3 make g++
10+
11+
# Copy package files first for better layer caching
12+
COPY package.json yarn.lock ./
13+
14+
# Install all dependencies (including devDependencies for building)
15+
RUN yarn config set network-timeout 600000 \
16+
&& yarn --frozen-lockfile
17+
18+
# Copy source files needed for build
19+
COPY index.html vite.config.js ./
20+
COPY ui ./ui
21+
COPY lib ./lib
22+
23+
# Build frontend assets
24+
RUN yarn build:frontend
25+
26+
# ================================
27+
# Stage 2: Production stage
28+
# ================================
29+
FROM node:22-alpine
230

331
WORKDIR /fredy
432

5-
# Install Chromium and curl without extra recommended packages and clean apt cache
6-
# curl is needed for the health check
7-
RUN apt-get update \
8-
&& apt-get install -y --no-install-recommends chromium curl \
9-
&& rm -rf /var/lib/apt/lists/*
33+
# Install Chromium and curl (for healthcheck)
34+
# Using Alpine's chromium package which is much smaller
35+
RUN apk add --no-cache chromium curl
1036

1137
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
12-
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
38+
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
1339

14-
# Copy lockfiles first to leverage cache for dependencies
15-
COPY package.json yarn.lock .
40+
# Install build dependencies for native modules, then remove them after yarn install
41+
COPY package.json yarn.lock ./
1642

17-
# Set Yarn timeout, install dependencies and PM2 globally
18-
RUN yarn config set network-timeout 600000 \
19-
&& yarn --frozen-lockfile \
20-
&& yarn global add pm2
43+
RUN apk add --no-cache --virtual .build-deps python3 make g++ \
44+
&& yarn config set network-timeout 600000 \
45+
&& yarn --frozen-lockfile --production \
46+
&& yarn cache clean \
47+
&& apk del .build-deps
2148

22-
# Copy application source and build production assets
23-
COPY . .
24-
RUN yarn build:frontend
49+
# Copy built frontend from builder stage
50+
COPY --from=builder /build/ui/public ./ui/public
51+
52+
# Copy application source (only what's needed at runtime)
53+
COPY index.js ./
54+
COPY index.html ./
55+
COPY lib ./lib
2556

2657
# Prepare runtime directories and symlinks for data and config
2758
RUN mkdir -p /db /conf \
@@ -34,5 +65,4 @@ EXPOSE 9998
3465
VOLUME /db
3566
VOLUME /conf
3667

37-
# Start application using PM2 runtime
38-
CMD ["pm2-runtime", "index.js"]
68+
CMD ["node", "index.js"]

docker-compose.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
services:
22
fredy:
33
container_name: fredy
4-
# build from empty build folder to reduce size of image
54
build:
65
context: .
76
dockerfile: Dockerfile
87
image: ghcr.io/orangecoding/fredy
9-
# map existing config and database
108
volumes:
119
- ./conf:/conf
1210
- ./db:/db
1311
ports:
1412
- "9998:9998"
1513
restart: unless-stopped
14+
# Resource limits to prevent runaway memory usage from Chromium
15+
deploy:
16+
resources:
17+
limits:
18+
memory: 1G
1619
healthcheck:
17-
# The container will immediately stop when health check fails after retries
18-
test: ["CMD-SHELL", "curl --fail --silent --show-error --max-time 5 http://localhost:9998/ || exit 1"]
20+
test: ["CMD", "curl", "--fail", "--silent", "--show-error", "--max-time", "5", "http://localhost:9998/"]
1921
interval: 120s
2022
timeout: 10s
21-
retries: 1
22-
start_period: 10s
23+
retries: 3
24+
start_period: 30s

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fredy",
3-
"version": "15.1.0",
3+
"version": "15.1.1",
44
"description": "[F]ind [R]eal [E]states [d]amn eas[y].",
55
"scripts": {
66
"prepare": "husky",

0 commit comments

Comments
 (0)