Skip to content

编写 Dockerfile

创建 efficient Dockerfiles 使用 FROM, RUN, COPY, and 最佳实践 for production.

你将学到

  • 结构 and instructions 的 Dockerfile
  • FROM, RUN, COPY, WORKDIR, and CMD
  • Layer 缓存 and 构建 optimization
  • Security 最佳实践 for Dockerfiles

概念讲解

Dockerfile 结构

Dockerfile 是一种 text file with instructions for building a Docker image. Each instruction is on its own line and creates a new layer. The file starts with a FROM instruction that specifies the base image. Then you add instructions to install dependencies, copy source code, and configure the application. The file ends with CMD or ENTRYPOINT, which defines what runs when the container starts.

Core Instructions

FROM 节点:20-alpine sets the base image. WORKDIR /app sets the working directory for subsequent instructions. COPY 包.JSON . copies files from the build context into the image. RUN npm 安装 executes a shell command during the build. CMD ["节点", "服务器.js"] sets the default command when the container starts. EXPOSE 3000 documents which port the container listens on. ENV NODE_ENV=production sets environment variables in the image.

Layer Caching

Docker caches each layer. If an instruction's inputs haven't changed, Docker reuses the cached layer instead of re-running it. To maximize cache hits, put instructions that change rarely (like installing dependencies) before instructions that change often (like copying source code). A common pattern is to copy 包.JSON and run npm 安装 first, then copy the rest of the source code. This way, changing source code doesn't invalidate the dependency layer.

Security Best Practices

Never run containers as root — create a non-root user with RUN adduser and switch to it with USER. Use minimal base images like alpine or distroless to reduce the attack surface. Don't bake secrets into images — pass them at runtime with environment variables. Use .dockerignore to exclude sensitive files from the build context. Pin base image versions with digests for maximum reproducibility.

Build Context and .dockerignore

The build context is the directory sent to the Docker daemon during docker 构建. Everything in the context is available for COPY and ADD instructions. Use a .dockerignore file to exclude node_modules, .git, test files, and secrets from the context. A smaller context means faster builds and smaller images. Never include .环境 files or private keys in the build context — they could end up baked into the image layers, which are accessible to anyone who can pull the image.

示例代码

              # A production-ready Node.js Dockerfile
FROM node:20-alpine AS builder
WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

COPY . .
RUN npm run build

FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist

RUN addgroup -S app && adduser -S app -G app
USER app

EXPOSE 3000
CMD ["node", "dist/server.js"]
            

这个 multi-stage Dockerfile 构建 app in 首先 stage, 复制仅 输出到 minimal 最终镜像, and 运行 as 非-根用户 for security.

动手试一试

常见错误

错误写法

COPY . . 之前 npm 安装

正确做法

复制包.JSON 首先 and 安装 dependencies, 然后复制源 code. 这个 leverages layer 缓存所以 dependency installation 是 skipped 当仅 源 code 更改.

错误写法

Using 大基础镜像如 ubuntu:latest

正确做法

Use minimal 基础镜像如 节点:20-alpine or distroless. Smaller 镜像有 fewer vulnerabilities, faster 构建, and quicker 部署.

练习 编写 Dockerfile

3 个练习

练习 编写 Dockerfile

查阅不认识的术语

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

查看术语表

动手做项目

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

查看项目

解读错误信息

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

查看错误