loopbackio / loopbackio/loopback-next

Dockerfile improvements

Offen
#10,326 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Build developer-experience feature
Vorherrschende Sprache
TypeScript
Sterne
5.1k
Forks
1.1k
Ø Merge
2 T. 21 Std.
Gemergte PRs (30 T.)
27

Beschreibung

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:

  1. The Dockerfile uses npm install, which is great for local development, but I think it should be npm ci for deployments. npm ci --only=production would be even better, but that won't work without additional changes, because of my next point.
  2. 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. Changing npm install to npm ci --only=production won'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.
  3. The Dockerfile does the build at runtime, so every time the application is run, it calls npm start, which calls prestart, which calls rebuild, which calls build (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
  1. Generate a new application
  2. CD to the directory
  3. Build the Dockerfile and observe the size
  4. Change npm install to npm ci --only=production in Dockerfile
  5. Build an image using the Dockerfile and observe that the build fails

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Finde den LoopBack-Scaffolding-Einstiegspunkt oder das Template, das die Dockerfile generiert, und reproduziere anschließend den im Issue beschriebenen Build des generierten Images. Überprüfe das vorgeschlagene Verhalten für lokal versus Produktion und validiere die resultierende Image-Größe und den Startpfad; fertig ist die Aufgabe, wenn die beabsichtigten Deployment-Anweisungen oder Dockerfile-Änderungen durch die relevanten Scaffolding-Tests abgedeckt sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
docker, node.js, typescript
Bereich
build-system, devops
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.