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

👷 build: optimize image size under glibc env #4025

Merged
merged 37 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3ecc15c
👷 build: optimize image size under `glibc` env
hezhijie0327 Sep 19, 2024
35ecda3
Merge branch 'main' into dockerfile
hezhijie0327 Sep 19, 2024
1641538
👷 build: add `startServer.js` as server launcher
hezhijie0327 Sep 19, 2024
f5419df
🐛 fix: `PROXY_URL` missing
hezhijie0327 Sep 19, 2024
63d837b
🔨 chore: exit if DB migration failed
hezhijie0327 Sep 19, 2024
0ca15f9
🔨 chore: allow resolve ipv6 address
hezhijie0327 Sep 19, 2024
28f1823
🔨 chore: rollback changes, cleanup code
hezhijie0327 Sep 19, 2024
44d09b8
🔨 chore: improve console log
hezhijie0327 Sep 19, 2024
fb2434b
🔨 chore: improve error print
hezhijie0327 Sep 19, 2024
0e914da
👷 build: add `isValidSSL` function to check SSL cert
hezhijie0327 Sep 19, 2024
ee775ce
🔨 chore: handle `CERT_HAS_EXPIRED` error
hezhijie0327 Sep 19, 2024
1e2e682
🔨 chore: cleanup code
hezhijie0327 Sep 19, 2024
48c9bee
🔨 chore: improve console log
hezhijie0327 Sep 19, 2024
de68ab4
👷 build: check oss & auth issuer ssl connection before running
hezhijie0327 Sep 19, 2024
513dd48
🔨 chore: improve console log
hezhijie0327 Sep 19, 2024
abe57cb
🔨 chore: change `SSL` to `TLS`
hezhijie0327 Sep 19, 2024
5d6559a
🐛 fix: fix `443` port not display in logs
hezhijie0327 Sep 19, 2024
c9aebf7
🔨 chore: improve console output
hezhijie0327 Sep 19, 2024
a640258
Merge branch 'main' into dockerfile
hezhijie0327 Sep 19, 2024
73a6232
🐛 fix: fix error catch
hezhijie0327 Sep 19, 2024
83fa139
🔨 chore: handle corner case
hezhijie0327 Sep 19, 2024
e5b774a
👷 build: support self-signed SSL cert, switch to system-wide CA cert
hezhijie0327 Sep 19, 2024
ddd37ce
🔨 chore: handle `UNABLE_TO_GET_ISSUER_CERT_LOCALLY` error
hezhijie0327 Sep 20, 2024
7bd0a95
🔨 chore: handle when `*_ISSUER` not existed
hezhijie0327 Sep 20, 2024
689b7e9
🔨 chore: handle non-https protocol, skip TLS checking
hezhijie0327 Sep 20, 2024
5fcf2d9
🔨 chore: improve console log
hezhijie0327 Sep 20, 2024
fac469d
🐛 fix: fix proxychains logs not available when host is ip addr
hezhijie0327 Sep 20, 2024
3e1a94c
👷 build: add DNS server self-check support, split DNS resolve function
hezhijie0327 Sep 22, 2024
f5f1858
🔨 chore: improve console.log
hezhijie0327 Sep 22, 2024
dc202ee
🔨 chore: print DNS server before db migration
hezhijie0327 Sep 22, 2024
32800ea
🔨 chore: update `isValidIP` & `resolveHostIP` function, ready for IPv6
hezhijie0327 Sep 22, 2024
d4b1d72
🐛 fix: fix error handle
hezhijie0327 Sep 22, 2024
e89929c
Merge branch 'main' into dockerfile
arvinxx Sep 24, 2024
dc8334a
Merge branch 'lobehub:main' into dockerfile
hezhijie0327 Sep 25, 2024
9e80f40
👷 build: set `ENTRYPOINT` to `/bin/node`
hezhijie0327 Sep 25, 2024
cdf934f
👷 build: set full path for `proxychains` & `node`, ready for distroless
hezhijie0327 Sep 25, 2024
deb3ea8
👷 build: pin node LTS version to 20
hezhijie0327 Sep 26, 2024
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
85 changes: 36 additions & 49 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Base image for all the stages
## Base image for all building stages
FROM node:20-slim AS base

ARG USE_CN_MIRROR
Expand All @@ -10,19 +10,22 @@ RUN \
if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \
sed -i "s/deb.debian.org/mirrors.ustc.edu.cn/g" "/etc/apt/sources.list.d/debian.sources"; \
fi \
# Add required package & update base package
# Add required package
&& apt update \
&& apt install busybox proxychains-ng -qy \
&& apt full-upgrade -qy \
&& apt autoremove -qy --purge \
&& apt clean -qy \
# Configure BusyBox
&& busybox --install -s \
# Add nextjs:nodejs to run the app
&& addgroup --system --gid 1001 nodejs \
&& adduser --system --home "/app" --gid 1001 -uid 1001 nextjs \
# Set permission for nextjs:nodejs
&& chown -R nextjs:nodejs "/etc/proxychains4.conf" \
&& apt install ca-certificates proxychains-ng -qy \
# Prepare required package to distroless
&& mkdir -p /distroless/bin /distroless/etc /distroless/etc/ssl/certs /distroless/lib \
# Copy proxychains to distroless
&& cp /usr/lib/$(arch)-linux-gnu/libproxychains.so.4 /distroless/lib/libproxychains.so.4 \
&& cp /usr/lib/$(arch)-linux-gnu/libdl.so.2 /distroless/lib/libdl.so.2 \
&& cp /usr/bin/proxychains4 /distroless/bin/proxychains \
&& cp /etc/proxychains4.conf /distroless/etc/proxychains4.conf \
# Copy node to distroless
&& cp /usr/lib/$(arch)-linux-gnu/libstdc++.so.6 /distroless/lib/libstdc++.so.6 \
&& cp /usr/lib/$(arch)-linux-gnu/libgcc_s.so.1 /distroless/lib/libgcc_s.so.1 \
&& cp /usr/local/bin/node /distroless/bin/node \
# Copy CA certificates to distroless
&& cp /etc/ssl/certs/ca-certificates.crt /distroless/etc/ssl/certs/ca-certificates.crt \
# Cleanup temp files
&& rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/*

Expand Down Expand Up @@ -80,7 +83,9 @@ COPY . .
RUN npm run build:docker

## Application image, copy all the files for production
FROM scratch AS app
FROM busybox:latest AS app

COPY --from=base /distroless/ /

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

Expand All @@ -90,13 +95,25 @@ COPY --from=builder /app/.next/standalone /app/
COPY --from=builder /app/.next/static /app/.next/static
COPY --from=builder /deps/node_modules/.pnpm /app/node_modules/.pnpm

# Copy server launcher
COPY --from=builder /app/scripts/serverLauncher/startServer.js /app/startServer.js

RUN \
# Add nextjs:nodejs to run the app
addgroup -S -g 1001 nodejs \
&& adduser -D -G nodejs -H -S -h /app -u 1001 nextjs \
# Set permission for nextjs:nodejs
&& chown -R nextjs:nodejs /app /etc/proxychains4.conf

## Production image, copy all the files and run next
FROM base
FROM scratch

# Copy all the files from app, set the correct permission for prerender cache
COPY --from=app --chown=nextjs:nodejs /app /app
COPY --from=app / /

ENV NODE_ENV="production" \
NODE_OPTIONS="--use-openssl-ca" \
NODE_EXTRA_CA_CERTS="/etc/ssl/certs/ca-certificates.crt"
NODE_TLS_REJECT_UNAUTHORIZED=""

# set hostname to localhost
Expand Down Expand Up @@ -174,36 +191,6 @@ USER nextjs

EXPOSE 3210/tcp

CMD \
if [ -n "$PROXY_URL" ]; then \
# Set regex for IPv4
IP_REGEX="^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$"; \
# Set proxychains command
PROXYCHAINS="proxychains -q"; \
# Parse the proxy URL
host_with_port="${PROXY_URL#*//}"; \
host="${host_with_port%%:*}"; \
port="${PROXY_URL##*:}"; \
protocol="${PROXY_URL%%://*}"; \
# Resolve to IP address if the host is a domain
if ! [[ "$host" =~ "$IP_REGEX" ]]; then \
nslookup=$(nslookup -q="A" "$host" | tail -n +3 | grep 'Address:'); \
if [ -n "$nslookup" ]; then \
host=$(echo "$nslookup" | tail -n 1 | awk '{print $2}'); \
fi; \
fi; \
# Generate proxychains configuration file
printf "%s\n" \
'localnet 127.0.0.0/255.0.0.0' \
'localnet ::1/128' \
'proxy_dns' \
'remote_dns_subnet 224' \
'strict_chain' \
'tcp_connect_time_out 8000' \
'tcp_read_time_out 15000' \
'[ProxyList]' \
"$protocol $host $port" \
> "/etc/proxychains4.conf"; \
fi; \
# Run the server
${PROXYCHAINS} node "/app/server.js";
ENTRYPOINT ["/bin/node"]

CMD ["/app/startServer.js"]
89 changes: 36 additions & 53 deletions Dockerfile.database
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Base image for all the stages
## Base image for all building stages
FROM node:20-slim AS base

ARG USE_CN_MIRROR
Expand All @@ -10,19 +10,22 @@ RUN \
if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \
sed -i "s/deb.debian.org/mirrors.ustc.edu.cn/g" "/etc/apt/sources.list.d/debian.sources"; \
fi \
# Add required package & update base package
# Add required package
&& apt update \
&& apt install busybox proxychains-ng -qy \
&& apt full-upgrade -qy \
&& apt autoremove -qy --purge \
&& apt clean -qy \
# Configure BusyBox
&& busybox --install -s \
# Add nextjs:nodejs to run the app
&& addgroup --system --gid 1001 nodejs \
&& adduser --system --home "/app" --gid 1001 -uid 1001 nextjs \
# Set permission for nextjs:nodejs
&& chown -R nextjs:nodejs "/etc/proxychains4.conf" \
&& apt install ca-certificates proxychains-ng -qy \
# Prepare required package to distroless
&& mkdir -p /distroless/bin /distroless/etc /distroless/etc/ssl/certs /distroless/lib \
# Copy proxychains to distroless
&& cp /usr/lib/$(arch)-linux-gnu/libproxychains.so.4 /distroless/lib/libproxychains.so.4 \
&& cp /usr/lib/$(arch)-linux-gnu/libdl.so.2 /distroless/lib/libdl.so.2 \
&& cp /usr/bin/proxychains4 /distroless/bin/proxychains \
&& cp /etc/proxychains4.conf /distroless/etc/proxychains4.conf \
# Copy node to distroless
&& cp /usr/lib/$(arch)-linux-gnu/libstdc++.so.6 /distroless/lib/libstdc++.so.6 \
&& cp /usr/lib/$(arch)-linux-gnu/libgcc_s.so.1 /distroless/lib/libgcc_s.so.1 \
&& cp /usr/local/bin/node /distroless/bin/node \
# Copy CA certificates to distroless
&& cp /etc/ssl/certs/ca-certificates.crt /distroless/etc/ssl/certs/ca-certificates.crt \
# Cleanup temp files
&& rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/*

Expand Down Expand Up @@ -84,7 +87,9 @@ COPY . .
RUN npm run build:docker

## Application image, copy all the files for production
FROM scratch AS app
FROM busybox:latest AS app

COPY --from=base /distroless/ /

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

Expand All @@ -103,13 +108,25 @@ COPY --from=builder /app/src/database/server/migrations /app/migrations
COPY --from=builder /app/scripts/migrateServerDB/docker.cjs /app/docker.cjs
COPY --from=builder /app/scripts/migrateServerDB/errorHint.js /app/errorHint.js

# Copy server launcher
COPY --from=builder /app/scripts/serverLauncher/startServer.js /app/startServer.js

RUN \
# Add nextjs:nodejs to run the app
addgroup -S -g 1001 nodejs \
&& adduser -D -G nodejs -H -S -h /app -u 1001 nextjs \
# Set permission for nextjs:nodejs
&& chown -R nextjs:nodejs /app /etc/proxychains4.conf

## Production image, copy all the files and run next
FROM base
FROM scratch

# Copy all the files from app, set the correct permission for prerender cache
COPY --from=app --chown=nextjs:nodejs /app /app
COPY --from=app / /

ENV NODE_ENV="production" \
NODE_OPTIONS="--use-openssl-ca" \
NODE_EXTRA_CA_CERTS="/etc/ssl/certs/ca-certificates.crt"
NODE_TLS_REJECT_UNAUTHORIZED=""

# set hostname to localhost
Expand Down Expand Up @@ -206,40 +223,6 @@ USER nextjs

EXPOSE 3210/tcp

CMD \
if [ -n "$PROXY_URL" ]; then \
# Set regex for IPv4
IP_REGEX="^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$"; \
# Set proxychains command
PROXYCHAINS="proxychains -q"; \
# Parse the proxy URL
host_with_port="${PROXY_URL#*//}"; \
host="${host_with_port%%:*}"; \
port="${PROXY_URL##*:}"; \
protocol="${PROXY_URL%%://*}"; \
# Resolve to IP address if the host is a domain
if ! [[ "$host" =~ "$IP_REGEX" ]]; then \
nslookup=$(nslookup -q="A" "$host" | tail -n +3 | grep 'Address:'); \
if [ -n "$nslookup" ]; then \
host=$(echo "$nslookup" | tail -n 1 | awk '{print $2}'); \
fi; \
fi; \
# Generate proxychains configuration file
printf "%s\n" \
'localnet 127.0.0.0/255.0.0.0' \
'localnet ::1/128' \
'proxy_dns' \
'remote_dns_subnet 224' \
'strict_chain' \
'tcp_connect_time_out 8000' \
'tcp_read_time_out 15000' \
'[ProxyList]' \
"$protocol $host $port" \
> "/etc/proxychains4.conf"; \
fi; \
# Run migration
node "/app/docker.cjs"; \
if [ "$?" -eq "0" ]; then \
# Run the server
${PROXYCHAINS} node "/app/server.js"; \
fi;
ENTRYPOINT ["/bin/node"]

CMD ["/app/startServer.js"]
Loading
Loading