merge: Smaller container builds (#214)

This commit is contained in:
Marie 2023-12-08 01:44:56 +01:00 committed by GitHub
commit 22d1aa5df7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 81 deletions

View file

@ -1,101 +1,57 @@
# syntax = docker/dockerfile:1.4 # syntax = docker/dockerfile:1.4
ARG NODE_VERSION=20.5.1-bullseye ARG NODE_VERSION=21.4.0-alpine3.18
# build assets & compile TypeScript FROM node:${NODE_VERSION} as build
FROM --platform=$BUILDPLATFORM node:${NODE_VERSION} AS native-builder
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
rm -f /etc/apt/apt.conf.d/docker-clean \
; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache \
&& apt-get update \
&& apt-get install -yqq --no-install-recommends \
build-essential curl ca-certificates
ARG TARGETARCH
RUN curl -L https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-$TARGETARCH-static.tar.xz -o /ffmpeg.tar.xz \
&& tar xvf /ffmpeg.tar.xz -C / --strip-components 1 --wildcards 'ffmpeg-*-static/ffmpeg' 'ffmpeg-*-static/ffprobe'
RUN corepack enable RUN corepack enable
WORKDIR /sharkey WORKDIR /outpatient
COPY --link ["pnpm-lock.yaml", "pnpm-workspace.yaml", "package.json", "./"] RUN apk add git
COPY --link ["scripts", "./scripts"]
COPY --link ["packages/megalodon/package.json", "./packages/megalodon/"]
COPY --link ["packages/backend/package.json", "./packages/backend/"]
COPY --link ["packages/frontend/package.json", "./packages/frontend/"]
COPY --link ["packages/sw/package.json", "./packages/sw/"]
COPY --link ["packages/misskey-js/package.json", "./packages/misskey-js/"]
COPY . ./
RUN git submodule update
RUN pnpm config set fetch-retries 5
RUN --mount=type=cache,target=/root/.local/share/pnpm/store,sharing=locked \ RUN --mount=type=cache,target=/root/.local/share/pnpm/store,sharing=locked \
pnpm i --frozen-lockfile --aggregate-output pnpm i
COPY --link . ./
ARG NODE_ENV=production
RUN git submodule update --init
RUN pnpm build RUN pnpm build
RUN rm -rf .git/ RUN node scripts/trim-deps.mjs
RUN mv packages/frontend/assets sharkey-assets
# build native dependencies for target platform RUN rm -r node_modules packages/frontend packages/sw
FROM --platform=$TARGETPLATFORM node:${NODE_VERSION} AS target-builder
RUN apt-get update \
&& apt-get install -yqq --no-install-recommends \
build-essential
RUN corepack enable
WORKDIR /sharkey
COPY --link ["pnpm-lock.yaml", "pnpm-workspace.yaml", "package.json", "./"]
COPY --link ["scripts", "./scripts"]
COPY --link ["packages/megalodon/package.json", "./packages/megalodon/"]
COPY --link ["packages/backend/package.json", "./packages/backend/"]
RUN --mount=type=cache,target=/root/.local/share/pnpm/store,sharing=locked \ RUN --mount=type=cache,target=/root/.local/share/pnpm/store,sharing=locked \
pnpm i --frozen-lockfile --aggregate-output pnpm i --prod
RUN rm -rf .git
FROM --platform=$TARGETPLATFORM node:${NODE_VERSION}-slim AS runner FROM node:${NODE_VERSION}
ARG UID="991" WORKDIR /outpatient
ARG GID="991"
RUN apt-get update \ RUN apk add ffmpeg tini
&& apt-get install -y --no-install-recommends \
tini curl libjemalloc-dev libjemalloc2 \
&& ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so \
&& corepack enable \
&& groupadd -g "${GID}" sharkey \
&& useradd -l -u "${UID}" -g "${GID}" -m -d /sharkey sharkey \
&& find / -type d -path /proc -prune -o -type f -perm /u+s -ignore_readdir_race -exec chmod u-s {} \; \
&& find / -type d -path /proc -prune -o -type f -perm /g+s -ignore_readdir_race -exec chmod g-s {} \; \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists
USER sharkey COPY --from=build /outpatient/built ./built
WORKDIR /sharkey COPY --from=build /outpatient/node_modules ./node_modules
COPY --from=build /outpatient/packages/backend/built ./packages/backend/built
COPY --from=build /outpatient/packages/backend/node_modules ./packages/backend/node_modules
COPY --from=build /outpatient/packages/megalodon/lib ./packages/megalodon/lib
COPY --from=build /outpatient/packages/megalodon/node_modules ./packages/megalodon/node_modules
COPY --from=build /outpatient/packages/misskey-js/built ./packages/misskey-js/built
COPY --from=build /outpatient/packages/misskey-js/node_modules ./packages/misskey-js/node_modules
COPY --from=build /outpatient/fluent-emojis ./fluent-emojis
COPY --from=build /outpatient/sharkey-assets ./packages/frontend/assets
COPY --chown=sharkey:sharkey --from=target-builder /sharkey/node_modules ./node_modules COPY package.json ./package.json
COPY --chown=sharkey:sharkey --from=target-builder /sharkey/packages/megalodon/node_modules ./packages/megalodon/node_modules COPY pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --chown=sharkey:sharkey --from=target-builder /sharkey/packages/backend/node_modules ./packages/backend/node_modules COPY packages/backend/package.json ./packages/backend/package.json
COPY --chown=sharkey:sharkey --from=native-builder /ffmpeg /usr/local/bin/ COPY packages/backend/check_connect.js ./packages/backend/check_connect.js
COPY --chown=sharkey:sharkey --from=native-builder /ffprobe /usr/local/bin/ COPY packages/backend/ormconfig.js ./packages/backend/ormconfig.js
COPY --chown=sharkey:sharkey --from=native-builder /sharkey/built ./built COPY packages/backend/migration ./packages/backend/migration
COPY --chown=sharkey:sharkey --from=native-builder /sharkey/packages/megalodon/lib ./packages/megalodon/lib COPY packages/backend/assets ./packages/backend/assets
COPY --chown=sharkey:sharkey --from=native-builder /sharkey/packages/backend/built ./packages/backend/built COPY packages/megalodon/package.json ./packages/megalodon/package.json
COPY --chown=sharkey:sharkey --from=native-builder /sharkey/fluent-emojis /sharkey/fluent-emojis COPY packages/misskey-js/package.json ./packages/misskey-js/package.json
COPY --chown=sharkey:sharkey . ./
ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so
ENV NODE_ENV=production ENV NODE_ENV=production
VOLUME "/sharkey/files" RUN corepack enable
HEALTHCHECK --interval=5s --retries=20 CMD ["/bin/bash", "/sharkey/healthcheck.sh"]
ENTRYPOINT ["/usr/bin/tini", "--"] ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["pnpm", "run", "migrateandstart"] CMD ["pnpm", "run", "migrateandstart"]

35
scripts/trim-deps.mjs Normal file
View file

@ -0,0 +1,35 @@
// trims dependencies for production
// only run after a full build
import fs from 'node:fs'
const checks = ['dependencies', 'optionalDependencies', 'devDependencies']
function removeDeps(path, patterns) {
let pkg = JSON.parse(fs.readFileSync(path));
for (const pattern of patterns) {
if (typeof pattern === 'string') {
for (const check of checks) {
if (pkg[check] !== undefined && pkg[check][pattern] !== undefined) {
delete pkg[check][pattern];
}
}
} else if (pattern instanceof RegExp) {
for (const check of checks) {
if (pkg[check] !== undefined) {
for (const dep of Object.keys(pkg[check])) {
if (pattern.exec(dep) !== null) {
delete pkg[check][dep];
}
}
}
}
}
}
fs.writeFileSync(path, JSON.stringify(pkg, undefined, 2));
}
removeDeps('package.json', ['execa', 'cssnano', 'postcss', 'terser', 'typescript'])
removeDeps('packages/backend/package.json', ['bufferutil', 'utf-8-validate', /^@swc\//, 'typescript'])
removeDeps('packages/megalodon/package.json', [/^@types\//, 'typescript'])
removeDeps('packages/misskey-js/package.json', [/^@swc\//, 'typescript'])