loopbackio / loopbackio/loopback-next
Dockerfile improvements
まだ誰も着手していません。
- 主要言語
- TypeScript
- スター
- 5.1k
- フォーク
- 1.1k
- 平均マージ
- 2日 21時間
- マージ済み PR(30日)
- 27
説明
Describe the bug
Since the loopback scaffolding automatically generates a Dockerfile, I've noticed a lot of developers in our organisation assume that this Dockerfile is ready out-of-the-box for production deployments.
Is the generated Dockerfile meant for local developer use? In this case, I think it might be good to mention this somewhere (maybe in the Dockerfile itself) and that it needs some adjustments to be production ready.
Alternatively, I would suggest these changes:
- The Dockerfile uses
npm install, which is great for local development, but I think it should benpm cifor deployments.npm ci --only=productionwould be even better, but that won't work without additional changes, because of my next point. - The built Dockerfile includes devDependencies. If I build it as-is (e.g.
docker build .), the image size is 393MB because it includes all of the original TypeScript, the transpiled JavaScript, dependencies, and devDependencies. Changingnpm installtonpm ci --only=productionwon't work in a single-stage build because those devDependencies are needed to transpile the TypeScript. By using a multi-stage build, this allows only including the needed files, which reduces the image size to 277MB, and should have a positive impact on storage, deployment times, application startup times, etc. - The Dockerfile does the build at runtime, so every time the application is run, it calls
npm start, which callsprestart, which callsrebuild, which callsbuild(and so on). The build should be done when the container is built, so when the container is run, all it has to do is run the code.
I'm not an expert, but here's a Dockerfile that uses a mutli-stage build to fix all of the above issues:
# Check out https://hub.docker.com/_/node to select a new base image
FROM docker.io/library/node:18-slim as builder
# Set to a non-root built-in user `node`
USER node
# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY --chown=node package*.json ./
RUN npm ci
# Bundle app source code
COPY --chown=node . .
RUN npm run build
FROM docker.io/library/node:18-slim
USER node
RUN mkdir -p /home/node/app
WORKDIR /home/node/app
COPY --chown=node --from=builder /home/node/app/dist dist
COPY --chown=node --from=builder /home/node/app/package*.json ./
RUN npm ci --only=production
# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000
EXPOSE ${PORT}
CMD [ "node", "." ]
Logs
No response
Additional information
No response
Reproduction
- Generate a new application
- CD to the directory
- Build the Dockerfile and observe the size
- Change
npm installtonpm ci --only=productionin Dockerfile - Build an image using the Dockerfile and observe that the build fails
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
LoopBack の scaffolding のエントリーポイント、または Dockerfile を生成するテンプレートを見つけ、その後、issue に記載された生成イメージの build を再現します。提案されているローカルと本番の動作を確認し、生成されたイメージのサイズと起動パスを検証します。関連する scaffolding テストで、意図したデプロイ手順または Dockerfile の変更がカバーされれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- docker, node.js, typescript
- 領域
- build-system, devops
- issue の種類
- 機能追加
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100