Docker 容器
运行, manage, and 调试 Docker 容器使用 ports, 体积, and 环境变量.
你将学到
- 运行容器使用 docker 运行 and 键 flags
- 映射 ports and mounting 体积
- Passing 环境变量到 容器
- Inspecting and 调试运行容器
概念讲解
运行容器
The docker 运行 command starts a container from an image. The -d flag runs it in the background (detached). --name gives the container a readable name. -p 8080:80 maps port 8080 on your host to port 80 in the container — this is how you make a containerized web server accessible from outside. Without port mapping, the container's port is only reachable from inside the Docker network.
Volumes
Containers are ephemeral — when a container is removed, any data written inside it is lost. Volumes solve this by storing data on the host filesystem, managed by Docker. Use -v mydata:/数据 to mount a named volume, or -v /host/path:/容器/path to mount a specific host directory. Volumes persist across container restarts and removals, making them essential for databases and any stateful application.
Environment Variables
You pass configuration to containers with the -e flag: docker 运行 -e NODE_ENV=production myapp. For many variables, use --环境-file .环境 to load from a file. Inside the container, the application reads these variables the same way it reads any environment variable. This is the standard pattern for configuring containerized applications without baking secrets or environment-specific values into the image.
Inspecting and Debugging
docker ps lists running containers. docker logs <容器> shows the container's stdout and stderr. docker exec -它 <容器> bash opens an interactive shell inside a running container — invaluable for debugging. docker inspect <容器> returns detailed JSON information about the container's configuration, network, and mounts. docker stats shows real-time resource usage.
Container Networking
By default, Docker creates a bridge network for containers. Containers on the same network can communicate using their names as hostnames. Port mapping (-p host:容器) exposes a container port to the host. User-defined networks provide better isolation and DNS resolution. For multi-container apps, Docker Compose automatically creates a network and connects all services. Understanding networking is essential for connecting your application to databases, caches, and external services in a containerized environment.
示例代码
# Run a web server with port mapping
docker run -d --name web -p 8080:80 nginx:alpine
# Run with volumes and environment variables
docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data \
-p 5432:5432 \
postgres:16
# Inspect and debug
docker ps # list running containers
docker logs -f web # follow container logs
docker exec -it web sh # open a shell inside
docker inspect db | head -50 # view container details
docker stats # real-time resource usage
docker 运行开始容器使用 port 映射 (-p), 体积 (-v), and 环境 vars (-e). docker exec 打开 shell for 调试, and docker stats shows resource usage.
动手试一试
- Docker Compose Generator
Generate compose files for multi-容器 setups.
- 环境 File Generator
创建 .环境 files for 容器配置.
- YAML Formatter
格式 and prettify YAML 配置 files.
常见错误
错误写法
Forgetting 到映射 ports 当运行容器
正确做法
Use -p hostPort:containerPort 到 expose 容器's port. 不使用它, service 是仅 reachable 内部 Docker network, 不从 你的 host or 浏览器.
错误写法
存储 persistent 数据内部容器 filesystem
正确做法
Use 体积 (-v) for 任何数据那个必须 survive 容器 removal. 容器 filesystems 是 ephemeral — 数据是 lost 当容器是 删除.
练习 Docker 容器
6 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。