Skip to content

Docker 镜像

构建, 标签, and 推送 Docker 镜像 using layers, registries, and 最佳实践.

你将学到

  • 如何 Docker 镜像是 构建从 layers
  • 构建镜像使用 docker 构建
  • Tagging and pushing 镜像到 registries
  • Optimizing 镜像使用 multi-stage 构建

概念讲解

镜像层

A Docker image is built up from a series of layers. Each instruction in a Dockerfile — FROM, RUN, COPY, ADD — creates a new layer. Layers are cached, so if a layer hasn't changed, Docker reuses it on the next build. This makes rebuilds fast. Layers are also shared between images, saving disk space. Understanding layers is key to writing efficient Dockerfiles: you want to put rarely-changing instructions first and frequently-changing ones last to maximize cache hits.

Building Images

The docker 构建 command reads a Dockerfile and produces an image. You specify a context (the directory Docker can access during the build) and optionally a tag. For example, docker 构建 -t myapp:1.0 . builds the Dockerfile in the current directory and tags the result as myapp:1.0. The .dockerignore file excludes files from the context, similar to .gitignore, which speeds up builds and prevents secrets from being copied into the image.

Tagging and Registries

A tag is a label for an image, typically name:version like myapp:1.0 or myapp:latest. A registry is a storage service for images — Docker Hub is the public default, but teams use private registries like Amazon ECR or GitHub Container Registry. You push images with docker 推送 and pull them with docker 拉取. Tagging images with semantic versions instead of just latest makes deployments reproducible and rollbacks easy.

Multi-Stage Builds

Multi-stage builds use multiple FROM instructions in a single Dockerfile. You build your application in a first stage with all build tools, then copy only the compiled output into a smaller final image. This dramatically reduces the final image size — often from hundreds of megabytes to tens — by excluding compilers, source code, and intermediate files from the production image.

Image Security

Scan images for vulnerabilities with tools like Trivy or Docker Scout. Use distroless base images that contain only your application and its runtime — no shell, no package manager, no extra binaries. This dramatically reduces the attack surface. Sign images with Cosign to verify their integrity in production. Pull images by digest (@sha256:...) instead of tag for maximum reproducibility — a tag can be reassigned, but a digest is immutable and always points to the exact same image layers.

示例代码

              # Build an image from a Dockerfile
docker build -t myapp:1.0 .
docker build -t myapp:1.0 -f Dockerfile.prod .   # custom Dockerfile

# Tag and push to a registry
docker tag myapp:1.0 registry.example.com/myapp:1.0
docker push registry.example.com/myapp:1.0

# Inspect image layers and size
docker image history myapp:1.0      # show layer history
docker images myapp                 # list image variants

# Multi-stage build (in Dockerfile)
# FROM node:20 AS builder
# COPY . . && RUN npm run build
# FROM nginx:alpine
# COPY --from=builder /dist /usr/share/nginx/html
            

docker 构建创建镜像, docker 标签 and docker 推送 publish 它们到 registries, and multi-stage 构建 reduce 最终镜像大小通过排除构建 tools.

动手试一试

常见错误

错误写法

Using :latest 标签 in production

正确做法

标签镜像使用特定 versions 如 myapp:1.2.3. :latest 标签是 可变 and 能点 到不同镜像 over 时间, 制作部署非-reproducible.

错误写法

Putting COPY . . 之前 RUN npm 安装

正确做法

顺序 Dockerfile instructions 从最少到 大多数频繁地更改. 复制包.JSON 首先, 安装 dependencies, 然后复制源 code. 这个 maximizes layer 缓存 hits.

练习 Docker 镜像

4 个练习

练习 Docker 镜像

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误