|
16 | 16 | - [Docker Run](#docker-run)
|
17 | 17 | - [Security](#security)
|
18 | 18 | - [node-gyp alpine](#node-gyp-alpine)
|
| 19 | +- [Smaller images without npm/yarn](#smaller-images-without-npmyarn) |
19 | 20 |
|
20 | 21 | <!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
21 | 22 |
|
@@ -205,3 +206,39 @@ FROM node:alpine as app
|
205 | 206 | ## Copy built node modules and binaries without including the toolchain
|
206 | 207 | COPY --from=builder node_modules .
|
207 | 208 | ```
|
| 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