Docker常用命令使用(一)
程序员文章站
2024-03-13 17:01:09
...
Docker常用命令
命令 | 描述 |
---|---|
docker 、docker help 、docker --help
|
列出可用命令 |
docker version |
显示Docker 版本信息 |
docker info |
显示Docker 系统范围的信息,包括内核版本,容器数和映像 |
docker images |
列出镜像信息 |
1.1docker help
Docker
文档官网地址:https://docs.docker.com/engine/reference/commandline/docker/
#下述命令都可列出可使用的命令
docker
docker help
docker --help
示例
[[email protected] /]# 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
-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/
[[email protected] /]#
1.2docker version
1.2.1 描述
显示
Docker
版本信息
1.2.2 用法
docker version [OPTIONS]
1.2.3 参数说明
名称,简写 | 默认 | 描述 |
---|---|---|
--format , -f |
使用给定的Go 模板格式化输出 |
|
--kubeconfig |
Kubernetes 配置文件(该参数适用于kubernetes 编排器) |
1.2.4 命令及示例
1.2.4.1 默认输出
docker version
[[email protected] /]# docker version
Client: Docker Engine - Community
Version: 20.10.3
API version: 1.41
Go version: go1.13.15
Git commit: 48d30b5
Built: Fri Jan 29 14:34:14 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.3
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 46229ca
Built: Fri Jan 29 14:32:37 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.3
GitCommit: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc:
Version: 1.0.0-rc92
GitCommit: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
docker-init:
Version: 0.19.0
GitCommit: de40ad0
1.2.4.2 获取服务器版本
docker version --format '{{.Server.Version}}'
[[email protected] /]# docker version --format '{{.Server.Version}}'
20.10.3
[[email protected] /]#
1.2.4.3 转储原始JSON数据
docker version --format '{{json .}}'
以
json
格式输出版本信息
[[email protected] /]# docker version --format '{{json .}}'
{"Client":{"Platform":{"Name":"Docker Engine - Community"},"Version":"20.10.3","ApiVersion":"1.41","DefaultAPIVersion":"1.41","GitCommit":"48d30b5","GoVersion":"go1.13.15","Os":"linux","Arch":"amd64","BuildTime":"Fri Jan 29 14:34:14 2021","Context":"default","Experimental":true},"Server":{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"20.10.3","Details":{"ApiVersion":"1.41","Arch":"amd64","BuildTime":"Fri Jan 29 14:32:37 2021","Experimental":"false","GitCommit":"46229ca","GoVersion":"go1.13.15","KernelVersion":"3.10.0-1127.19.1.el7.x86_64","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.4.3","Details":{"GitCommit":"269548fa27e0089a8b8278fc4fc781d7f65a939b"}},{"Name":"runc","Version":"1.0.0-rc92","Details":{"GitCommit":"ff819c7e9184c13b7c2607fe6c30ae19403a7aff"}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":"de40ad0"}}],"Version":"20.10.3","ApiVersion":"1.41","MinAPIVersion":"1.12","GitCommit":"46229ca","GoVersion":"go1.13.15","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-1127.19.1.el7.x86_64","BuildTime":"2021-01-29T14:32:37.000000000+00:00"}}
[[email protected] /]#
1.2.4.3 打印当前上下文
docker version --format='{{.Client.Context}}'
[[email protected] /]# docker version --format='{{.Client.Context}}'
default
[[email protected] /]#
1.3docker info
1.3.1 描述
显示
Docker
系统范围的信息,包括内核版本,容器数和映像
1.3.2 用法
docker info [OPTIONS]
1.3.3 参数说明
名称,简写 | 默认 | 描述 |
---|---|---|
--format , -f |
使用给定的Go 模板格式化输出 |
1.3.4 命令及示例
1.3.4.1 默认输出
docker info
[[email protected] /]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.3
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
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 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1127.19.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.795GiB
Name: VM-0-14-centos
ID: SYNP:7I3G:LIOF:SNKT:2U52:3TPU:DXDX:REFJ:EPSC:45K6:NSKY:6KSU
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
[[email protected] /]#
1.3.4.2 格式化输出
docker info --format '{{json .}}'
以json格式输出
[[email protected] /]# docker info --format '{{json .}}'
{"ID":"SYNP:7I3G:LIOF:SNKT:2U52:3TPU:DXDX:REFJ:EPSC:45K6:NSKY:6KSU","Containers":0,"ContainersRunning":0,"ContainersPaused":0,"ContainersStopped":0,"Images":0,"Driver":"overlay2","DriverStatus":[["Backing Filesystem","extfs"],["Supports d_type","true"],["Native Overlay Diff","true"]],"Plugins":{"Volume":["local"],"Network":["bridge","host","ipvlan","macvlan","null","overlay"],"Authorization":null,"Log":["awslogs","fluentd","gcplogs","gelf","journald","json-file","local","logentries","splunk","syslog"]},"MemoryLimit":true,"SwapLimit":true,"KernelMemory":true,"KernelMemoryTCP":true,"CpuCfsPeriod":true,"CpuCfsQuota":true,"CPUShares":true,"CPUSet":true,"PidsLimit":true,"IPv4Forwarding":true,"BridgeNfIptables":true,"BridgeNfIp6tables":true,"Debug":false,"NFd":22,"OomKillDisable":true,"NGoroutines":33,"SystemTime":"2021-02-08T14:20:25.115152471+08:00","LoggingDriver":"json-file","CgroupDriver":"cgroupfs","CgroupVersion":"1","NEventsListener":0,"KernelVersion":"3.10.0-1127.19.1.el7.x86_64","OperatingSystem":"CentOS Linux 7 (Core)","OSVersion":"7","OSType":"linux","Architecture":"x86_64","IndexServerAddress":"https://index.docker.io/v1/","RegistryConfig":{"AllowNondistributableArtifactsCIDRs":[],"AllowNondistributableArtifactsHostnames":[],"InsecureRegistryCIDRs":["127.0.0.0/8"],"IndexConfigs":{"docker.io":{"Name":"docker.io","Mirrors":[],"Secure":true,"Official":true}},"Mirrors":[]},"NCPU":1,"MemTotal":1927176192,"GenericResources":null,"DockerRootDir":"/var/lib/docker","HttpProxy":"","HttpsProxy":"","NoProxy":"","Name":"VM-0-14-centos","Labels":[],"ExperimentalBuild":false,"ServerVersion":"20.10.3","Runtimes":{"io.containerd.runc.v2":{"path":"runc"},"io.containerd.runtime.v1.linux":{"path":"runc"},"runc":{"path":"runc"}},"DefaultRuntime":"runc","Swarm":{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers":null},"LiveRestoreEnabled":false,"Isolation":"","InitBinary":"docker-init","ContainerdCommit":{"ID":"269548fa27e0089a8b8278fc4fc781d7f65a939b","Expected":"269548fa27e0089a8b8278fc4fc781d7f65a939b"},"RuncCommit":{"ID":"ff819c7e9184c13b7c2607fe6c30ae19403a7aff","Expected":"ff819c7e9184c13b7c2607fe6c30ae19403a7aff"},"InitCommit":{"ID":"de40ad0","Expected":"de40ad0"},"SecurityOptions":["name=seccomp,profile=default"],"Warnings":null,"ClientInfo":{"Debug":false,"Context":"default","Plugins":[{"SchemaVersion":"0.1.0","Vendor":"Docker Inc.","Version":"v0.9.1-beta3","ShortDescription":"Docker App","Experimental":true,"Name":"app","Path":"/usr/libexec/docker/cli-plugins/docker-app"},{"SchemaVersion":"0.1.0","Vendor":"Docker Inc.","Version":"v0.5.1-docker","ShortDescription":"Build with BuildKit","Name":"buildx","Path":"/usr/libexec/docker/cli-plugins/docker-buildx"}],"Warnings":null}}
[[email protected] /]#
1.4docker images
1.4.1 描述
默认值
docker images
将显示所有*镜像,它们的存储库和标签以及它们的大小REPOSITORY
:镜像的存储库TAG
:镜像的标签IMAGE ID
:镜像的idCREATED
:镜像的创建时间SIZE
:镜像的大小
1.4.2 用法
docker images [OPTIONS] [REPOSITORY[:TAG]]
1.4.3 参数说明
名称,简写 | 默认 | 描述 |
---|---|---|
--all , -a |
显示所有镜像(默认隐藏中间镜像) | |
--digests |
显示摘要 | |
--filter , -f |
根据提供的条件过滤输出 | |
--format |
使用Go模板格式化输出 | |
--no-trunc |
不要截断输出 | |
--quiet , -q |
仅显示镜像ID |
1.4.4 命令及示例
1.4.4.1 显示新创建的镜像
docker images
[[email protected] /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 13 months ago 13.3kB
[[email protected] /]#
1.4.4.2 按名称和标签显示镜像
docker images
命令采用可选[REPOSITORY[:TAG]]
参数,该参数将列表限制为与该参数匹配的镜像。
如果指定REPOSITORY
,但不指定TAG
,则该docker images
命令将列出给定存储库中的所有镜像像
若同时指定REPOSITORY
和TAG
,则[REPOSITORY[:TAG]]
值必须是完全匹配
docker iamges [REPOSITORY[:TAG]]
1.4.4.3 显示镜像ID的全称
docker images --no-trunc
[[email protected] /]# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b 13 months ago 13.3kB
[[email protected] /]#
1.4.4.4 显示镜像摘要
使用v2或更高版本格式的镜像具有称为摘要的内容可寻址标识符。只要用于生成镜像的输入不变,摘要值就可以预测
docker images --digests
[[email protected] /]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
hello-world latest sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d bf756fb1ae65 13 months ago 13.3kB
[[email protected] /]#
1.4.4.5 过滤输出
过滤标志(
-f
或--filter
)格式为“键=值”。
若有多个过滤器,则传递多个标志
代补充
1.4.4.6 格式化输出
格式化选项(
--format
)将使用Go
模板漂亮地打印容器输出。
Go
模板的有效占位符:
占位符 | 描述 |
---|---|
.ID |
镜像ID
|
.Repository |
镜像库 |
.Tag |
镜像标签 |
.Digest |
镜像摘要 |
.CreatedSince |
自创建镜像以来经过的时间 |
.CreatedAt |
创建镜像的时间 |
.Size |
镜像磁盘大小 |
docker images --format "{{.ID}}: {{.Repository}}"
[[email protected] /]# docker images --format "{{.ID}}: {{.Repository}}"
bf756fb1ae65: hello-world
[[email protected] /]#
以表格式列出所有镜像及其存储库和标签
docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
[[email protected] /]# docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
IMAGE ID REPOSITORY TAG
bf756fb1ae65 hello-world latest
[[email protected] /]#
Docker常用命令使用(二) 待完善