fix: rework Dockerfile to reduce image size

This commit is contained in:
nezu 2023-10-17 12:27:12 +02:00
parent d3b69fd97d
commit 6ea4c7f184
2 changed files with 39 additions and 12 deletions

View file

@ -1,7 +1,11 @@
node_modules
.git
.gitignore
.dockerignore
.pre-commit-config.yaml
.prettierrc.json
.env.local
.next
.next
Dockerfile
docker-compose.yml
README.md

View file

@ -1,8 +1,33 @@
FROM node:18-bookworm-slim
FROM node:18-bookworm-slim as base
# build stage
FROM base AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --only=production
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN sed -i "s/images:/output: 'standalone',images:/" next.config.js
RUN npm run build
# run stage
FROM base AS runner
ENV NODE_ENV production
RUN apt-get update && apt-get install -y \
curl git jq jc borgbackup openssh-server sudo cron && \
apt-get upgrade -y && \
curl jq jc borgbackup openssh-server sudo cron && \
apt-get clean && rm -rf /var/lib/apt/lists/*
RUN echo "borgwarehouse ALL=(ALL) NOPASSWD: /usr/sbin/service ssh restart" >> /etc/sudoers
@ -17,18 +42,16 @@ RUN cp /etc/ssh/sshd_config /etc/ssh/moduli /home/borgwarehouse/
WORKDIR /home/borgwarehouse/app
COPY . .
RUN chown -R borgwarehouse:borgwarehouse * .*
COPY --from=builder --chown=borgwarehouse:borgwarehouse /app/docker-bw-init.sh /app/LICENSE ./
COPY --from=builder --chown=borgwarehouse:borgwarehouse /app/helpers/shells ./helpers/shells
COPY --from=builder --chown=borgwarehouse:borgwarehouse /app/.next/standalone ./
COPY --from=builder --chown=borgwarehouse:borgwarehouse /app/public ./public
COPY --from=builder --chown=borgwarehouse:borgwarehouse /app/.next/static ./.next/static
USER borgwarehouse
RUN npm ci --only=production
RUN npm run build
EXPOSE 3000 22
ENTRYPOINT ["./docker-bw-init.sh"]
CMD ["npm", "run", "start"]
CMD ["node", "server.js"]