Docker

发布: (2026年1月31日 GMT+8 13:37)
4 分钟阅读
原文: Dev.to

I’m happy to help translate the article, but I’ll need the full text you’d like translated. Could you please paste the content (excluding the source line you’ve already provided) here? Once I have the text, I’ll translate it into Simplified Chinese while preserving the original formatting and markdown.

什么是 Docker

Docker 容器是 Docker 镜像的运行实例。它包括应用程序、库和运行时,并与主机系统隔离。

为什么使用 Docker

  • 一致性 – 在笔记本、CI 和生产环境中使用相同的环境。
  • 可移植性 – 镜像可以在任何运行 Docker 引擎的地方运行。
  • 高效性 – 容器共享主机内核,启动快速,内存占用比虚拟机更少。
  • 简化 CI/CD 与微服务 – 轻松构建、交付并扩展小型独立服务。

Core concepts

  • Image – 不可变的蓝图(只读)。
  • Container – 镜像的运行实例(其上方的可写层)。
  • Registry – 镜像的存储(例如 Docker Hub)。
  • Dockerfile – 包含构建镜像指令的文本文件。
  • Volume / Bind mount – 在容器外持久化或共享数据的方式。
  • Network drivers – bridge(默认)、host、none、overlay(用于多主机)。
  • Layer caching – 构建时复用未更改的层以加快构建速度。

简约 Dockerfile — 每行含义

FROM openjdk:17-jdk-slim      # base image with Java runtime
WORKDIR /app                  # set working directory inside the image
COPY . /app                   # copy files from host into the image
EXPOSE 8080                   # documents a port (does not publish it)
ENTRYPOINT ["java","-jar","app.jar"]  # executable used when container starts
  • FROM – 指定基础镜像。
  • WORKDIR – 设置后续命令运行的工作目录。
  • COPY – 将主机上的文件复制到镜像中。
  • EXPOSE – 声明容器监听的端口(仅用于文档说明)。
  • ENTRYPOINT – 定义容器启动时执行的命令。

提示: 添加 .dockerignore 文件以排除不想放入镜像的构建产物。

基本命令(简洁速查表)

常规信息

docker --version            # show Docker client version
docker info                 # daemon summary (containers, images, storage driver)

构建镜像

docker build -t myapp:1.0 .                     # build image from Dockerfile in current dir
docker build --no-cache -t myapp:1.0 .          # rebuild without cache

运行与生命周期(创建、启动、停止、删除)

docker run --name web -d -p 8080:80 nginx:latest
# -d (detached), -p hostPort:containerPort, --name containerName

docker run -it --rm ubuntu bash
# -it interactive, --rm remove on exit
docker ps                     # list running containers
docker ps -a                  # list all containers (including stopped)
docker stop       # graceful stop
docker start      # start stopped container
docker restart    # restart
docker rm          # remove container

镜像

docker pull nginx:latest    # download image from registry

检查、执行与日志

docker logs -f           # follow logs
docker exec -it  bash    # open a shell inside a running container
docker inspect           # detailed low‑level information

卷与挂载(持久化数据)

docker volume create myvol
docker run -v myvol:/data ...                # named volume
docker run -v /host/path:/data ...           # bind mount
docker volume ls
docker volume rm myvol
  • 命名卷(由 Docker 管理)最适合数据库和可移植性。
  • 绑定挂载直接将主机文件夹映射到容器中(开发时很有用)。

网络(快速)

docker network ls
docker network create mynet
docker run --network mynet ...
docker network inspect mynet

清理(释放磁盘空间)

docker system df
docker image prune          # remove dangling images
docker container prune      # remove stopped containers
docker system prune -a      # remove unused images/containers/networks (use with care)

Compose(多容器应用)

docker compose up -d
docker compose down
docker compose ps
docker compose logs -f

监控与调试

docker stats                   # live resource usage per container
Back to Blog

相关文章

阅读更多 »

我能在脑海中看到我的成功

成功的幻象 我看得很清楚。我的 app 已经上线,毫无 bug,用户们赞不绝口。投资者们接连来电。我的 GitHub repo 的 stars 像潮水般涌入。