Merge pull request #1771 from zsolt-dev/zsolt-dev-patch-1

Add an example of how to create smaller images without npm/yarn
This commit is contained in:
Laurent Goderre 2023-10-18 16:44:31 -04:00 committed by GitHub
commit c7cf5c2910
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -193,6 +193,8 @@ image as a base, add the things you need in your own Dockerfile
(see the [`alpine` image description](https://hub.docker.com/_/alpine/) for
examples of how to install packages if you are unfamiliar).
To make the image size even smaller, you can [bundle without npm/yarn](./docs/BestPractices.md#smaller-images-without-npmyarn).
### `node:buster`
This image is based on version 10 of

View File

@ -16,6 +16,7 @@
- [Docker Run](#docker-run)
- [Security](#security)
- [node-gyp alpine](#node-gyp-alpine)
- [Smaller images without npm/yarn](#smaller-images-without-npmyarn)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
@ -205,3 +206,39 @@ FROM node:alpine as app
## Copy built node modules and binaries without including the toolchain
COPY --from=builder node_modules .
```
## Smaller images without npm/yarn
If you want to achieve an even smaller image size than the `-alpine`, you can omit the npm/yarn like this:
```Dockerfile
ARG ALPINE_VERSION=3.16
FROM node:18-alpine${ALPINE_VERSION} AS builder
WORKDIR /build-stage
COPY package*.json ./
RUN npm ci
# Copy the the files you need
COPY . ./
RUN npm run build
FROM alpine:${ALPINE_VERSION}
# Create app directory
WORKDIR /usr/src/app
# Add required binaries
RUN apk add --no-cache libstdc++ dumb-init \
&& addgroup -g 1000 node && adduser -u 1000 -G node -s /bin/sh -D node \
&& chown node:node ./
COPY --from=builder /usr/local/bin/node /usr/local/bin/
COPY --from=builder /usr/local/bin/docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
USER node
# Update the following COPY lines based on your codebase
COPY --from=builder /build-stage/node_modules ./node_modules
COPY --from=builder /build-stage/dist ./dist
# Run with dumb-init to not start node with PID=1, since Node.js was not designed to run as PID 1
CMD ["dumb-init", "node", "dist/index.js"]
```