所有命令请查看帮助文档:https://docs.docker.com/engine/reference/commandline/docker/
docker version # 显示docker的版本信息
docker info #显示docker的系统信息
docker 命令 --help # 万能命令
列出所有容器的仓库来源,标签,ID,创建时间,大小
可选项
Name, shorthand | Default | Description |
---|---|---|
--all , -a |
Show all images (default hides intermediate images) | |
--digests |
Show digests | |
--filter , -f |
Filter output based on conditions provided | |
--format |
Pretty-print images using a Go template | |
--no-trunc |
Don’t truncate output | |
--quiet , -q |
Only show image IDs |
根据名字或标签列出镜像
# docker images 镜像名/标签[:版本]
docker images centos:latest
搜索镜像
docker search mysql
docker search mysql --limit 5
可选项
Name, shorthand | Default | Description |
---|---|---|
--filter , -f |
Filter output based on conditions provided | |
--format |
Pretty-print search using a Go template | |
--limitr |
25 | Max number of search results |
--no-trunct |
Don’t truncate output |
下载镜像
# docker pull 镜像名[:tag]
docker pull mysql
# 打印如下
Using default tag: latest # 如果不写,tag默认为latest
latest: Pulling from library/mysql
a10c77af2613: Pull complete # 分层下载,docker images的核心 联合文件系统
b76a7eb51ffd: Pull complete
258223f927e4: Pull complete
2d2c75386df9: Pull complete
63e92e4046c9: Pull complete
f5845c731544: Pull complete
bd0401123a9b: Pull complete
3ef07ec35f1a: Pull complete
c93a31315089: Pull complete
3349ed800d44: Pull complete
6d01857ca4c1: Pull complete
4cc13890eda8: Pull complete
Digest: sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
注意:mac m1没有mysql镜像没使用如下命令即可
docker pull --platform linux/x86_64 mysql
tag是版本号,默认是’latest ',即最新版,可以指定版本下载:
docker pull mysql:5.7
删除镜像,-f表示强制删除
docker rmi -f e6a0117ec169 # 通过ID删除
docker rmi -f 容器id 容器id # 删除多个
docker rmi -f $(docker images -aq) # 全部删除
有镜像才可以创建容器
先下载一个centos镜像
docker pull centos
运行一个容器
可选参数非常多,下面列出常用的
Name, shorthand | Default | Description |
---|---|---|
--name |
容器名字 | |
--detach , -d |
后台方式运行 | |
--interactive , -i |
交互方式运行 | |
--tty , -t |
为容器重新分配一个伪输入终端,通常与 -i 同时使用; | |
--publish , -p |
指定容器端口,主机端口:容器端口,如8080:8080 | |
--publish-all , -P |
随机指定端口 | |
--env , -e |
设置环境变量 | |
--volume , -v |
数据卷挂载 | |
--rm |
退出容器时自动删除,一般测试的时候使用 |
测试运行一个centos
test@testdeMacBook-Pro ~ % docker run -it centos /bin/bash
[root@4f61dc24a89a /]#
列出容器
docker ps # 列出正在运行的容器
docker ps -a # 列出正在运行的容器和历史运行过的容器
docker ps -n=? # 最近创建的n个容器
docker ps -q # 只显示容器编号
结果如下
test@testdeMacBook-Pro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f61dc24a89a centos "/bin/bash" 2 minutes ago Exited (0) About a minute ago gifted_tereshkova
1f79c5cbf344 centos "/bin/bash" 4 days ago Exited (255) 47 hours ago centos1
删除容器
docker rm 容器id # 删除指定容器,不能删除正在运行的容器
docker rm -f 容器id # 强制删除指定容器,可删除正在运行的容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id
退出容器
exit # 停止并退出容器
ctrl + p + q # 容器不停止退出
拷贝容器内文件到主机上:
docker cp 容器ID:容器内路径 目的主机路径
导出容器的内容留作为一个tar归档文件[对应import命令]
docker export 容器ID > 文件名.tar
如果容器被删除,可以使用import命令进行恢复容器,格式如下:
cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号
docker run -d centos
上面这种方式,发现centos停止了运行。因为docker容器使用后台运行,必须有一个前台进程,docker发现,没有应用,就会自动停止。使用docker run -it -d centos,其中 -it就是前台应用。
查看日志
docker logs [options] 容器ID
options:
Name, shorthand | Default | Description |
---|---|---|
--details |
Show extra details provided to logs | |
--follow , -f |
Follow log output | |
--since |
Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) | |
--tail , -n |
all |
Number of lines to show from the end of the logs |
--timestamps , -t |
Show timestamps | |
--until |
API 1.35+ Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) |
比如:
docker logs -tf --tail 10 8db2ecb06e9d
查看容器进程信息
docker top 容器ID
查看容器元信息
docker inspect 8db2ecb06e9d
# 包含容器信息,如完整ID,挂载,网络信息等
[
{
"Id": "8db2ecb06e9dac6ca503309cdb2214cccf48e3afc739dae2261c5c9503f87896",
"Created": "2021-11-27T02:32:16.337485884Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2077,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-11-27T02:32:16.526287176Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:e6a0117ec169eda93dc5ca978c6ac87580e36765a66097a6bfb6639a3bd4038a",
"ResolvConfPath": "/var/lib/docker/containers/8db2ecb06e9dac6ca503309cdb2214cccf48e3afc739dae2261c5c9503f87896/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/8db2ecb06e9dac6ca503309cdb2214cccf48e3afc739dae2261c5c9503f87896/hostname",
"HostsPath": "/var/lib/docker/containers/8db2ecb06e9dac6ca503309cdb2214cccf48e3afc739dae2261c5c9503f87896/hosts",
"LogPath": "/var/lib/docker/containers/8db2ecb06e9dac6ca503309cdb2214cccf48e3afc739dae2261c5c9503f87896/8db2ecb06e9dac6ca503309cdb2214cccf48e3afc739dae2261c5c9503f87896-json.log",
"Name": "/optimistic_cerf",
"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/3d09d6dad1162af25c63c5fffe7701a923a703d9bcf76970c6ae4fd38502c37b-init/diff:/var/lib/docker/overlay2/92c05e645000fce43a6680ce205c154dcb870178d3eb63487dd410802e45fc78/diff",
"MergedDir": "/var/lib/docker/overlay2/3d09d6dad1162af25c63c5fffe7701a923a703d9bcf76970c6ae4fd38502c37b/merged",
"UpperDir": "/var/lib/docker/overlay2/3d09d6dad1162af25c63c5fffe7701a923a703d9bcf76970c6ae4fd38502c37b/diff",
"WorkDir": "/var/lib/docker/overlay2/3d09d6dad1162af25c63c5fffe7701a923a703d9bcf76970c6ae4fd38502c37b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "8db2ecb06e9d",
"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": [
"/bin/bash"
],
"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": "79ad5f94eea2304ff998a4ed3bbb950778bc62de585e8f35683ff07cc28c67f3",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/79ad5f94eea2",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e61868a744567379d8e8762c86e8ef358ab687cd228a5983b0977bda8b21661b",
"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": "db82cd6e917deefa753ca81a9334ba4e17f76332ce7121edffab97f65a42c4d7",
"EndpointID": "e61868a744567379d8e8762c86e8ef358ab687cd228a5983b0977bda8b21661b",
"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 /bin/bash
也是进入正在运行的容器
docker attach -it 容器id
没有-it选项。与exec区别:exec进入后打开一个新的终端,attach进入容器正在运行的终端,不会启动新的进程。
从容器内拷贝文件到主机
docker cp 容器id:容器内路径 目的的主机路径
拷贝是一个手动过程,后面学习数据卷挂载,可以实现容器内和主机文件同步。