Inżynieria struktury and Design
Przewodnik krok po kroku do budowy wieloetapowych zdjęć docker
Table of Contents
Co to jest?
Wielostakowe budynki Docker są wyposażone w jeden system dockerfiles. Each Files; Each Files; Each Files; Equant 1; FLT: 1 Fix3; instruction zaczyna się od nowej sceny, która musi być całkowicie niezastąpiona, a następnie kończy się na kolejnych zdjęciach. Artifacts can by selective cope the from on stage tone to another, which thele final images only retains when it is strictes, and conditions. Artifacts can by select cope applicates.
Te dwa projekty, które tworzą środowisko naturalne, te projekty, które tworzą środowisko naturalne, te projekty, które tworzą środowisko. Ich projekt jest taki sam, że są wykorzystywane do produkcji for. That bloats the image and progreses the attack surface. Multi- stage builds solt them thale thale will bee used a thalk, dicure- rich images for compilation and then copying on y thee result ting artifacts inta minima runtime suche suche ais a thalf; 1I; FLT: 3XD; X3XD; XD; X3XD; XD; XD; XL; X3D; XL; XL; XL; XL; XL; XL; XL; XL; XL; XD; XL; XD; XD; XD; XD; XD; XD; XD; XD; XD; XD; XD; XD; XD; XD
Korzyści Of Multi- Stage Builds
Te zalety są korzystne dla przyjęcia wielostapowych budynków go beyond size reduction. They have a profund impact on security, maintainability, and deployment speed.
1. Reduced Image Size
By discarding build- time dependencies, multi- stage builds often shrink images by 50% t o 90%. For example, a Node.js application built using the full eng1; FLT: 4; FLT: 3; FLT: 4; FLT: 4; FLT: 3XD; FLT: 6; FLT: 3Base. This storage savings directy translates tfar steal times; folder into an 1; FLT: 6; FLT: 3XD; Base. This storave savings diredirecty translates tfar ster pull times, folder work bandwidth, and lower registrs.
2. Improved Security
Every installald package or tool in a contener images is a potential levitability. Multi- stage builds allow you to contrigade compilers, debiggers, and development libraries from the final image, confidently reducing the attack surface. You can even use images like 1; end 1; FLT: 7 contain only the bare minimute te te execute thee application binary.
3. Strumień procesów budowlanych
All build steps are defined in a single Dockerfile, making the process self-contained and esy to o version. CI / CD contactiines benefitif from a single entry point: the Dockerfile. There is no need to o maintain separate built scripts or manual cleanup steps.
4. Wzmocnienie Reproducibility i Konsekwencja
Ponieważ te same warstwy są bardziej szczegółowe i nie są one bardziej wiarygodne niż te, które można znaleźć w innych miejscach.
Building a Multi- Stage Dockerfile: Step by Step
This walktrigh covers the creation of a production-ready multi-stage Dockerfile for a Node.js andReact application. The same principles applicy to o nich compiled language.
1. Staże dla młodych
Before writing code, map out thee stages you need. A typical multi-stage build has at leaast two stages:
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Builder stage Xi1; Xi1; FLT: 1 Xi3; Xi3; - installs all build tools, installs dependencies, andd runs the build commodd.
- W przypadku gdy w wyniku badania nie można określić, czy dane są dostępne, należy podać dane dotyczące wszystkich danych, które należy podać w sprawozdaniu z badania.
For complex projects you might add intermediate states for tests, static analysis, or asset compression.
2. Write the Builder Stage
Rozpocząć with a base image that includes thee requid toolchain. Usie beidid 1; Xi1; FLT: 0 Xi3; Xi3; named stages beif1; Xi1; FLT: 1 Xif3; Xif1; Xif1; FLT: 9 Xif3; Xif3; to reference them later. For Node.js:
FROM node:14-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
Kiełbaski:
- Use presendi1; Presendi1; FLT: 11 presendi3; for determinastic, faster dependency installation.
- Keep is 1; Evil; FLT: 12 Evidence 3; Evidence 3; Commands as separate layers when an possible to o leverage caching.
- Install build tools (like TypeScript, webpack) in this stage only.
3. Add an Intermediate Teszt Stage (Opcja)
Tia stage can thee builder image or install additional tools. Because it it it final stage, tett failures will not be present in thee final image.
FROM builder AS test
RUN npm run test
You can run this stage in your CI injene with entire the entire final image.
4. Definiować te Runtime Stage
For a React application, the runtime image can be an Nginx server. For a backend API, it might be a distroles base image or a minimal Alpine with the Node.js runtime. Copy only the essential artifacts using 1; British 1; FLT: 15 habilit3; British 3;.
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
If you need the Node.js runtime, avoid copying presendi1; If you need the Node.js runtime, avoid copying presendi1; If you need the Node.js runtime, avoid copying presenci1; Iden1; FLT: 17 presendi3; Identi3; from the builder; instead reinstall production dependiencies in the runtime stage:
FROM node:14-alpine AS runtime
WORKDIR /app
COPY --from=builder /app/dist ./dist
RUN npm ci --only=production
EXPOSE 3000
CMD ["node", "dist/server.js"]
5. Build and d Teszt thee Image
Build thee final image using thee standard command:
docker build -t myapp:latest .
To verify thee size, run container andh confirm the application responds correctly:
docker run -d -p 8080:80 myapp:latest
curl http://localhost:8080
Begt Practices for Multi- Stage Builds
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Usie specific base image tags Xi1; Xi1; FLT: 1 Xi3; Xi3; - avoid Xi1; Xi1; FLT: 22 Xi3; Xi3; to prevent surprises. Prefer Xi1; Xi1; FLT: 23 Xi3; Xi3; or Xi1; Xi1; FLT: 24 XI3; XI3;
- Xi1; Xi1; FLT: 0 X3; Xi3; Optimize layer caching gig1; Xi1; FLT: 1 XI3; Xi3; - copy Xi1; Xi1; FLT: 25 XI3; XI3; And Xi1; XI1; FLT: 26 XI3; XI3; FLT: 26 XI3; FLT; before the rest of the source code so that the XI1; XIF: 27 XIF 3; X3; Layer is only invicidated wheren depencies change.
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Leverage buildKit Xi1; Xi1; FLT: 1 Xi3; Xi3; - enable BuildKit with Xi1; Xi1; FLT: 28 Xion3; Xion3; for faster builds, inline caching, and better parallelism.
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Create multiple final stages for different environments Xi1; Xi1; FLT: 1 Xi3; Xi3; - for example, a development stage with debugging tools anda production stage witch a hardened base image.
- Xi1; Xi1; FLT: 0 X3; Xi3; Usie Xi1; Xi1; FLT: 29 Xi3; Xi3; for development builds (Builds): Xi1; Xi1; FLT: 1 XI3; FLT: 1 XI3; FLT: 30 XI3; XI3; stage to get live reload andd source maps, then rebuild with 1; XI1; FLT: 31 XI3; XI3; for production.
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Keep secrets out of images is 1; Xi1; FLT: 1 Xi3; Xi3; - use Docker BuildKit 's Xi1; Xi1; FLT: 32 XI3; Xi3; FLG if you need to o pass credentials during build; never include them in thee Final image.
Common Patterns andUsie Cases
Kompilacja Languages (Go, Rust, C + +)
For static binaries, the runtime stage can use present 1; Xi1; FLT: 33 X3; Xi3; (empty base image). Only the binary ande maybe a configuation file are copied. Example for Go:
FROM golang:1.20-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o myapp .
FROM scratch
COPY --from=builder /app/myapp /myapp
ENTRYPOINT ["/myapp"]
Wnioski o wydanie pitonu
Use a builder stage with amend1; EDI1; FLT: 35 contents 3; EDI3; to install dependencies and compile any C extensions, then copy only the installed packages to a runtime stage:
FROM python:3.11-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
COPY . .
FROM python:3.11-slim
COPY --from=builder /root/.local /root/.local
COPY --from=builder /app /app
ENV PATH=/root/.local/bin:$PATH
CMD ["python", "app.py"]
Frontend with API Servir
Build both frontend and backend in one Dockerfile. Usie separate builder stages for each, then copy both artifacts into a single runtime image:
FROM node:14-alpine AS frontend-builder
WORKDIR /app
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ .
RUN npm run build
FROM node:14-alpine AS api-builder
WORKDIR /app
COPY api/package*.json ./
RUN npm ci
COPY api/ .
RUN npm run build
FROM node:14-alpine
WORKDIR /app
COPY --from=frontend-builder /app/build ./public
COPY --from=api-builder /app/dist ./dist
RUN npm ci --only=production
EXPOSE 3000
CMD ["node", "dist/server.js"]
Troubleshooting Multi- Stage Builds
- Xi1; Xi1; FLT: 0 XI3; XI3; Layer caching nott working Xi1; XI1; FLT: 1 XI3; XI3; - ensure Xi1; XI1; FLT: 38 XI3; XI3; XI3; Commands order dependencies before source ce code. Usie XI1; XI1; FLT: 39 XI3; XI3; TO XIDE Unnecesary files.
- W przypadku gdy w wyniku badania nie można określić, czy dany produkt jest zgodny z wymogami określonymi w pkt 1, należy podać numer identyfikacyjny produktu, który ma zostać wprowadzony do obrotu, oraz podać numer identyfikacyjny produktu.
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Secret cleage Xi1; Xi1; FLT: 1 Xi3; Xi3; - never copy entire directorie that might contain Xi1; Xi1; FLT: 42 XI3; Xi3; or Xi1; XiVE; FLT: 43 Xi3; XiV3. Explicitly copy only needed files.
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Large final images despite multi- stage Xi1; Xi1; FLT: 1 Xi3; Xi3; - check if you are e copying Xi1; Xi1; FLT: 44 Xi3; Xi3; or the entire source. Usie Xi1; Xi1; FLT: 45 Xi3; Xi3; to see layer sizes.
Konkluzja
Wieloetapowe prace Docker są bardzo proste, a także współczesne prace nad tym, jak bardzo jest to możliwe.
For more details, refer te environ1; head1; FLT: 0; FLT: 0; FL3; official Docker multi- stage build documentation presentation 1; FLT: 1; FLT: 3; FLT: and the event 1; FLT: 2; FLT: 3; FLT: 3; FLT: 4; FLT: 3; FLT: 3; FLT: 3; FLT: 3; FLT: 3X.FLT: 3; FLT: 3; FLX: 3X.3; FLT: 3X.3; FLT: 3X.3X.DOmentation; FLX: 1; FLT: 5; FLT: 3; FLT: 3; FL.3.