Skip to content

Docker 常用命令

帮助命令

docker version 查看 docker 版本信息

bash
docker version

Client: Docker Engine - Community
 Version:           27.0.3
 API version:       1.46
 Go version:        go1.21.11
 Git commit:        7d4bcd8
 Built:             Sat Jun 29 00:02:29 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.0.3
  API version:      1.46 (minimum version 1.24)
  Go version:       go1.21.11
  Git commit:       662f78c
  Built:            Sat Jun 29 00:02:29 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.19
  GitCommit:        2bf793ef6dc9a18e00cb12efb64355c2c9d5eb41
 runc:
  Version:          1.7.19
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker info

bash
docker info
(train) root@T0321:/home/dmai/train# docker info
Client: Docker Engine - Community
 Version:    27.0.3
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.15.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.28.1
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 2
  Running: 1
  Paused: 0
  Stopped: 1
 Images: 4
 Server Version: 27.0.3
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 2bf793ef6dc9a18e00cb12efb64355c2c9d5eb41
 runc version: v1.1.13-0-g58aa920
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
 Kernel Version: 5.4.0-89-generic
 Operating System: Ubuntu 20.04.6 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 15.63GiB
 Name: T0321
 ID: aa608d00-ec4a-442f-a22f-45dfdf523557
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://docker.m.daocloud.io/
  https://docker.1panel.live/
  https://hub.rat.dev/
 Live Restore Enabled: false

docker help

bash
docker help
Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx
  compose*    Docker Compose
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  swarm       Manage Swarm

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  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
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  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
  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
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  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
  wait        Block until one or more containers stop, then print their exit codes

Global 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 to connect to
  -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

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

For more help on how to use Docker, head to https://docs.docker.com/go/guides/

镜像命令

docker images

text
REPOSITORY    TAG                     IMAGE ID       CREATED         SIZE
mongo         7.0.0                   00307f461add   10 months ago   735MB
openjdk       11-jre                  362cda5d270e   24 months ago   302MB
maven         3.8.5-openjdk-11-slim   6c6bb942e663   2 years ago     446MB
hello-world   latest                  48b5124b2768   7 years ago     1.84kB
选项说明
REPOSITORY表示镜像的仓库源
TAG镜像的标签
IMAGE ID镜像 ID
CREATED镜像创建时间
SIZE镜像大小
参数说明
-a列出本地所有的镜像
-q只显示镜像 ID
--digests显示镜像的摘要信息
--no-trunc显示完整的镜像信息

搜索镜像

bash
docker search

拉取镜像

bash
docker pull

删除镜像

bash
docker rmi
删除方式命令
删除单个docker rmi -f 镜像 ID
删除多个docker rmi -f 镜像 1:TAG 镜像 2:TAG
删除全部docker rmi -f $(docker images -qa)

容器命令

创建并启动一个容器的命令

bash
docker run [OPTIONS] IMAGE [COMMAND]

OPTIONS 参数说明

options说明
--name"容器新名字": 为容器指定一个名称
-d后台运行容器,并返回容器 ID,也即启动守护式容器
-i以交互模式运行容器,通常与 -t 同时使用
-t为容器重新分配一个伪输入终端,通常与 -i 同时使用
-P:随机端口映射
-p指定端口映射,有以下四种格式 ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort

列举运行的容器

bash
docker ps [OPTIONS]
OPTIONS说明
-a列出当前所有正在运行的容器+历史上运行过的
-l显示最近创建的容器。
-n显示最近 n 个创建的容器。
-q静默模式,只显示容器编号。
--no-trunc不截断输出。

退出容器命令

退出方式说明
exit容器停止退出
ctrl+p+q容器不停止退出

启动容器

bash
docker start 容器ID或者容器名称

重启容器

bash
docker restart 容器id或者名称

停止容器

bash
docker stop 容器ID或者名称

删除容器

bash
docker rm 容器ID
docker rm -f $(docker ps -qa)
docker ps -a -q | xargs docker rm

查看容器中运行的进程

bash
docker top 容器id

守护式容器

为了让守护式容器能够一直执行,我们可以在启动容器后在后台运行一个循环的脚本

bash
docker run -d centos /bin/bash -c 'while true;do echo hello bobo;sleep 2;done'

查看容器日志

bash
docker logs -t -f --tail 3 容器id

查看容器细节

bash
docker inspect 容器id

进入运行的容器

进入方式说明
exec在容器中打开新的终端,并且可以启动新的进程
attach直接进入容器启动命令的终端,不会启动新的进程

文件复制

容器中拷贝到宿主机

bash
docker cp 容器ID:容器内路径  目的地路径

Released under the MIT License.