命令总图
帮助命令
docker version
显示docker 的版本信息
[root@localhost admin]# docker version
Client: Docker Engine - Community
Version: 20.10.9
API version: 1.41
Go version: go1.16.8
Git commit: c2ea9bc
Built: Mon Oct 4 16:08:25 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.9
API version: 1.41 (minimum version 1.12)
Go version: go1.16.8
Git commit: 79ea9d3
Built: Mon Oct 4 16:06:48 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.11
GitCommit: 5b46e404f6b9f661a205e28d59c982d3634148f8
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
docker info
显示docker的系统信息,包括镜像和容器的数量
[root@localhost admin]# 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.6.3-docker)
scan: Docker Scan (Docker Inc., v0.8.0)
Server:
Containers: 5
Running: 0
Paused: 0
Stopped: 5
Images: 3
Server Version: 20.10.9
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
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 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
runc version: v1.0.2-0-g52b36a2
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.18.0-305.3.1.el8.x86_64
Operating System: CentOS Linux 8
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.748GiB
Name: localhost.localdomain
ID: 5XYB:FCRV:5FPN:RGQN:I6XP:VSZE:SR7S:2BI4:7FZU:62HJ:D73A:EAY5
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
Registry Mirrors:
https://my2232nw.mirror.aliyuncs.com/
Live Restore Enabled: false
docker --help
查看docker相关的命令
[root@localhost admin]# 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.6.3-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
scan* Docker Scan (Docker Inc., v0.8.0)
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/
官方命令帮助文档地址:https://docs.docker.com/reference/
镜像命令
docker images
查看所有的本地上的镜像信息
[root@localhost admin]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 42b97d3c2ae9 7 weeks ago 1.24MB
continuumio/miniconda3 latest 67414e5844b6 2 months ago 391MB
hello-world latest d1165f221234 7 months ago 13.3kB
# 名称解释
REPOSITORY 镜像仓库源
TAG 镜像标签
IMAGE ID 镜像ID
CREATED 镜像创建时间
SIZE 镜像大小
#可选项
-a, --all Show all images (default hides intermediate images) 列出所有镜像
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs 只显示镜像ID
***********************************************************************************************************
[root@localhost admin]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 42b97d3c2ae9 7 weeks ago 1.24MB
continuumio/miniconda3 latest 67414e5844b6 2 months ago 391MB
hello-world latest d1165f221234 7 months ago 13.3kB
[root@localhost admin]# docker images -q
42b97d3c2ae9
67414e5844b6
d1165f221234
docker search
搜索镜像
[root@localhost admin]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11530 [OK]
mariadb MariaDB Server is a high performing open sou… 4378 [OK]
# 可选项
-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 (default 25)
--no-trunc Don't truncate output
***********************************************************************************************************************
[root@localhost admin]# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11530 [OK]
docker pull
下载镜像
[root@localhost admin]# docker pull mysql
Using default tag: latest # 默认下载最新版本
latest: Pulling from library/mysql
b380bbd43752: Pull complete # 分层下载 联合文件系统
f23cbf2ecc5d: Pull complete
30cfc6c29c0a: Pull complete
b38609286cbe: Pull complete
8211d9e66cd6: Pull complete
2313f9eeca4a: Pull complete
7eb487d00da0: Pull complete
a5d2b117a938: Pull complete
1f6cb474cd1c: Pull complete
896b3fd2ab07: Pull complete
532e67ebb376: Pull complete
233c7958b33f: Pull complete
Digest: sha256:5d52dc010398db422949f079c76e98f6b62230e5b59c0bf7582409d2c85abacb
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
下载制定版本
[root@localhost admin]# docker pull mysql:5.7
5.7: Pulling from library/mysql
b380bbd43752: Already exists
f23cbf2ecc5d: Already exists
30cfc6c29c0a: Already exists
b38609286cbe: Already exists
8211d9e66cd6: Already exists
2313f9eeca4a: Already exists
7eb487d00da0: Already exists
bb9cc5c700e7: Pull complete
88676eb32344: Pull complete
8fea0b38a348: Pull complete
3dc585bfc693: Pull complete
Digest: sha256:b8814059bbd9c80b78fe4b2b0b70cd70fe3772b3c5d8ee1edfa46791db3224f9
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rim
删除镜像
[root@localhost admin]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 8a8a506ccfdc 34 hours ago 448MB
mysql latest 9da615fced53 34 hours ago 514MB
busybox latest 42b97d3c2ae9 7 weeks ago 1.24MB
continuumio/miniconda3 latest 67414e5844b6 2 months ago 391MB
hello-world latest d1165f221234 7 months ago 13.3kB
[root@localhost admin]# docker rmi 8a8a506ccfdc
Untagged: mysql:5.7
Untagged: mysql@sha256:b8814059bbd9c80b78fe4b2b0b70cd70fe3772b3c5d8ee1edfa46791db3224f9
Deleted: sha256:8a8a506ccfdc7699b62e0818774fa06c7e1f30d17b4695d2c8be42848870e2ef
Deleted: sha256:ba884392d0236efc0b56a5177c1d95fa11627aeeb1a094ddb6b79af8a974f239
Deleted: sha256:e24812a440029006d5515d620552882486acac4685ddc1cee8c41114871d5b33
Deleted: sha256:e5bf95b5be3c9b3a31d22bee3844f30b7eabf1d56186ab13df5fd7635f3e5947
Deleted: sha256:57cd1e9fd1efe26b2bd726093c561c7ff90edbb3392e6ab94dc54a669b45a489
[root@localhost admin]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 9da615fced53 34 hours ago 514MB
busybox latest 42b97d3c2ae9 7 weeks ago 1.24MB
continuumio/miniconda3 latest 67414e5844b6 2 months ago 391MB
hello-world latest d1165f221234 7 months ago 13.3kB
删除全部镜像,递归删除
docker rmi -f $(docker images -aq)
# 解释
$() 将括号中的命令,作为参数传入到前面的命令中
容器命令
容器是由镜像运行的得到的
通过容器内虚拟centos命令为例
1、下载centos镜像 docker pull
[root@localhost admin]# docker pull centos
2、 启动镜像 docker run
docker run [可选参数] images
# 参数说明
--name="name" 容器名称,用来区分容器
-d 后台运行
-it 使用交互模式运行
-p 指定容器的端口
-p 主机端口:容器端口 (常用端口指令)
-p ip:主机端口:容器端口
-p 容器端口
************************************************************************************
[root@localhost admin]# docker run -it centos /bin/bash
[root@c1db162e7f95 /]# ls #查看容器内的centos
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
3、退出命令 exit
exit 退出后,容器停止
[root@c1db162e7f95 /]# exit
ctrl + p + q 快捷键退出,并保留容器运行
[root@localhost admin]# docker run -it centos /bin/bash
[root@bcac99cb042b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@bcac99cb042b /]# ls[root@localhost admin]#
[root@localhost admin]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bcac99cb042b centos "/bin/bash" 30 seconds ago Up 28 seconds fervent_williams
4、容器查看命令 docker ps
docker ps
# 可选项
-a # 列出当前正在运行的容器,历史运行过的容器
# 列出当前正在运行的容器
-n # 显示最近创建的容器
-q # 只显示容器的编号
5、删除容器 docker rm
docker rm 容器id 不能删除运行中的容器,
docker rm -f $(docker ps -aq) 强制删除所有容器
docker ps -a -q|xargs docker rm 删除所有容器
6、启动停止容器
docker start 容器ID
docker restart 容器ID
docker stop 容器ID
docker kill 容器ID
常用的其他命令
后台启动命令 docker run -d
docker 容器使用后台运行,必须有一个前台运行,没有前台服务,会自动停止
查看日志 docker logs
docker logs [可选参数] 容器ID
-t 显示日志时间戳
-f 显示日志信息
--tail 查看指定数量的日志
查看进程 docker top
查看镜像的源数据 docker inspect
docker inspect 容器ID
********************************************************************************************
[root@localhost admin]# docker inspect 40fb1d258c8e
[
{
"Id": "40fb1d258c8e5e1f784e2f5f44d084f4e5129e72222957bb9f950c133ae53540",
"Created": "2021-10-14T03:19:13.788858888Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true; do echo 1111111111;sleep 1;done"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2021-10-14T03:19:14.064979882Z",
"FinishedAt": "2021-10-14T03:22:19.958226863Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/40fb1d258c8e5e1f784e2f5f44d084f4e5129e72222957bb9f950c133ae53540/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/40fb1d258c8e5e1f784e2f5f44d084f4e5129e72222957bb9f950c133ae53540/hostname",
"HostsPath": "/var/lib/docker/containers/40fb1d258c8e5e1f784e2f5f44d084f4e5129e72222957bb9f950c133ae53540/hosts",
"LogPath": "/var/lib/docker/containers/40fb1d258c8e5e1f784e2f5f44d084f4e5129e72222957bb9f950c133ae53540/40fb1d258c8e5e1f784e2f5f44d084f4e5129e72222957bb9f950c133ae53540-json.log",
"Name": "/zealous_knuth",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/fb5fcdce8f86bcc7a08e5d0bbfa28517453ee61173517b672f71ee23cb537acb-init/diff:/var/lib/docker/overlay2/bb89e16b7df9fec851b172a4a11ae54fab7a3f903a5e16498363ad6984650da2/diff",
"MergedDir": "/var/lib/docker/overlay2/fb5fcdce8f86bcc7a08e5d0bbfa28517453ee61173517b672f71ee23cb537acb/merged",
"UpperDir": "/var/lib/docker/overlay2/fb5fcdce8f86bcc7a08e5d0bbfa28517453ee61173517b672f71ee23cb537acb/diff",
"WorkDir": "/var/lib/docker/overlay2/fb5fcdce8f86bcc7a08e5d0bbfa28517453ee61173517b672f71ee23cb537acb/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "40fb1d258c8e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash",
"-c",
"while true; do echo 1111111111;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "090238ae08b71b5935dbf49867eb2b36a8b1fe7795a7f6dc5fa594b04712fe7b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/090238ae08b7",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "eeecf069e31eeecbb168c439fadfae0ba4942552444485ee4772719a2789aeb3",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
进入运行中的容器 docker exec & docker attach
docker exec -it 容易ID
*******************************************************************************************************
root@localhost admin]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75eda5fc3c9a centos "/bin/bash" 15 seconds ago Up 15 seconds vigorous_cohen
[root@localhost admin]# docker exec -it 75eda5fc3c9a /bin/bash
[root@75eda5fc3c9a /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@75eda5fc3c9a /]#
[root@localhost admin]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75eda5fc3c9a centos "/bin/bash" 2 minutes ago Up 2 minutes vigorous_cohen
[root@localhost admin]# docker attach 75eda5fc3c9a
区别:
docker exec :进入容器后,开启一个新的终端(常用命令)
docker attach: 进入正在执行的容器,
文件拷贝 docker cp
docker cp 容器id:容器文件路径 目的的主机路径
[root@localhost admin]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75eda5fc3c9a centos "/bin/bash" 6 minutes ago Up 6 minutes vigorous_cohen
[root@localhost home]# docker attach 75eda5fc3c9a
[root@75eda5fc3c9a /]# cd /home
[root@75eda5fc3c9a home]# touch test.java
[root@75eda5fc3c9a home]# ls
test.java
[root@75eda5fc3c9a home]# exit
exit
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost home]# docker cp 75eda5fc3c9a:/home/test.java /home
[root@localhost home]# ls
admin kaunshen.java test.java