Skip to content

Commit c7cf5c2

Browse files
Merge pull request #1771 from zsolt-dev/zsolt-dev-patch-1
Add an example of how to create smaller images without npm/yarn
2 parents bdf5edb + 647fbaa commit c7cf5c2

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ image as a base, add the things you need in your own Dockerfile
193193
(see the [`alpine` image description](https://hub.docker.com/_/alpine/) for
194194
examples of how to install packages if you are unfamiliar).
195195

196+
To make the image size even smaller, you can [bundle without npm/yarn](./docs/BestPractices.md#smaller-images-without-npmyarn).
197+
196198
### `node:buster`
197199

198200
This image is based on version 10 of

docs/BestPractices.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Docker Run](#docker-run)
1717
- [Security](#security)
1818
- [node-gyp alpine](#node-gyp-alpine)
19+
- [Smaller images without npm/yarn](#smaller-images-without-npmyarn)
1920

2021
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2122

@@ -205,3 +206,39 @@ FROM node:alpine as app
205206
## Copy built node modules and binaries without including the toolchain
206207
COPY --from=builder node_modules .
207208
```
209+
210+
211+
## Smaller images without npm/yarn
212+
213+
If you want to achieve an even smaller image size than the `-alpine`, you can omit the npm/yarn like this:
214+
215+
```Dockerfile
216+
ARG ALPINE_VERSION=3.16
217+
218+
FROM node:18-alpine${ALPINE_VERSION} AS builder
219+
WORKDIR /build-stage
220+
COPY package*.json ./
221+
RUN npm ci
222+
# Copy the the files you need
223+
COPY . ./
224+
RUN npm run build
225+
226+
FROM alpine:${ALPINE_VERSION}
227+
# Create app directory
228+
WORKDIR /usr/src/app
229+
# Add required binaries
230+
RUN apk add --no-cache libstdc++ dumb-init \
231+
&& addgroup -g 1000 node && adduser -u 1000 -G node -s /bin/sh -D node \
232+
&& chown node:node ./
233+
COPY --from=builder /usr/local/bin/node /usr/local/bin/
234+
COPY --from=builder /usr/local/bin/docker-entrypoint.sh /usr/local/bin/
235+
ENTRYPOINT ["docker-entrypoint.sh"]
236+
USER node
237+
# Update the following COPY lines based on your codebase
238+
COPY --from=builder /build-stage/node_modules ./node_modules
239+
COPY --from=builder /build-stage/dist ./dist
240+
# Run with dumb-init to not start node with PID=1, since Node.js was not designed to run as PID 1
241+
CMD ["dumb-init", "node", "dist/index.js"]
242+
```
243+
244+

0 commit comments

Comments
 (0)