docker常用命令
帮助命令
docker version
docker info
docker 命令 --help
镜像命令
docker images
docker search 镜像名
docker pull 镜像名
docker pull 镜像名:版本
docker rmi -f 镜像id
docker rmi -f 镜像id 镜像id 镜像id
docker rmi -f $(docker images -aq)
容器命令
新建容器并启动:
docker run [options] 镜像名 [command]
options:
--name=容器名
-d
-it
-p
-p 主机端口:容器端口
command:
/bin/bash
举例:
docker run -it centos /bin/bash
退出容器:
exit
Ctrl+P+Q
列出所有运行容器:
docker ps [options]
[oprions]:
-a
-n=条数
-q
删除容器
docker rm 容器id
docker rm -f 容器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
其他命令
后台启动容器
docker run -d 镜像名
例如:
docker run -d centos
日志查看
docker logs -t -f --tail number 容器id
说明:
-t:
-f:
--tail number:
例如:
首先运行一个容器:
docker run -d centos /bin/sh -c "while true;do echo xubin;sleep 1;done"
注:"while true;do echo xubin;sleep 1;done",shell脚本,循环打印xubin
查看其id:
docker ps
结果:
CONTAINER ID IMAGE
f14b51914b50 centos
查看其日志:
docker logs -tf --tail 10 f14b51914b50
结果:
2021-07-08T13:46:38.824429166Z xubin
2021-07-08T13:46:39.826570760Z xubin
2021-07-08T13:46:40.828772238Z xubin
2021-07-08T13:46:41.831111384Z xubin
2021-07-08T13:46:42.833243910Z xubin
2021-07-08T13:46:43.835402983Z xubin
2021-07-08T13:46:44.839101391Z xubin
2021-07-08T13:46:45.839585536Z xubin
2021-07-08T13:46:46.841750317Z xubin
2021-07-08T13:46:47.843940035Z xubin
查看容器中的进程信息
docker top 容器id
例如:
docker top f14b51914b50
结果:
UID(用户id) PID (进程id) PPID(父进程id) C STIME
root 29662 29641 0 21:46
查看镜像元数据
docker inspect 容器id
例如:
docker inspect f14b51914b50
结果:
[
{
"Id": "f14b51914b50ff40adffd78f1494ed6cc76596faa45c67f3380fa8f0c885897e",
"Created": "2021-07-08T13:46:11.435545355Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo xubin;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 29662,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-07-08T13:46:11.770180102Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/f14b51914b50ff40adffd78f1494ed6cc76596faa45c67f3380fa8f0c885897e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/f14b51914b50ff40adffd78f1494ed6cc76596faa45c67f3380fa8f0c885897e/hostname",
"HostsPath": "/var/lib/docker/containers/f14b51914b50ff40adffd78f1494ed6cc76596faa45c67f3380fa8f0c885897e/hosts",
"LogPath": "/var/lib/docker/containers/f14b51914b50ff40adffd78f1494ed6cc76596faa45c67f3380fa8f0c885897e/f14b51914b50ff40adffd78f1494ed6cc76
596faa45c67f3380fa8f0c885897e-json.log", "Name": "/sleepy_leakey",
"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/07e51fe7acae0373a3a578f7737191977e721f14dc7fbfa1dbff9c7535ecf4ef-init/diff:/var/lib/docker/ov
erlay2/72313cbc9e6f3eb02b24ed37697df9fc484fd59d0e29c878a347c707ea112438/diff", "MergedDir": "/var/lib/docker/overlay2/07e51fe7acae0373a3a578f7737191977e721f14dc7fbfa1dbff9c7535ecf4ef/merged",
"UpperDir": "/var/lib/docker/overlay2/07e51fe7acae0373a3a578f7737191977e721f14dc7fbfa1dbff9c7535ecf4ef/diff",
"WorkDir": "/var/lib/docker/overlay2/07e51fe7acae0373a3a578f7737191977e721f14dc7fbfa1dbff9c7535ecf4ef/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "f14b51914b50",
"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 xubin;sleep 1;done"
],
"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": "04aac9b1efefdf495e39931dbd3b62285e27bbfa85c1dfbcb0b9d71d475f07df",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/04aac9b1efef",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "b9e99e00bcb100aaadb1354211063e3f02d0eb718e6df34259db070c37a5415d",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "b14e3a71667d6c0f92e9f35c18b947f785fca7289a802dabcd02de2dfb923583",
"EndpointID": "b9e99e00bcb100aaadb1354211063e3f02d0eb718e6df34259db070c37a5415d",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
容器通常使用后台方式运行,修改配置时,需进入正在运行的容器
docker exec -it 容器id /bin/bash
docker attach 容器id
测试两者不同:
[root@iZ2zeaqrzkcxyxdaxp7gysZ ~]
[root@f14b51914b50 /]
exit
[root@iZ2zeaqrzkcxyxdaxp7gysZ ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f14b51914b50 centos "/bin/sh -c 'while t…" 40 minutes ago Up 40 minutes sleepy_leakey
b13432b75bc9 centos "/bin/bash" 29 hours ago Up About an hour quizzical_banach
[root@iZ2zeaqrzkcxyxdaxp7gysZ ~]
[root@b13432b75bc9 /]
exit
[root@iZ2zeaqrzkcxyxdaxp7gysZ ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f14b51914b50 centos "/bin/sh -c 'while t…" 45 minutes ago Up 45 minutes sleepy_leakey
[root@iZ2zeaqrzkcxyxdaxp7gysZ ~]
结论:
在exit时,前者不会停止原本的进程,而后者会停止,
从容器内拷贝文件到主机
docker cp 容器id:文件在容器内的路径 目的主机路径
例如:
进入容器
docker attach b13432b75bc9
到容器home目录下
cd /home
创建一个文件
touch a.java
退出容器
exit
从容器中copy刚刚新建的文件到主机的home目录下
docker cp b13432b75bc9:/home/a.java /home
进入主机home目录
cd /home
查看结果
ls
小结
|