跳转至

Docker客户端命令

docker 是 C/S 架构

Docker 客户端命令格式,参考了 Git 的命令方式,根据功能分子命令

docker 客户端命令格式

docker --help

$ docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with
                           "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to (default )
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Build with BuildKit (Docker Inc., v0.5.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/

Docker命令选项详解

内容摘要

本章节聚焦于Docker命令的详细解析,特别是Docker命令的选项及其使用场景。与其他章节相比,本章节的核心在于深入探讨Docker命令行工具的功能和参数,帮助用户全面掌握Docker命令的使用技巧和最佳实践。

主要内容

1. Docker命令概述

  • Docker命令行工具的基本结构
  • Docker命令可以分为多个类别,包括容器管理、镜像管理、网络管理等。每个类别下的命令都有其特定的功能和作用。
  • 例如,docker run用于启动容器,docker build用于构建镜像,docker network用于管理网络等。

  • 通用选项

  • --help:显示命令的帮助信息。
  • --version:显示Docker的版本信息。
  • 这些通用选项可以帮助用户快速了解命令的使用方法和Docker的版本信息。

2. 命令选项详解

  • docker run命令选项
  • -d:后台运行容器。
  • -p:端口映射,将容器内的端口映射到主机端口。
  • -v:数据卷挂载,将主机目录挂载到容器内。
  • 示例:docker run -d -p 8080:80 -v /host/path:/container/path nginx

  • docker build命令选项

  • -t:为镜像指定标签。
  • --no-cache:禁用缓存,强制重新构建镜像。
  • 示例:docker build -t my-image:latest --no-cache .

  • docker network命令选项

  • --driver:指定网络驱动类型,如bridgeoverlay等。
  • --subnet:配置子网。
  • 示例:docker network create --driver bridge --subnet 192.168.1.0/24 my-network

3. 高级选项与技巧

  • docker exec命令选项
  • --env:设置环境变量。
  • 示例:docker exec --env VAR1=value1 my-container echo $VAR1

  • docker logs命令选项

  • --tail:限制显示的日志行数。
  • 示例:docker logs --tail 10 my-container

  • 命令行技巧

  • 使用docker ps -a | grep过滤容器列表。
  • 示例:docker ps -a | grep running

4. 与其他章节的区别

  • 与“产品功能详解”章节的区别
  • 本章节专注于Docker命令的选项解析,而非命令的整体功能或底层原理。这些内容在“产品功能详解”或“原理解析”章节中已有详细说明。

  • 与“客户端命令详解”章节的区别

  • 本章节更侧重于选项的深度解析,而非命令的全面介绍。

目标读者

  • 适合已经熟悉Docker基础操作,希望进一步提升命令行使用效率的用户
  • 适合需要深入了解Docker命令选项的开发者和运维人员

通过本章节的学习,读者将能够熟练使用Docker命令的各种选项,优化日常操作流程,并解决实际使用中的复杂问题。