Docker的常用命令
文章目录
- Docker的常用命令
- 帮助命令
- 镜像命令
- docker search 搜索镜像
- docker pull 下载镜像
- docker rmi 删除镜像
- 容器的命令
- 常用其他命令
- 总结
帮助命令
#显示docker的版本信息
在docker version
#显示docker的系统信息,包括镜像和容器的数量
docker info
# 帮助命令
docker 命令 --help
帮助文档地址:https://docs.docker.com/reference/
镜像命令
docker images 查看所有本地的主机上的镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 6464f5e476fc 37 hours ago 381MB
<none> <none> f82b8575e06e 37 hours ago 231MB
hello-world latest feb5d9fea6a5 9 months ago 13.3kB
#解释
命令 | 说明 |
REPOSITORY | 镜像的仓库源 |
TAG | 镜像的标签 |
IMAGE ID | 镜像的id |
CREATED | 镜像的创建时间 |
SIZE | 镜像的大小 |
#选项
选项 | 说明 |
-a, --all | 列出所有镜像 |
[root@docker ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 9 months ago 13.3kB
#选项
选项 | 说明 |
-q,–quiet | 只显示镜像id |
[root@docker ~]# docker images -q
6464f5e476fc
f82b8575e06e
feb5d9fea6a5
eeb6ee3f44bd
5d0da3dc9764
docker search 搜索镜像
[root@docker ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 17061 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 169
bitnami/nginx Bitnami nginx Docker Image 133 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 52
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 18 [OK]
#选项
选项,通过收藏来过滤
选项 | 说明 |
–filter=STARS=3000 | 所搜出来的镜像就是START大于3000的 |
[root@docker ~]# docker search nginx --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 17061 [OK]
docker pull 下载镜像
#下载镜像 docker pull 镜像名[:tag]
[root@docker ~]# docker pull nginx
Using default tag: latest #如果不写tag,默认就是 latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete #分层下载,docker iamge的核心 联合文件系统
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 #签名
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest #真实地址
docker pull nginx等价于 docker.io/library/nginx:latest
指定版本
docker pull mysql:5.7
[root@docker ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
docker rmi -f 605c77e624dd(容器id) #删除指定的容器
docker rim 6464f5e476fc(容器id) f82b8575e06e(容器id)#删除多个容器
[root@docker ~]# docker rmi -f $(docker images -qa) #删除全部的容器
容器的命令
说明:我们有了镜像才可以创建容器,linux下载一个 centos 镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] images
#参数说明
选项 | 说明 |
-d | 后台方式运行 |
–name=“Name” | 容器名字 nginx1 nginx2,用来区分容器 |
-it | 使用交互方式运行,进入容器查看内容 |
-p | 指定容器的端口 -p 8080:8080 |
-p ip:主机端口:容器端口 | |
-p 主机端口:容器端口(常用) | |
-p 容器端口 | |
容器端口 | |
-P | 随机指定端口 |
#测试,启动并进入容器
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 9 months ago 231MB
root@docker ~]# docker run -it centos /bin/bash
[root@2452d7133536 /]# ls #查看容器内的centos,基础版本,很多命令都是不完善的!
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
#从容器中退回主机
[root@2452d7133536 /]# exit
exit
[root@docker ~]# ls
anaconda-ks.cfg 公共
dockerfile 密码.txt
example.c 模板
initial-setup-ks.cfg 视频
mysql80-community-release-el7-5.noarch.rpm 图片
mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm 文档
mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm.1 下载
mysql-community-common-8.0.26-1.el7.x86_64.rpm 音乐
mysql-community-libs-compat-8.0.26-1.el7.x86_64.rpm 桌面
mysql-community-server-8.0.26-1.el7.x86_64.rpm
列出所有正在运行的容器
#docker ps 命令
选项 | 说明 |
列出当前正在运行的容器 | |
-a | 列出当前正在运行的容器+带出历史运行过的容器 |
-n=num | 显示最近创建的容器 |
-q | 只显示容器的编号 |
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2452d7133536 centos "/bin/bash" 4 minutes ago Exited (0) About a minute ago sad_neumann
573f2e16ec85 feb5d9fea6a5 "/hello" 12 hours ago Exited (0) 12 hours ago cranky_blackwell
80de298af53d 6464f5e476fc "/bin/sh -c 'yum ins…" 38 hours ago Exited (1) 38 hours ago tender_wozniak
b002e83a441a 6464f5e476fc "/bin/sh -c 'yum ins…" 38 hours ago Exited (1) 38 hours ago sharp_lamarr
d8153531ef13 6464f5e476fc "/bin/sh -c 'yum ins…" 38 hours ago Exited (1) 38 hours ago sleepy_wescoff
2afedc281078 6464f5e476fc "/bin/sh -c 'yum ins…" 38 hours ago Exited (1) 38 hours ago mystifying_bouman
a7dac2495d51 6464f5e476fc "/bin/sh -c 'yum ins…" 38 hours ago Exited (1) 38 hours ago eager_greider
4442b28886d9 f82b8575e06e "/bin/sh -c 'yum ins…" 38 hours ago Exited (1) 38 hours ago hungry_gauss
a1df0ab8ddce f82b8575e06e "/bin/sh -c 'yum ins…" 38 hours ago Exited (1) 38 hours ago lucid_curie
退出容器
直接容器停止并退出
exit
容器不停止退出
Ctrl + P + Q
删除容器
docker rm 容器id
#删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -qa) #删除所有的容器
docker ps -a -q |xargs docker rm
#删除所有的容器
启动和停止容器的操作
docker start 容器di #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
常用其他命令
后台启动容器
命令:
docker run -d 镜像名
[root@docker ~]# docker run -d centos
#问题docker ps ,发现centos 停止了
#常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止 #nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -f -t --tail 容器,没有日志
#自己编写一段脚本
[root@docker ~]# docker run -d centos /bin/sh -c "while ture;do echo guanguan;sleep 1;done"
[root@docker ~]# docker ps
CONTAINER ID IMAGE
13acd7fc0e24 centos
显示日志
选项 | 说明 |
-tf | 显示日志 |
–tail number | 要显示日志条数 |
docker logs -tf --tail 10 13acd7fc0e24
查看容器中的进程信息 ps
命令:
docker top 容器id
[root@docker ~]# docker top 73f07d7e6bef
UID PID PPID C STIME TTY TIME CMD
root 30333 30311 0 14:18 pts/0 00:00:00 /bin/bash
查看镜像元数据
命令:
docker inspect 73f07d7e6bef
测试
[root@docker ~]# docker inspect 73f07d7e6bef
[
{
"Id": "73f07d7e6bef26b0737b5d4cda59d58144126a1d22ca6ee4b891846ec51deeec",
"Created": "2022-07-08T06:18:47.258305Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 30333,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-07-08T06:18:47.980418448Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/73f07d7e6bef26b0737b5d4cda59d58144126a1d22ca6ee4b891846ec51deeec/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/73f07d7e6bef26b0737b5d4cda59d58144126a1d22ca6ee4b891846ec51deeec/hostname",
"HostsPath": "/var/lib/docker/containers/73f07d7e6bef26b0737b5d4cda59d58144126a1d22ca6ee4b891846ec51deeec/hosts",
"LogPath": "/var/lib/docker/containers/73f07d7e6bef26b0737b5d4cda59d58144126a1d22ca6ee4b891846ec51deeec/73f07d7e6bef26b0737b5d4cda59d58144126a1d22ca6ee4b891846ec51deeec-json.log",
"Name": "/sweet_burnell",
"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/49b6da9937b4d65052d23494138b01e8c05a302cb7102aff2753a5e98e2436b4-init/diff:/var/lib/docker/overlay2/2ac5941947ecb873c661c04c597cfd83404749f3e442894af6ea10ae366768c1/diff",
"MergedDir": "/var/lib/docker/overlay2/49b6da9937b4d65052d23494138b01e8c05a302cb7102aff2753a5e98e2436b4/merged",
"UpperDir": "/var/lib/docker/overlay2/49b6da9937b4d65052d23494138b01e8c05a302cb7102aff2753a5e98e2436b4/diff",
"WorkDir": "/var/lib/docker/overlay2/49b6da9937b4d65052d23494138b01e8c05a302cb7102aff2753a5e98e2436b4/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "73f07d7e6bef",
"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": "65dd9f57692f637db9cd0f0eb9114e5df0b7560054fc952496c45ec21b04889a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/65dd9f57692f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "c9eb643dc861b63405040a6f1eff32e3ea727ce631f9ff30a5d1d4b979bfe221",
"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": "64b4457bc3b623247e4d281f85c4c16eee75cfde3cca87416ca90c6ed4590cdb",
"EndpointID": "c9eb643dc861b63405040a6f1eff32e3ea727ce631f9ff30a5d1d4b979bfe221",
"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@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
73f07d7e6bef centos "/bin/bash" 7 minutes ago Up 7 minutes sweet_burnell
[root@docker ~]# docker exec -it 73f07d7e6bef /bin/bash
[root@73f07d7e6bef /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@73f07d7e6bef /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:18 pts/0 00:00:00 /bin/bash
root 15 0 0 06:27 pts/1 00:00:00 /bin/bash
root 30 15 0 06:27 pts/1 00:00:00 ps -ef
方式二
docker attach 容器id
测试
[root@docker ~]# docker attach 73f07d7e6bef
正在执行当前的代码…
docker exec #进入容器后开启一个新的终端,可以在里面操作(常用)
docker attach #进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机上
命令:
docker cp 容器id
[root@docker ~]# docker run -it centos /bin/bash
[root@8cabaded8052 /]# [root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8cabaded8052 centos "/bin/bash" 15 seconds ago Up 14 seconds busy_curie
查看当前主机目录下
[root@docker ~]# ls
anaconda-ks.cfg 公共
dockerfile 密码.txt
example.c 模板
initial-setup-ks.cfg 视频
mysql80-community-release-el7-5.noarch.rpm 图片
mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm 文档
mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm.1 下载
mysql-community-common-8.0.26-1.el7.x86_64.rpm 音乐
mysql-community-libs-compat-8.0.26-1.el7.x86_64.rpm 桌面
mysql-community-server-8.0.26-1.el7.x86_64.rpm
进入docker容器内部
[root@docker ~]# docker attach 8cabaded8052
[root@8cabaded8052 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
在容器内新建一个文件
[root@8cabaded8052 /]# touch test.c
[root@8cabaded8052 /]# ls
bin etc lib lost+found mnt proc run srv test.c usr
dev home lib64 media opt root sbin sys tmp var
[root@8cabaded8052 /]# exit
exit
将这个文件拷贝到主机上
[root@docker ~]# docker cp 8cabaded8052:/test.c /root
[root@docker ~]# ls
anaconda-ks.cfg test.c
dockerfile 公共
example.c 密码.txt
initial-setup-ks.cfg 模板
mysql80-community-release-el7-5.noarch.rpm 视频
mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm 图片
mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm.1 文档
mysql-community-common-8.0.26-1.el7.x86_64.rpm 下载
mysql-community-libs-compat-8.0.26-1.el7.x86_64.rpm 音乐
mysql-community-server-8.0.26-1.el7.x86_64.rpm 桌面
#拷贝是一个手动的过程,随着学习的深入,我们可以使用-volume卷的技术,实现自动拷贝
总结
本篇笔记是基于B站up主:遇见狂神说的笔记进行学习,感兴趣的可以去B站进行学习。