update docker readme

This commit is contained in:
Roger Rutishauser 2025-04-22 23:49:31 +02:00
parent ac2e4ec664
commit 8ca5a7cde7

View File

@ -107,7 +107,7 @@ etc.
Dockerfile documentation: https://docs.docker.com/reference/builder Dockerfile documentation: https://docs.docker.com/reference/builder
## Build Docker image ### Build Docker image
Im Ordner wo das Dockerfile liegt, ausführen: Im Ordner wo das Dockerfile liegt, ausführen:
@ -121,6 +121,83 @@ Um dem Image einen Namen und einen Tag zu geben (1.0 ist im folgenden Beispiel d
docker build -t node-app:1.0 . docker build -t node-app:1.0 .
``` ```
#### Layers
The commands `RUN`, `COPY` and `ADD` may possibly generate a new layer, if the files have changed. Otherwise, the __cached__ layers will be used.
If another image is based on the same base image, like e.g. `node:16:13.0-alpine`, then docker knows it and will not include it again in another image. `docker images` will show the aggregated image size, not the real size. Second image would actually be much less if base image is already in another image.
#### Example
```
# Image to build upon
FROM node:16:13.0-alpine
# create user "node"
USER node
# set working directory
WORKDIR /home/node
# add files that will not change first
# creates new layer if file changes:
ADD --chown=node:node package.json .
# creates new layer if file changes:
ADD --chown=node:node package-lock.json .
# ... so that npm can be installed based on these files
# creates new layer if previous files had changed:
RUN npm install
# each RUN creates new layer, so it migth by good to group them. For example, combine "admin" tools in one RUN and then another RUN for the stuff that is needed for the app.
RUN apk update && \
apk add curl wget
RUN npm install
# and then add the rest of the files, like app.js
# creates new layer if files change:
ADD --chown=node:node . .
# execute node app.js
CMD [ "node", "app.js" ]
```
### Multi stage build
Used for pre-compiled stuff, for example typescript. The following will create 2 images. The second will only install production dependencies.
```
FROM node:16:13.0-alpine AS builder
USER node
WORKDIR /home/node
ADD --chown=node:node package.json .
ADD --chown=node:node package-lock.json .
RUN npm install
ADD --chown=node:node . .
RUN npx tsc
# ---------------------------------------
FROM node:16:13.0-alpine
USER node
WORKDIR /home/node
COPY --from=builder /home/node/package.json ./package.json
COPY --from=builder /home/node/package-lock.json ./package-lock.json
COPY --from=builder /home/node/build ./build
COPY --from=builder /home/node/public ./public
RUN npm install --production
CMD [ "node", "app.js" ]
```
## Run docker image ## Run docker image
Image, which was either pulled from a registry or was built on the system, can be run with: Image, which was either pulled from a registry or was built on the system, can be run with: