命令总览
命令图示总览
- 简单图示
- 命令逻辑图
所有命令总览
- 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 (Docker Inc., v0.14.0)
compose* Docker Compose (Docker Inc., v2.27.0)
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:
config Manage Swarm configs
node Manage Swarm nodes
secret Manage Swarm secrets
service Manage Swarm services
stack Manage Swarm stacks
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 version
查看当前docker版本信息
Client: Docker Engine - Community
Version: 24.0.9
API version: 1.43
Go version: go1.20.13
Git commit: 2936816
Built: Thu Feb 1 00:51:49 2024
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 24.0.9
API version: 1.43 (minimum version 1.12)
Go version: go1.20.13
Git commit: fca702d
Built: Thu Feb 1 00:50:53 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.31
GitCommit: e377cd56a71523140ca6ae87e30244719194a521
runc:
Version: 1.1.12
GitCommit: v1.1.12-0-g51d5e94
docker-init:
Version: 0.19.0
GitCommit: de40ad0
docker info
查看当前docker的基本信息
Client: Docker Engine - Community
Version: 24.0.9
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.14.0
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.27.0
Path: /usr/libexec/docker/cli-plugins/docker-compose
Server:
Containers: 11
Running: 10
Paused: 0
Stopped: 1
Images: 19
Server Version: 24.0.9
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 logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: e377cd56a71523140ca6ae87e30244719194a521
runc version: v1.1.12-0-g51d5e94
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-1160.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.84GiB
Name: devops-harbor
ID: ebfefe39-dd5b-4102-9e9c-5452ae073c99
Docker Root Dir: /data/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://11777p0c.mirror.aliyuncs.com/
Live Restore Enabled: false
docker --help
查看docker命令的基本帮助信息
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 (Docker Inc., v0.14.0)
compose* Docker Compose (Docker Inc., v2.27.0)
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 search --help
查看docker中具体命令帮助文档
Usage: docker search [OPTIONS] TERM
Search Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results
--no-trunc Don't truncate output
镜像相关命令
docker search
搜索 Docker Hub 上的镜像
- docker search centos
搜索镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos DEPRECATED; The official build of CentOS. 7731 [OK]
kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 44
bitnami/centos-base-buildpack Centos base compilation image 0 [OK]
dokken/centos-7 CentOS 7 image for kitchen-dokken 10
spack/centos7 CentOS 7 with Spack preinstalled 2
dokken/centos-8 CentOS 8 image for use with Test Kitchen's k… 6
dokken/centos-stream-8 CentOS Stream 8 image for use with Test Kitc… 5
dokken/centos-6 EOL: CentOS 6 image for kitchen-dokken 0
dokken/centos-stream-9 CentOS Stream 9 image for use with Test Kitc… 10
atlas/centos7-atlasos ATLAS CentOS 7 Software Development OS 3
spack/centos6 CentOS 6 with Spack preinstalled 1
eclipse/centos_jdk8 CentOS, JDK8, Maven 3, git, curl, nmap, mc, … 5 [OK]
ustclug/centos Official CentOS Image with USTC Mirror 0
corpusops/centos-bare https://github.com/corpusops/docker-images/ 0
corpusops/centos centos corpusops baseimage 0
eclipse/centos_go Centos + Go 0 [OK]
spack/centos-stream 2
fnndsc/centos-python3 Source for a slim Centos-based Python3 image… 0 [OK]
eclipse/centos_spring_boot Spring boot ready image based on CentOS 0 [OK]
openmicroscopy/centos-systemd-ip centos/systemd with iproute, for testing mul… 0 [OK]
eclipse/centos CentOS based minimal stack with only git and… 1 [OK]
eclipse/centos_nodejs CentOS based nodejs4 stack 0 [OK]
eclipse/centos_vertx CentOS + vertx 0 [OK]
eclipse/centos_wildfly_swarm CentOS, WildFly, Swarm 0 [OK]
dockette/centos My Custom CentOS Dockerfiles 1 [OK]
- docker search centos:7.9
搜索带标签的镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
rymcu/java-container Centos:7.9.2009 & jdk1.8.0_202 & tomcat:8.5.… 0
chengmo23/jdk8.381-mvn3.8.8 centos:7.9.2009 && jdk8.381 && mvn3.… 0
yaoguangtao/centos 基础镜像为官方的 centos:7.9 里面封装了 lrzsz… 0
oopex/python Based on CentOS:7.9, add vim, Python-3.10.2,… 0
sunfly2009/centos7_java1_8 CentOS:7.9.2009 + Oracle Java SE 1.8.0_731 +… 0
alpacahtt/centos79py39 centos:7.9.2009+Python3.9.5 0
mtimalsina/lz_offline_centos7_9 lz offline image FROM mtimalsina/lz_base_os:… 0
qiangwushuang/dft-centos base centos:7.9.2009 0
docker images
查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
sonarqube 10.5-community b728f044f72f 13 days ago 787MB
sonarqube 10.5.0-community 0130cd024290 4 weeks ago 787MB
sonarqube 10.0-community 09108ea99ebb 11 months ago 684MB
goharbor/harbor-exporter v2.8.1 bfbbb56f0c0e 12 months ago 97.9MB
goharbor/redis-photon v2.8.1 cb8b735b33ba 12 months ago 128MB
goharbor/trivy-adapter-photon v2.8.1 5314d0e3feb2 12 months ago 454MB
goharbor/notary-server-photon v2.8.1 d11f3ca7e07e 12 months ago 113MB
goharbor/notary-signer-photon v2.8.1 e97556d5f939 12 months ago 111MB
goharbor/harbor-registryctl v2.8.1 c141d82ffd0a 12 months ago 141MB
goharbor/registry-photon v2.8.1 c125efcb6c2a 12 months ago 79.1MB
goharbor/nginx-photon v2.8.1 cea1bb2450ee 12 months ago 127MB
goharbor/harbor-log v2.8.1 145a69b937ca 12 months ago 134MB
goharbor/harbor-jobservice v2.8.1 977ee6bff288 12 months ago 141MB
goharbor/harbor-core v2.8.1 075ed731ac18 12 months ago 165MB
goharbor/harbor-portal v2.8.1 6c2b21221aca 12 months ago 134MB
goharbor/harbor-db v2.8.1 01ca249e7dc2 12 months ago 174MB
goharbor/prepare v2.8.1 4f83854677d4 12 months ago 155MB
postgres 15.0 027eba2e8939 18 months ago 377MB
sonarqube community d3899771462f 2 years ago 514MB
docker pull
拉取镜像
- docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
- docker pull nginx:1.22.1
1.22.1: Pulling from library/nginx
f1f26f570256: Pull complete
fd03b214f774: Pull complete
ef2fc869b944: Pull complete
ac713a9ef2cc: Pull complete
fd071922d543: Pull complete
2a9f38700bb5: Pull complete
Digest: sha256:fc5f5fb7574755c306aaf88456ebfbe0b006420a184d52b923d2f0197108f6b7
Status: Downloaded newer image for nginx:1.22.1
docker.io/library/nginx:1.22.1
docker tag
标记镜像
- 语法
docker tag [选项] [源镜像名]:[源标签] [目标镜像名]:[目标标签]
- 将一个镜像标记为新的仓库和标签
docker tag nginx:1.22.1 registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1
docker push
推送镜像
- 语法
docker push [选项] [Docker Registry 地址]/[用户名]/[镜像名]:[标签]
- 推送一个名为registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1的镜像到阿里云
docker push registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1
docker build
构建镜像
- 格式
docker build [选项] [路径]
- 使用/data/docke/worke/Dockerfile构建一个镜像
docker build -f /data/docke/worke/Dockerfile .
docker run
运行镜像
- 格式
docker run [选项] [镜像名]:[标签]
- 运行一个nginx镜像并启动一个交互式终端
docker run -it nginx:1.22.1
docker rmi
删除镜像
- 格式
docker rmi [选项] [镜像名]:[标签]
- 删除一个名为nginx:1.22.1的镜像
docker rmi nginx:1.22.1
docker history
查看镜像历史
- 格式
docker history [选项] [镜像名]:[标签]
- 显示镜像的构建历史
docker history registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1
docker save
保存镜像
- 格式
docker save [选项] [镜像名]:[标签] > [文件名].tar
- 将一个镜像保存为 tar 文件
docker save -o registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1 > nginx.tar
docker load
加载镜像
- 格式
docker load [选项] < [文件名].tar
- 从 tar 文件加载一个镜像
docker load -i nginx.tar
docker load < nginx.tar
docker inspect
查看镜像详细信息
- 格式
docker inspect [选项] IMAGE [IMAGE...]
- 查看名为nginx镜像的详细信息
docker inspect nginx
docker commit
创建镜像
- 语法
docker commit [选项] CONTAINER [REPOSITORY[:TAG]]
- 从一个运行中的容器创建一个新的镜像
docker commit sonarqube sonarqube:latest
docker image inspect
镜像分层详细信息
- 格式
docker image inspect [选项] IMAGE [IMAGE...]
- 查看一个镜像的分层详细信息
docker image inspect nginx:latest
docker image prune
镜像清理
- 格式
docker image prune [选项]
- 清理未被使用的镜像,释放空间
docker image prune -a -f
docker builder prune
镜像构建缓存清理
- 格式
docker builder prune [选项]
- 清理 Docker 构建时产生的缓存
#这个命令会默认删除所有悬空的构建缓存。如果你想要更详细的输出,可以加上 --verbose 选项
docker builder prune --verbose
#想要指定清理的空间大小,可以使用 --filter 选项
docker builder prune --filter "until=10GB"
#请谨慎使用 --force 选项,因为它会跳过所有确认步骤,直接执行清理操作
docker builder prune --force
容器相关命令
docker create
创建容器
- 格式
docker create [选项] [镜像名]:[标签]
- 创建一个名为my_nginx的容器
docker create --name my_nginx nginx
docker start
启动容器
- 格式
docker start [容器名或ID]
- 启动一个名为my_nginx的容器
docker start my_nginx
docker stop
停止容器
- 格式
docker stop [容器名或ID]
- 停止一个名为my_nginx的容器
docker stop my_nginx
- 停止所有容器
docker stop $(docker ps -qa)
docker restart
重启容器
- 格式
docker restart [容器名或ID]
- 重启一个名为my_nginx的容器
docker restart my_nginx
docker rm
删除容器
- 格式
docker rm [选项] [容器名或ID]
- 删除一个名为my_nginx的容器
#删除容器
docker rm my_nginx
#强制删除容器
docker rm -f my_nginx
- 删除所有容器
docker rm $(docker ps -qa)
docker ps
查看容器
- 查看运行中的容器
docker ps
- 查看所有容器(包括停止的)
docker ps -a
- 查看所有容器的容器ID
docker ps -qa
docker logs
查看容器日志
- 格式
docker logs [选项] [容器名或ID]
- 查看一个名为my_nginx的容器的日志
docker logs my_nginx
- 动态查看一个名为my_nginx的容器的日志
docker logs -f my_nginx
docker inspect
查看容器的详细信息
- 格式
docker inspect [容器名或ID]
- 查看一个名为my_nginx的容器的详细信息
docker inspect my_nginx
docker exec
- 格式
docker exec [选项] [容器名或ID] [命令]
- 进入运行中的容器
docker exec -it my_nginx /bin/bash
- 在容器内执行命令
docker exec -it my_nginx nginx -v
docker pause
暂停容器
- 格式
docker pause [容器名或ID]
- 暂停一个名为my_nginx的容器
docker pause my_nginx
docker unpause
恢复暂停的容器
- 格式
docker unpause [容器名或ID]
- 恢复一个名为my_nginx的容器
docker unpause my_nginx
docker stats
容器状态
- 格式
docker stats [容器名或ID]
- 显示容器的实时资源使用情况
docker stats my_nginx
docker rename
容器重命名
- 格式
docker rename [当前名称] [新名称]
- 将一个名为my_nginx的容器重命名为my_nginx_web
docker rename my_nginx my_nginx_web
docker update
更新容器配置
- 格式
docker update [选项] [容器名或ID]
- 更新一个容器的内存限制
docker update --memory 2g my_nginx_web
docker cp
- 格式
docker cp [源文件或目录] [容器名:目标目录]
docker cp [容器名:源文件或目录] [目标文件或目录]
- 将本地的file.txt 复制到名为 mycontainer 的容器的my_nginx_web目录下
docker cp file.txt my_nginx_web:/app
- 从名为my_nginx_web的容器复制/app/file.txt到本地目录
docker cp my_nginx_web:/app/file.txt /root/
docker network
- 格式
docker network connect [网络名称] [容器名或ID]
docker network disconnect [网络名称] [容器名或ID]
- 将一个容器连接到名为mynetwork的网络
docker network connect mynetwork my_nginx_web
- 将一个容器从名为mynetwork的网络断开
docker network disconnect mynetwork my_nginx_web
docker expose
容器端口暴露
- 格式
docker expose [容器名或ID] [端口]
- 将一个容器的 80 端口暴露给外部
docker expose my_nginx_web 80
仓库相关命令
docker login
登录仓库
- 格式
docker login [仓库地址]
- 登录阿里云仓库
docker login --username=ai王茗渠ai registry.cn-beijing.aliyuncs.com
docker push
推送镜像到仓库
- 格式
docker push [仓库地址]/[用户名]/[镜像名]:[标签]
- 将镜像推送到阿里云仓库
docker push registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1
docker pull
从仓库拉取镜像
- 格式
docker pull [仓库地址]/[用户名]/[镜像名]:[标签]
- 从阿里云仓库拉取镜像
docker pull registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1
docker search
搜索仓库中的镜像
- 格式
docker search [仓库地址]/[用户名]/[镜像名]
- 从阿里云仓库搜索镜像
docker search registry.cn-beijing.aliyuncs.com/publicspaces/nginx
docker images
查看仓库的镜像
- 格式
docker images [仓库地址]/[用户名]/[镜像名]
- 查看阿里云仓库镜像
docker images registry.cn-beijing.aliyuncs.com/publicspaces/nginx
docker rmi
删除仓库中的镜像
- 格式
docker rmi [仓库地址]/[用户名]/[镜像名]:[标签]
- 从阿里云查看中删除镜像
docker rmi registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1
docker tag
管理仓库的镜像标签
- 格式
docker tag [镜像名]:[标签] [仓库地址]/[用户名]/[镜像名]:[标签]
- 标记镜像为新的仓库和标签
docker tag nginx:1.22.1 registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1
网络相关命令
docker network create
创建网络
- 格式
docker network create [选项] NETWORK
- 创建一个名为 my-network 的网络
docker network create my-network
- 创建一个名为 my-bridge-network 的桥接网络
docker network create -d bridge my-bridge-network
- 创建一个名为 my-overlay-network 的覆盖网络
docker network create -d overlay my-overlay-network
- 创建一个名为 my-host-network 的主机网络
docker network create -d host my-host-network
- 创建一个名为 my-subnet-network 的网络,并指定子网和网关
#格式
docker network create --subnet [子网] --gateway [网关] NETWORK
#举例
docker network create --subnet 172.17.0.0/16 --gateway 172.17.0.1 my-subnet-network
- 创建一个名为 my-ipv4-network 的网络,并指定 IPv4 地址
#格式
docker network create --internal --ipv4-address [IPv4 地址] --ipv6-address [IPv6 地址] NETWORK
#举例
docker network create --internal --ipv4-address 172.17.0.1 my-ipv4-network
- 创建一个名为 my-attachable-network 的网络,允许容器独立于网络
#格式
docker network create --attachable NETWORK
#举例
docker network create --attachable my-attachable-network
- 创建一个名为 my-override-network 的网络,并覆盖主机名和域名
#格式
docker network create --hostname-override [主机名] --domain-override [域名] NETWORK
#举例
docker network create --hostname-override myhost --domain-override mydomain.com my-override-network
- 创建一个名为 my-config-network 的网络,并从指定的配置文件中读取配置
#格式
docker network create --config-from-file [配置文件路径] NETWORK
#举例
docker network create --config-from-file /path/to/config.json my-config-network
docker network ls
查看网络
- 列出所有 Docker 网络
docker network ls
docker network rm
- 格式
docker network rm [选项] NETWORK
- 删除一个名为 my-network 的网络
docker network rm my-network
docker network connect
连接容器到网络
- 格式
docker network connect [选项] NETWORK CONTAINER
- 将一个名为 my_nginx 的容器连接到名为 my-network 的网络
docker network connect my-network my_nginx
docker network disconnect
从网络断开连接
- 格式
docker network disconnect [选项] NETWORK CONTAINER
- 从名为 my-network 的网络断开名为 my_nginx 的容器的连接
docker network disconnect my-network my_nginx
docker network inspect
查看网络配置
- 格式
docker network inspect [选项] NETWORK
- 查看名为 my-network 的网络的详细配置
docker network inspect my-network
- 查看名为 my-network 的网络的详细信息
docker network inspect -f "{{json .Name}}" my-network
集群相关命令
docker swarm
docker swarm init
初始化swarm集群
- swarm集群初始化
docker swarm init
- 指定网卡的swam集群初始化
# --advertise-addr 当主机有多块网卡时使用其选择其中一块用于广播,用于其它节点连接管理节点使用+
# --listen-addr 监听地址,用于承载集群流量使用
docker swarm init \
--advertise-addr 192.168.10.10 \
--listen-addr 192.168.10.10:2377
docker swarm join
添加节点到集群中
- 添加工作节点到集群
# 生成工作节点加入集群的token
docker swarm join-token worker
#添加工作节点到集群
docker swarm join --token SWMTKN-1-4casbbfuntxpotkt0d4mw50nh82m4pexpngdebclmps2cslpvo-5vsddlonkqqvdtjqafk93a9do 192.168.108.128:2377
- 添加管理节点到集群
# 生成管理节点加入集群的token
docker swarm join-token manager
#添加管理节点到集群
docker swarm join --token SWMTKN-1-4casbbfuntxpotkt0d4mw50nh82m4pexpngdebclmps2cslpvo-01o19uvzv5rybor9hdmio7b4j 192.168.108.128:2377
docker swarm join-token
生成token用于添加节点
- 生成工作节点加入集群的token
docker swarm join-token worker
- 生成管理节点加入集群的token
docker swarm join-token manager
docker swarm leave
从集群中移除节点
- 将节点从swarm集群中强制移除
docker swarm leave --force
docker swarm update
用于更新集群的配置
- 格式
docker swarm update [options]
#参数
--swarm-overlay-driver:指定 Swarm 的覆盖网络驱动。
--overlay-network:配置覆盖网络的参数,如子网、网关等。
--host-network:启用主机网络模式。
--dispatcher-heartbeat:设置调度器心跳间隔。
--max-replicas:设置服务可以扩展到的最大副本数。
--snapshot-interval:设置快照创建的间隔。
--task-history-limit:设置任务历史记录的最大数量。
--raft-log-level:设置 Raft 日志级别。
--raft-snapshot-threshold:设置 Raft 快照的阈值。
--raft-ttl:设置 Raft 信息的超时时间。
--task-history-ttl:设置任务历史记录的超时时间。
- 更新集群的覆盖网络驱动
docker swarm update --swarm-overlay-driver <driver-name>
docker node
docker node inspect
查看节点详情
- 格式
docker node inspect [NODE_NAME]
- 查看名为 node1 的节点的详细信息
docker node inspect node1
docker node ls
列出节点
docker node ls
docker node ps
列出集群中所有节点的服务
- 格式
docker node ps [options]
#参数
--filter:过滤器,用于筛选结果。
--limit:限制返回的条目数量。
--no-trunc:不截断输出。
--sort:排序选项,如 -started_at、-desired_state、-current_state 等。
- 筛选特定节点的服务
docker node ps --filter NodeName=node1
docker node rm
删除节点
- 格式
docker node rm [NODE_NAME]
- 删除名为 node2 的节点
docker node rm node2
docker node update
更新节点
- 格式
docker node update [选项] NODE_NAME
docker node update [选项] NODE_NAME --role [role]
- 更新名为 node1 的节点的标签
docker node update node1 --label-add my-label=value
- 将名为 node1 的节点设置为管理节点
docker node update node1 --role manager
docker node demote
将节点从集群中移除
- 格式
docker node demote [NODE_NAME]
- 将名为 node1 的节点从管理节点中移除
docker node demote node1
docker node promote
将节点升级为管理节点
- 格式
docker node promote [NODE_NAME]
- 将名为 node1 的节点升级为管理节点
docker node promote node1
服务堆栈相关命令
docker service
docker service create
创建服务
- 格式
docker service create [选项] IMAGE[:TAG] [SERVICE_NAME]
- 创建一个名为 my-service 的服务,使用 ubuntu:latest 镜像
docker service create --name my-service --replicas 2 ubuntu:latest
docker service ls
列出服务
- 列出集群中的所有服务及其状态
docker service ls
docker service rm
- 格式
docker service rm [SERVICE_NAME]
- 删除名为 my-service 的服务
docker service rm my-service
docker service update
更新服务
- 格式
docker service update [选项] SERVICE_NAME
- 更新名为 my-service 服务的副本数量
docker service update my-service --replicas 3
- 根据指定的重启策略重启服务中的所有任务
#格式
docker service update [SERVICE_NAME] --restart-policy condition=any
#举例
docker service update my-service --restart-policy condition=any
- 根据指定的镜像版本滚动更新服务中的任务
#格式
docker service update [SERVICE_NAME] --image-version [VERSION]
#举例
docker service update my-service --image-version [VERSION]
docker service rollback
服务回退
- 格式
docker service rollback [SERVICE_NAME] [VERSION]
- 将服务 my-service 回退到 1.0.0 版本
docker service rollback my-service 1.0.0
docker service inspect
查看服务详情
- 格式
docker service inspect [SERVICE_NAME]
- 查看名为 my-service 的服务的详细信息
docker service inspect my-service
- 查看名为 my-service 的服务的配置,并以易于阅读的格式输出
#格式
docker service inspect [SERVICE_NAME] --pretty
#举例
docker service inspect my-service --pretty
- 服务的详细配置信息
#格式
docker service inspect [SERVICE_NAME]
#举例
docker service inspect my-service
docker service scale
- 格式
docker service scale [SERVICE_NAME]=[REPLICAS]
- 将名为 my-service 的服务的副本数量增加到 3
docker service scale my-service=3
- 将名为 my-service 的服务的副本数量减少到 1
docker service scale my-service=1
docker service logs
- 格式
docker service logs [SERVICE_NAME]
- 查看名为 my-service 的服务的日志
docker service logs my-service
docker service tasks
- 格式
docker service tasks [SERVICE_NAME]
- 列出服务中所有任务的当前状态
docker service tasks my-service
docker service history
- 格式
docker service history [SERVICE_NAME]
- 列出服务的历史记录,包括每个任务的详细信息、执行时间、结果等
docker service history my-service
docker stack
docker stack deploy
创建一个新的堆栈
- 格式
docker stack deploy [选项] [STACK_NAME] [COMPOSE_FILE]
- 创建一个名为 my-stack 的堆栈,使用 docker-compose.yml 文件
docker stack deploy --compose-file docker-compose.yml my-stack
docker stack ls
- 列出所有已定义的堆栈及其服务
docker stack ls
docker stack ps
- 格式
docker stack ps [STACK_NAME]
- 列出堆栈中的所有服务及其状态
docker stack ps my-stack
docker stack rm
删除堆栈
- 格式
docker stack rm [STACK_NAME]
- 删除名为 my-stack 的堆栈
docker stack rm my-stack
docker stack services
- 格式
docker stack services [options] [STACK_NAME]
#参数
--filter:过滤器,用于筛选结果。
--limit:限制返回的条目数量。
--no-trunc:不截断输出。
--sort:排序选项,如 -started_at、-desired_state、-current_state 等。
- 列出名为 my-stack 的堆栈中的所有服务
docker stack services my-stack
- 筛选特定服务
docker stack services my-stack --filter ServiceName=my-service
数据卷相关命令
docker volume create
创建数据卷
- 格式
docker volume create [VOLUME_NAME]
docker volume create --driver [DRIVER] --opt [OPTIONS] [VOLUME_NAME]
#驱动参数
local:本地驱动,使用宿主机的目录作为数据卷。
nfs:NFS 驱动,使用 NFS 服务器作为数据卷。
zfs:ZFS 驱动,使用 ZFS 文件系统作为数据卷。
glusterfs:GlusterFS 驱动,使用 GlusterFS 文件系统作为数据卷。
#数据参数
o=bind:将宿主机的目录挂载到容器中。
o=rbind:将宿主机的目录以只读方式挂载到容器中。
type=none:创建一个空的、无关联的数据卷。
- 创建一个名为 myvolume 的数据卷
docker volume create myvolume
- 创建一个名为 myvolume 的数据卷,并指定 local 驱动和 type=none 选项
docker volume create --driver local --opt type=none myvolume
docker volume ls
- 列出所有本地数据卷
docker volume ls
docker volume inspect
查看数据卷详情
- 格式
docker volume inspect [VOLUME_NAME]
docker volume inspect --format '{{json .Mountpoint}}' [VOLUME_NAME]
- 查看名为 myvolume 的数据卷的详细信息
docker volume inspect myvolume
- 查看名为 myvolume 的数据卷挂载到容器的路径
docker volume inspect --format '{{json .Mountpoint}}' myvolume
docker volume rm
删除数据卷
- 格式
docker volume rm [VOLUME_NAME]
- 删除名为 myvolume 的数据卷
docker volume rm myvolume
docker volume prune
清理未挂载的数据卷
- 清理所有未挂载的数据卷
docker volume prune
- 强制删除未挂载的数据卷
docker volume prune -f