Docker的常用命令
帮助命令
docker version ##显示docker的版本信息
docker info ##显示docker的系统信息,包括镜像和容器的数量等
docker --help ##帮助命令
文档地址: https://docs.docker.com/engine/reference/commandline/docker/
镜像命令
docker images 查询所有本地的主机上的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 13 months ago 13.3kB
[root@localhost ~]# [root@localhost ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-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
##############################################################
[root@localhost ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 13 months ago 13.3kB
[root@localhost ~]# docker images -q
feb5d9fea6a5
[root@localhost ~]# docker images -aq
feb5d9fea6a5
docker search 搜索镜像
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13409 [OK]
mariadb MariaDB Server is a high performing open sou… 5114 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 672 [OK]
percona Percona Server is a fork of the MySQL relati… 592 [OK]
bitnami/mysql Bitnami MySQL Docker Image 78 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 74
linuxserver/mysql-workbench 45
ubuntu/mysql MySQL open source fast, stable, multi-thread… 38
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 37
circleci/mysql MySQL is a widely used, open-source relation… 28
google/mysql MySQL server for Google Compute Engine 21 [OK]
rapidfort/mysql RapidFort optimized, hardened image for MySQL 13
bitnami/mysqld-exporter 4
ibmcom/mysql-s390x Docker image for mysql-s390x 2
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK]
vitess/mysqlctld vitess/mysqlctld 1 [OK]
hashicorp/mysql-portworx-demo 0
docksal/mysql MySQL service images for Docksal - https://d… 0
rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ… 0
mirantis/mysql 0
cimg/mysql 0
drud/mysql 0
silintl/mysql-backup-restore Simple docker image to perform mysql backups… 0 [OK]
corpusops/mysql https://github.com/corpusops/docker-images/ 0
drud/mysql-local-57 ddev mysql local container 0
###########################################
[root@localhost ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the 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 (default 25)
--no-trunc Don't truncate output
[root@localhost ~]# docker search mysql -f=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13409 [OK]
mariadb MariaDB Server is a high performing open sou… 5114 [OK]
docker pull 拉取镜像
[root@localhost ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
[root@localhost ~]# docker pull mysql
Using default tag: latest #命令没有加:tag, 默认是latest
latest: Pulling from library/mysql
d67a603b911a: Pull complete #分层下载,docker image的核心,联合文件系统
0cf69c8f1492: Pull complete
a5ee239a0d3a: Pull complete
0f166cb3e327: Pull complete
882d294bf188: Pull complete
2649fc7eb806: Pull complete
bddb3394e2e3: Pull complete
93c83d9a2206: Pull complete
99d7f45787c0: Pull complete
234663a2e3ee: Pull complete
74531487bb7b: Pull complete
Digest: sha256:d4055451e7f42869e64089a60d1abc9e66eccde2910629f0dd666b53a5f230d8 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest 真实地址
#两命令等同
docker pull mysql
docker pull docker.io/library/mysql:latest
#指定版本下载
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
c19d5474d2cf: Pull complete
734cfb67bc8c: Pull complete
729529a0299e: Pull complete
63d3c603a591: Pull complete
305e0fbc2dcb: Pull complete
db7d48a00fea: Pull complete
fbf883b19c2f: Pull complete
28356e179593: Pull complete
49abbf373312: Pull complete
6ce6621986fd: Pull complete
d3d631e923a6: Pull complete
Digest: sha256:f5e2d4d7dccdc3f2a1d592bd3f0eb472b2f72f9fb942a84ff5b5cc049fe63a04
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c2c2eba5ae85 3 days ago 535MB
mysql 5.7 14905234a4ed 9 days ago 495MB
hello-world latest feb5d9fea6a5 13 months ago 13.3kB
docker rmi 删除镜像
[root@localhost ~]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
[root@localhost ~]# docker rmi -f 镜像id #删除指定的镜像
[root@localhost ~]# docker rmi -f 镜像id 镜像id 镜像id 镜像id... #删除多个镜像
[root@localhost ~]# docker rmi -f $(docker images -aq) #删除全部的镜像
容器命令
下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
-P 随机指定端口
# 测试
# 启动并进入容器, 主机名就是image id
[root@localhost ~]# docker run -it centos /bin/bash
[root@774cbec078aa /]#
[root@774cbec078aa /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 推出容器回到主机
[root@774cbec078aa /]# exit
exit
[root@localhost ~]#
列出所有运行的容器
# docker ps 命令
#列出当前正在运行的容器
-a #列出当前正在运行的容器+历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
774cbec078aa centos "/bin/bash" 5 minutes ago Exited (127) 4 minutes ago bold_chatelet
422de597cb92 hello-world "/hello" 3 days ago Exited (0) 3 days ago cool_shockley
[root@localhost ~]# docker ps -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
774cbec078aa centos "/bin/bash" 6 minutes ago Exited (127) 4 minutes ago bold_chatelet
[root@localhost ~]# docker ps -q
退出容器
exit #直接容器停止并退出
Ctrl + P + Q #容器不停止退出
删除容器
docker rm 容器id #删除指定容器,但是不能删除正在运行的容器
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有容器
启动和停止容器的操作
docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id
[root@localhost ~]# docker start 774cbec078aa
774cbec078aa
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
774cbec078aa centos "/bin/bash" 14 minutes ago Up 7 seconds bold_chatelet
[root@localhost ~]# docker restart 774cbec078aa
774cbec078aa
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
774cbec078aa centos "/bin/bash" 14 minutes ago Up 2 seconds bold_chatelet
[root@localhost ~]# docker stop 774cbec078aa
774cbec078aa
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
常用的其他命令
后台启动容器
# 命令 docker run -d 镜像名
[root@localhost ~]# docker run -d centos
# 问题docker ps,发现centos 停止了
# 常见的坑,docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx, 容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -tf --tail 数量 容器
######
[root@localhost ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
#自己编写一段shell脚本
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo lukistsai;sleep 1;done"
384f59f79ddd545e5407cd6328d298d265ba955c77016fa4a9b7889ef0c1a5e8
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
384f59f79ddd centos "/bin/sh -c 'while t…" 4 seconds ago Up 4 seconds romantic_zhukovsky
#查看日志
[root@localhost ~]# docker logs -tf --tail 10 384f59f79ddd
2022-10-31T06:16:37.111920805Z lukistsai
2022-10-31T06:16:38.119371092Z lukistsai
2022-10-31T06:16:39.126134517Z lukistsai
2022-10-31T06:16:40.132974741Z lukistsai
2022-10-31T06:16:41.138539349Z lukistsai
2022-10-31T06:16:42.141941100Z lukistsai
2022-10-31T06:16:43.160891845Z lukistsai
2022-10-31T06:16:44.167536200Z lukistsai
2022-10-31T06:16:45.174526886Z lukistsai
2022-10-31T06:16:46.179698337Z lukistsai
2022-10-31T06:16:47.185484133Z lukistsai
2022-10-31T06:16:48.189048567Z lukistsai
2022-10-31T06:16:49.197561780Z lukistsai
2022-10-31T06:16:50.202129839Z lukistsai
2022-10-31T06:16:51.209338716Z lukistsai
2022-10-31T06:16:52.214680410Z lukistsai
2022-10-31T06:16:53.218356636Z lukistsai
2022-10-31T06:16:54.224722473Z lukistsai
2022-10-31T06:16:55.230238339Z lukistsai
2022-10-31T06:16:56.233787634Z lukistsai
查看容器中进程信息
docker top
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo lukistsai;sleep 1;done"
b41fcb8650504bf25f4cbcda9ae433c3ba81339d56e0148454faf2b71c67b6ef
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b41fcb865050 centos "/bin/sh -c 'while t…" 4 seconds ago Up 3 seconds laughing_proskuriakova
[root@localhost ~]# docker top b41fcb865050
UID PID PPID C STIME TTY TIME CMD
root 4575 4555 0 14:21 ? 00:00:00 /bin/sh -c while true;do echo lukistsai;sleep 1;done
root 4621 4575 0 14:21 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器的元数据
docker inspect 容器id
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b41fcb865050 centos "/bin/sh -c 'while t…" 3 minutes ago Up 3 minutes laughing_proskuriakova
[root@localhost ~]# docker inspect b41fcb865050
[
{
"Id": "b41fcb8650504bf25f4cbcda9ae433c3ba81339d56e0148454faf2b71c67b6ef",
"Created": "2022-10-31T06:21:15.826793856Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo lukistsai;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4575,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-10-31T06:21:16.27025115Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/b41fcb8650504bf25f4cbcda9ae433c3ba81339d56e0148454faf2b71c67b6ef/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/b41fcb8650504bf25f4cbcda9ae433c3ba81339d56e0148454faf2b71c67b6ef/hostname",
"HostsPath": "/var/lib/docker/containers/b41fcb8650504bf25f4cbcda9ae433c3ba81339d56e0148454faf2b71c67b6ef/hosts",
"LogPath": "/var/lib/docker/containers/b41fcb8650504bf25f4cbcda9ae433c3ba81339d56e0148454faf2b71c67b6ef/b41fcb8650504bf25f4cbcda9ae433c3ba81339d56e0148454faf2b71c67b6ef-json.log",
"Name": "/laughing_proskuriakova",
"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/cc358e3f4a8ed7fcdc020dfa2a3110d0d90ac8229129939ed9eab803c76b95c9-init/diff:/var/lib/docker/overlay2/4a12e18180f4d8af50789c34578aaec0d5938c4f6bcb1131d8a58232d6e2a011/diff",
"MergedDir": "/var/lib/docker/overlay2/cc358e3f4a8ed7fcdc020dfa2a3110d0d90ac8229129939ed9eab803c76b95c9/merged",
"UpperDir": "/var/lib/docker/overlay2/cc358e3f4a8ed7fcdc020dfa2a3110d0d90ac8229129939ed9eab803c76b95c9/diff",
"WorkDir": "/var/lib/docker/overlay2/cc358e3f4a8ed7fcdc020dfa2a3110d0d90ac8229129939ed9eab803c76b95c9/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "b41fcb865050",
"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/sh",
"-c",
"while true;do echo lukistsai;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": "1dd636bb9118581f4e1c9da07060d3cfbde2d38393d1710b038f988b56a00429",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/1dd636bb9118",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "589e20c30206bc458e5444e54511ebad4ddd0d46eb8852f43c63ed19e9354460",
"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": "40f86512c8ad59ca7c643c06229af95974c02c66abd6ee3b2f06f76c0da54474",
"EndpointID": "589e20c30206bc458e5444e54511ebad4ddd0d46eb8852f43c63ed19e9354460",
"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 bashshell
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b41fcb865050 centos "/bin/sh -c 'while t…" 5 minutes ago Up 5 minutes laughing_proskuriakova
[root@localhost ~]# docker exec -it b41fcb865050 /bin/bash
[root@b41fcb865050 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@b41fcb865050 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:21 ? 00:00:00 /bin/sh -c while true;do echo lukistsai;sleep 1;done
root 391 0 0 06:27 pts/0 00:00:00 /bin/bash
root 417 1 0 06:27 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 418 391 0 06:27 pts/0 00:00:00 ps -ef
#命令
docker attach 容器id
[root@localhost ~]# docker attach b41fcb865050
正在执行当前代码
#docker exec #进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach #进入容器正在执行的终端,不会开启新的进程
从容器拷贝文件到主机
docker cp 容器id:容器内路径 目的的主机路径
[root@localhost ~]# docker run -it centos
[root@e72331ed3181 /]# cd home
[root@e72331ed3181 home]# ls
[root@e72331ed3181 home]# touch test.java
[root@e72331ed3181 home]# ls
test.java
[root@e72331ed3181 home]# exit
exit
[root@localhost ~]# cd /home
[root@localhost home]# ls
lukis
[root@localhost home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e72331ed3181 centos "/bin/bash" About a minute ago Exited (0) About a minute ago sharp_mccarthy
[root@localhost home]# docker cp e72331ed3181:/home/test.java /home/test.java
[root@localhost home]# ls
lukis test.java