Skip to content

Commit 9025d87

Browse files
authored
Add an example of a smaller alpine image without npm/yarn
1 parent 209f981 commit 9025d87

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/BestPractices.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,42 @@ FROM node:alpine as app
204204
## Copy built node modules and binaries without including the toolchain
205205
COPY --from=builder node_modules .
206206
```
207+
208+
209+
## Smaller images without npm/yarn
210+
211+
If you want to achieve an even smaller image size than the `-alpine`, you can omit the npm/yarn like this:
212+
213+
```Dockerfile
214+
FROM node:18-alpine3.16 AS builder
215+
WORKDIR /build-stage
216+
COPY package*.json ./
217+
RUN npm ci
218+
219+
# Copy the the files you need
220+
COPY . ./
221+
222+
RUN npm run build
223+
224+
225+
# Make sure the alpine version is the same as in the build stage
226+
FROM alpine:3.16
227+
RUN apk add --no-cache libstdc++
228+
RUN apk add --no-cache dumb-init
229+
RUN addgroup -g 1000 node && adduser -u 1000 -G node -s /bin/sh -D node
230+
231+
COPY --from=builder /usr/local/bin/node /usr/local/bin/
232+
COPY --from=builder /usr/local/bin/docker-entrypoint.sh /usr/local/bin/
233+
ENTRYPOINT ["docker-entrypoint.sh"]
234+
235+
# Update the following lines based on your codebase
236+
COPY --from=builder /build-stage/node_modules ./node_modules
237+
COPY --from=builder /build-stage/dist ./dist
238+
239+
RUN chown -R node:node ./
240+
USER node
241+
# Run with dumb-init to not start node with PID=1, since Node.js was not designed to run as PID 1
242+
CMD ["dumb-init", "node", "dist/index.js"]
243+
```
244+
245+

0 commit comments

Comments
 (0)