Skip to content

Commit

Permalink
Merge pull request nodejs#1771 from zsolt-dev/zsolt-dev-patch-1
Browse files Browse the repository at this point in the history
Add an example of how to create smaller images without npm/yarn
  • Loading branch information
LaurentGoderre authored Oct 18, 2023
2 parents bdf5edb + 647fbaa commit c7cf5c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions docs/BestPractices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand Down Expand Up @@ -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"]
```


0 comments on commit c7cf5c2

Please sign in to comment.