帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 帮助命令
镜像命令
docker images 查看所有本地的主机上的镜像
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a, --all Show all images (default hides intermediate images)
-q, --quiet Only show image IDs
docker search 查看所有本地的主机上的镜像
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11020 [OK]
mariadb MariaDB Server is a high performing open sou… 4176 [OK]
#可选项
-f, --filter filter Filter output based on conditions provided
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker search mysql --filter=STARS=6000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11020 [OK]
docker pull 下载镜像
#下载镜像 docker pull 镜像名[:tag]
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认为latest
latest: Pulling from library/mysql
69692152171a: Pull complete
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #镜像的真实地址
#下面两条命令等价
docker pull mysql
docker pull docker.io/library/mysql:latest
docker rmi 删除镜像
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker rmi -f c0cdc95609f1 #删除单个容器
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker rmi -f 容器id 容器id #删除多个容器
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker rmi -f $(docker images -qa) #删除全部容器
容器命令
docker pull centos
启动容器
docker run 镜像名称 #根据镜像创建一个容器并运行一个命令,操作的对象是 镜像
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker run -it centos bash
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker run -d --name nginx01 -p 3344:80 nginx
78b142b14b8900d166d03f323d4e225662c178ca1fa9b3bfac5737a9aa6774df
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78b142b14b89 nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp suspicious_jemison
#参数说明
--name="Name" 容器名字,用来区分容器
-d 后台方式运行
-it 使用交互方式运行
-p 指定容器的端口
#从容器中退回主机
[root@e5f399e99d8f /]# exit
列出所有运行的容器
docker ps
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker ps
#参数说明
docker ps #当前正在运行的容器
docker ps -a #当前正在运行的容器+利用运行过的容器
docker ps -q #只显示容器的ID
退出容器
exit # 直接停止容器并退出
Ctrl + P + Q # 不停止容器并退出
删除容器
docker rm 容器id #删除指定容器
docker rm -f $(docker images -qa) #删除全部容器
docker ps -a -q|xargs docekr rm #删除所有的容器
启动和停止容器操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行容器
docker kill 容器id #强制停止当前容器
常用其他命令
查看日志
dcoker logs 容器id
#参数说明
-tf #显示日志
--tail number #要显示的日志条数
查看容器中的进程信息
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker top 43d8d1ca8c97
UID PID PPID C STIME TTY TIME CMD
root 25022 24986 0 22:24 pts/0 00:00:00 bash
查看容器中的进程信息
docker inspect 容器id
[root@iZ8vbd1ko98b0el6771a37Z ~]# docker inspect 43d8d1ca8c97
[
{
"Id": "43d8d1ca8c97182f575cff41049454f47105cdf5cfe0d8755d1a3a497c89cf77",
"Created": "2021-06-20T14:24:55.198839733Z",
"Path": "bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 25022,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-06-20T14:24:55.681209633Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/43d8d1ca8c97182f575cff41049454f47105cdf5cfe0d8755d1a3a497c89cf77/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/43d8d1ca8c97182f575cff41049454f47105cdf5cfe0d8755d1a3a497c89cf77/hostname",
"HostsPath": "/var/lib/docker/containers/43d8d1ca8c97182f575cff41049454f47105cdf5cfe0d8755d1a3a497c89cf77/hosts",
"LogPath": "/var/lib/docker/containers/43d8d1ca8c97182f575cff41049454f47105cdf5cfe0d8755d1a3a497c89cf77/43d8d1ca8c97182f575cff41049454f47105cdf5cfe0d8755d1a3a497c89cf77-json.log",
"Name": "/bold_varahamihira",
"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/46891ba75193b467e8d07435900d8b8b8d6653410d2ea6e44876392720292df5-init/diff:/var/lib/docker/overlay2/3301e4c2e4827380b146308f68c11eb6fff48988000b5c2bd8ad2caa767d09e8/diff",
"MergedDir": "/var/lib/docker/overlay2/46891ba75193b467e8d07435900d8b8b8d6653410d2ea6e44876392720292df5/merged",
"UpperDir": "/var/lib/docker/overlay2/46891ba75193b467e8d07435900d8b8b8d6653410d2ea6e44876392720292df5/diff",
"WorkDir": "/var/lib/docker/overlay2/46891ba75193b467e8d07435900d8b8b8d6653410d2ea6e44876392720292df5/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "43d8d1ca8c97",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"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": "5bb5f74347c8897c6acdef0cb0aec30a814b1f421df4dd93d0ffba24743e9b54",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/5bb5f74347c8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "c999e5982bff7099dda2ed54db811606e68f485fe3e5fd38b23af4e53fdc7f65",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "1cfe54e4111499f41cf4d87279092ab998adc20e63e06980c5e5fb48d0b3d0fa",
"EndpointID": "c999e5982bff7099dda2ed54db811606e68f485fe3e5fd38b23af4e53fdc7f65",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
docker exec -it 容器id bash #在运行的容器中执行命令,操作的对象是容器
docker attach 容器id
# docker exec #进入容器后开启一个新的终端,可以在里面进行操作
# docker attach #进入容器正在执行的终端,不会启动新的进程
从容器拷贝文件到主机上
docker cp 容器id:容器内源地址 主机目标地址
小结
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