一、官方命令文档
https://docs.docker.com/reference/
二、基础命令
1.查看docker版本信息
docker version
2.查看docker详细信息
docker info
3.docker帮助命令
docker --help
三、镜像命令
1.查看本机镜像
docker images
此命令能够查看本机上所有的镜像信息
REPSOITORY:镜像的仓库 ,TAG:镜像的标签 ,IMAGE ID:镜像ID,CREATED:镜像的创建时间,SIZE:镜像的大小
此命令还可以携带一些参数,可以使用下面命令查看:
docker images --help
2.从docker仓库搜索镜像
基础命令
docker search 镜像名称
可携带参数
3.从docker仓库下载镜像
docker pull 镜像名称[:版本]
版本为可选项,若不携带版本则默认下载最新版本镜像。
4.从docker删除镜像
删除一个镜像:
docker rmi -f 镜像id
删除多个镜像,多个镜像用空格分割: docker rmi -f 镜像id...
删除全部镜像: docker rmi -f $(docker images -aq)
四、容器命令
1.创建容器并启动
docker run [参数] 镜像名 [命令行]
一些常用的参数如下:
--name 容器名
-d 后台运行
-p 指定容器端口
-P 随机指定容器端口
-it 交互式启动容器,也就是进入容器中
下面是简单的示例:
创建并启动容器,然后进入容器中
[root@VM-8-4-centos ~]# docker run -it --name=CENTOS01 centos /bin/bash
这里是启动了一个centos的容器,使用ls查看一下这个系统的目录
[root@2e6b7edf311c /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
退出并停止当前容器
[root@2e6b7edf311c /]# exit
exit
run命令就是将镜像存放到容器中并启动容器
2.查看容器
查看运行中的容器:
docker ps
一些常用参数:
-a 查看所有容器
-n=? 查看最近的n个容器
[root@VM-8-4-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-8-4-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e6b7edf311c centos "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago CENTOS01
34fac60877dd centos "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago stoic_lehmann
f356c576e824 centos "binbash" 5 minutes ago Created quirky_mclean
81558c767791 centos "/bin/bash" 8 minutes ago Exited (0) 8 minutes ago xnj2
df49592f7dc5 centos "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago xnj
697e0f564d5a hello-world "/hello" 3 hours ago Exited (0) 3 hours ago upbeat_hodgkin
[root@VM-8-4-centos ~]# docker ps -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e6b7edf311c centos "/bin/bash" 7 minutes ago Exited (0) 7 minutes ago CENTOS01
34fac60877dd centos "/bin/bash" 8 minutes ago Exited (0) 8 minutes ago stoic_lehmann
3.退出容器
退出并停止容器:
exit
退出但不停止容器:
Ctrl+P+Q
4.删除容器
基本删除命令:
docker rm 容器id
删除所有容器:
docker rm $(docker ps -aq)
未停止的容器无法删除,强制删除需要添加-f参数
5.启动和停止容器
启动容器:
docker start 容器id
停止容器:
docker stop 容器id
重启容器:
docker restart 容器id
强制停止容器:
docker kill 容器id
run和start的区别:run是将镜像放到容器中并启动,start是启动已经存在的容器
6.后台启动容器
docker run -d 镜像名称
此处后台创建并启动了一个容器,查看它却并没有运行,再查看所有容器发现它已经停止了
[root@VM-8-4-centos ~]# docker run -d centos
94c2680ad7932b7b370c4f0daff4839cbd602fa9c813b27b16976ff7657e668a
[root@VM-8-4-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-8-4-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94c2680ad793 centos "/bin/bash" 14 seconds ago Exited (0) 13 seconds ago intelligent_newton
这是因为docekr后台启动一个容器,如果这个容器中没有任何服务在运行,docekr就会把这个容器停止掉。
这里利用循环脚本建立一个一直运行的容器,再来查看发现它的确是在运行的
[root@VM-8-4-centos ~]# docker run -d centos /bin/sh -c "while true;do echo jsm;sleep 2;done"
417777cae12048333bc94d549051da0893a1749cbf40c8f2367aaf3dabf4bc9c
[root@VM-8-4-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
417777cae120 centos "/bin/sh -c 'while t…" 6 seconds ago Up 5 seconds boring_ellis
7.查看日志
docker logs 容器id
常用参数:
-f 跟随日志输入
--tail=? 输出日志的数量,默认是全部
-t 输出日志的时间
这里还是以上面6创建的容器测试
[root@VM-8-4-centos ~]# docker logs -t -f --tail=5 417777cae120
2023-06-07T14:39:36.551503694Z jsm
2023-06-07T14:39:38.554105234Z jsm
2023-06-07T14:39:40.556248379Z jsm
2023-06-07T14:39:42.558505271Z jsm
2023-06-07T14:39:44.561115787Z jsm
这里因为tail等于5所以输出5条日志,但因为-f所以后续日志会继续显示出来
8.查看容器中的进程信息
docker top 容器id
[root@VM-8-4-centos ~]# docker top 417777cae120
UID PID PPID C STIME TTY TIME CMD
root 29145 29126 0 22:37 ? 00:00:00 /bin/sh -c while true;do echo jsm;sleep 2;done
root 30784 29145 0 22:43 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2
9.查看容器的元数据
docker inspect 容器id
[root@VM-8-4-centos ~]# docker inspect 417777cae120
[
{
"Id": "417777cae12048333bc94d549051da0893a1749cbf40c8f2367aaf3dabf4bc9c",
"Created": "2023-06-07T14:37:22.146370585Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo jsm;sleep 2;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 29145,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-06-07T14:37:22.398892474Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/417777cae12048333bc94d549051da0893a1749cbf40c8f2367aaf3dabf4bc9c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/417777cae12048333bc94d549051da0893a1749cbf40c8f2367aaf3dabf4bc9c/hostname",
"HostsPath": "/var/lib/docker/containers/417777cae12048333bc94d549051da0893a1749cbf40c8f2367aaf3dabf4bc9c/hosts",
"LogPath": "/var/lib/docker/containers/417777cae12048333bc94d549051da0893a1749cbf40c8f2367aaf3dabf4bc9c/417777cae12048333bc94d549051da0893a1749cbf40c8f2367aaf3dabf4bc9c-json.log",
"Name": "/boring_ellis",
"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,
"ConsoleSize": [
42,
187
],
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"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/87f18c0e61b970f72054fc2efce0807a612a2a0511290115216167dc43b48a36-init/diff:/var/lib/docker/overlay2/b06bc18a7239c86f846f722aa7b23fd0443808aaa87b0c3efb7262b709638a8a/diff",
"MergedDir": "/var/lib/docker/overlay2/87f18c0e61b970f72054fc2efce0807a612a2a0511290115216167dc43b48a36/merged",
"UpperDir": "/var/lib/docker/overlay2/87f18c0e61b970f72054fc2efce0807a612a2a0511290115216167dc43b48a36/diff",
"WorkDir": "/var/lib/docker/overlay2/87f18c0e61b970f72054fc2efce0807a612a2a0511290115216167dc43b48a36/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "417777cae120",
"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 jsm;sleep 2;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": "7e6f76806440957c2e92b31900c09cea7a85557c5a582eac5a25ba32c8ac72f3",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/7e6f76806440",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "c41a012e511025373507bc4b89e3f0b2e3f3bad709f395588deb982ca254be1e",
"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": "105fe3d66e8af405f7aaadf4d32e9cda5125eb86e36be1e42756e201d007828a",
"EndpointID": "c41a012e511025373507bc4b89e3f0b2e3f3bad709f395588deb982ca254be1e",
"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
}
}
}
}
]
10.进入正在运行的容器
方法一:
docker exec -it 容器id 命令行
此方法是进入容器并开启一个新的中断
[root@VM-8-4-centos ~]# docker exec -it 417777cae120 /bin/bash
[root@417777cae120 /]# exit
exit
方法二:
docker attach 容器id
此方法是进入容器正在运行的终端
[root@VM-8-4-centos ~]# docker attach 417777cae120
jsm
jsm
11.将容器中的数据拷贝到主机上
docker cp 容器id:容器文件路径 主机路径
[root@VM-8-4-centos ~]# cd /home
[root@VM-8-4-centos home]# docker run -it centos /bin/bash
[root@bb413042f962 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@bb413042f962 /]# touch /home/java.txt
[root@bb413042f962 /]# ls /home
java.txt
[root@bb413042f962 /]# [root@VM-8-4-centos home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb413042f962 centos "/bin/bash" About a minute ago Up About a minute quirky_ptolemy
417777cae120 centos "/bin/sh -c 'while t…" 31 minutes ago Up 31 minutes boring_ellis
[root@VM-8-4-centos home]# docker cp bb413042f962:/home/java.txt /home
Successfully copied 1.54kB to /home
[root@VM-8-4-centos home]# ls /home
java.txt lighthouse
标签:bin,容器,centos,常用命令,Docker,null,root,docker From: https://www.cnblogs.com/jmsstudy/p/17464615.html