docker查看,删除,停止,暂停,恢复容器详解

一,查看容器(假设你有一个名为 `my_app_container` 的容器)。

  1. 查看所有容器(包括已停止的)

docker ps -a

输出示例:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

123456789abc my_app_image "npm start" 2 hours ago Up 2 hours 3000/tcp my_app_container

def4567890ab another_image "python app.py" 3 days ago Exited (0) 3 days ago another_container

  1. 获取容器的详细信息

docker inspect my_app_container

输出示例(部分):

[

{

"Id": "123456789abc",

"Created": "2023-10-10T12:34:56.789Z",

"Path": "npm",

"Args": ["start"],

"State": {

"Status": "running",

"Running": true,

"Paused": false,

"Restarting": false,

"OOMKilled": false,

"Dead": false,

"Pid": 1234,

"ExitCode": 0,

"Error": "",

"StartedAt": "2023-10-10T12:35:00.123Z",

"FinishedAt": "0001-01-01T00:00:00Z"

},

"Image": "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",

"ResolvConfPath": "/var/lib/docker/containers/123456789abc/resolv.conf",

"HostnamePath": "/var/lib/docker/containers/123456789abc/hostname",

"HostsPath": "/var/lib/docker/containers/123456789abc/hosts",

"LogPath": "/var/lib/docker/containers/123456789abc/123456789abc-.log",

"Name": "/my_app_container",

"RestartCount": 0,

"Driver": "overlay2",

"Platform": "linux",

"MountLabel": "",

"ProcessLabel": "",

"AppArmorProfile": "",

"ExecIDs": null,

"HostConfig": {

"Binds": null,

"ContainerIDFile": "",

"LogConfig": {

"Type": "-file",

"Config": {}

},

"NetworkMode": "default",

"PortBindings": {

"3000/tcp": [

{

"HostIp": "",

"HostPort": "3000"

}

]

},

"RestartPolicy": {

"Name": "no",

"MaximumRetryCount": 0

},

"AutoRemove": false,

"VolumeDriver": "",

"VolumesFrom": null,

"CapAdd": null,

"CapDrop": null,

"CgroupnsMode": "private",

"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/abcdef1234567890/diff:/var/lib/docker/overlay2/ghijklmnopqrstuv/diff",

"MergedDir": "/var/lib/docker/overlay2/123456789abc/merged",

"UpperDir": "/var/lib/docker/overlay2/123456789abc/diff",

"WorkDir": "/var/lib/docker/overlay2/123456789abc/work"

},

"Name": "overlay2"

},

"Mounts": [],

"Config": {

"Hostname": "123456789abc",

"Domainname": "",

"User": "",

"AttachStdin": false,

"AttachStdout": false,

"AttachStderr": false,

"ExposedPorts": {

"3000/tcp": {}

},

"Tty": false,

"OpenStdin": false,

"StdinOnce": false,

"Env": [

"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",

"NODE_ENV=production"

],

"Cmd": [

"npm",

"start"

],

"Image": "my_app_image",

"Volumes": null,

"WorkingDir": "/app",

"Entrypoint": null,

"OnBuild": null,

"Labels": {}

},

"NetworkSettings": {

"Bridge": "",

"SandboxID": "123456789abc",

"HairpinMode": false,

"LinkLocalIPv6Address": "",

"LinkLocalIPv6PrefixLen": 0,

"Ports": {

"3000/tcp": [

{

"HostIp": "0.0.0.0",

"HostPort": "3000"

}

]

},

"SandboxKey": "/var/run/docker/netns/123456789abc",

"SecondaryIPAddresses": null,

"SecondaryIPv6Addresses": null,

"EndpointID": "123456789abc",

"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": "123456789abc",

"EndpointID": "123456789abc",

"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

}

}

}

}

]

  1. 获取容器的IP地址

docker inspect -f '{{ .NetworkSettings.IPAddress }}' my_app_container

输出示例:

172.17.0.2

  1. 查看容器的日志输出

docker logs --tail 100 -f my_app_container

输出示例:

> my_app@1.0.0 start

> node app.js

Server running on port 3000

  1. 查看容器内的所有进程

docker top my_app_container

输出示例:

UID PID PPID C STIME TTY TIME CMD

root 1234 0 0 12:35 ? 00:00:00 npm start

node 1235 1234 0 12:35 ? 00:00:01 node app.js

  1. 实时显示容器的资源使用统计信息

docker stats my_app_container

输出示例:

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS

123456789abc my_app_container 0.10% 10.5MiB / 1.952GiB 0.52% 1.2kB / 0B 0B / 0B 2

  1. 列出容器的端口映射

docker port my_app_container

输出示例:

3000/tcp -> 0.0.0.0:3000

  1. 在容器中执行命令(进入容器的 Bash 终端)

docker exec -it my_app_container /bin/bash

输出示例(进入容器后的终端):

root@123456789abc:/app# ls

app.js package. node_modules

  1. 在容器中后台执行命令(创建一个测试文件)

docker exec -d my_app_container touch /tmp/testfile

验证:

docker exec -it my_app_container ls /tmp

输出示例:

testfile

  1. 实时显示 Docker 守护进程产生的事件(仅显示容器启动事件)

docker events --filter 'event=start'

输出示例:

2023-10-10T12:35:00.123456Z container start 123456789abc (image=my_app_image, name=my_app_container)

  1. 定期检查容器资源使用情况

watch -n 5 'docker stats --no-stream

输出示例(每5秒更新一次):

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS

123456789abc my_app_container 0.10% 10.5MiB / 1.952GiB 0.52% 1.2kB / 0B 0B / 0B 2

  1. 结合 `jq` 处理 `docker inspect` 输出

docker inspect my_app_container | jq '.[0].NetworkSettings.IPAddress'

输出示例:

"172.17.0.2"

  1. 查看容器的前100条日志,并显示时间戳

docker logs --tail 100 -t my_app_continer

输出示例:

2023-10-10T12:35:00.123456Z my_app_container Server running on port 3000

  1. 查看从某个时间点之后的容器日志

docker logs --since "2023-10-10" my_app_container

输出示例:

Server running on port 3000

  1. 查看容器的资源使用情况,不持续更新

docker stats --no-stream my_app_container

输出示例:

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS

123456789abc my_app_container 0.10% 10.5MiB / 1.952GiB 0.52% 1.2kB / 0B 0B / 0B 2

  1. 过滤显示特定类型的事件(例如容器启动事件)

docker events --filter 'event=start'

输出示例:

2023-10-10T12:35:00.123456Z container start 123456789abc (image=my_app_image, name=my_app_container)

  1. 查看容器的详细信息,格式化输出

docker inspect -f '{{ .Name }} {{ .State.Status }}' my_app_container

输出示例:

/my_app_container running

  1. 查看容器的磁盘使用情况

docker inspect -s my_app_container

输出示例:

false

  1. 在容器中以特定用户身份执行命令

docker exec -u root -it my_app_container /bin/bash

输出示例(进入容器后的终端):

root@123456789abc:/app# whoami

root

  1. 查看容器的环境变量

docker inspect -f '{{ .Config.Env }}' my_app_container

输出示例:

[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin NODE_ENV=production]

  1. 查看容器的挂载点

docker inspect -f '{{ .Mounts }}' my_app_container

输出示例:

[]

  1. 查看容器的网络配置

docker inspect -f '{{ .NetworkSettings }}' my_app_container

输出示例(部分):

{

"Bridge": "",

"SandboxID": "123456789abc",

"HairpinMode": false,

"LinkLocalIPv6Address": "",

"LinkLocalIPv6PrefixLen": 0,

"Ports": {

"3000/tcp": [

{

"HostIp": "0.0.0.0",

"HostPort": "3000"

}

]

},

"SandboxKey": "/var/run/docker/netns/123456789abc",

"SecondaryIPAddresses": null,

"SecondaryIPv6Addresses": null,

"EndpointID": "123456789abc",

"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": "123456789abc",

"EndpointID": "123456789abc",

"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

}

}

}

  1. 查看容器的镜像信息

docker inspect -f '{{ .Image }}' my_app_container

输出示例:

sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef

  1. 查看容器的创建时间

docker inspect -f '{{ .Created }}' my_app_container

输出示例:

2023-10-10T12:34:56.789Z

  1. 查看容器的重启策略

docker inspect -f '{{ .HostConfig.RestartPolicy }}' my_app_container

输出示例:

{Name:no MaximumRetryCount:0}

二.删除容器

示例:

删除所有容器

docker ps -aq |xargs docker rm -f 或者 docker rm -f `docker ps -aq`

删除停止状态容器

docker rm `docker ps -qf status=exited`

三.容器启动和停止 暂停恢复

批量启动所有容器

docker start `ps -aq`

批量停止所有容器

docker stop `ps -aq`

批量暂停所有容器

docker pause `ps -aq`

批量恢复所有容器

docker unpaus`ps -aq`

相关推荐
加载中loading...12 分钟前
Linux线程安全(二)条件变量实现线程同步
linux·运维·服务器·c语言·1024程序员节
安科瑞刘鸿鹏14 分钟前
校园建筑用电安全监测装置 电气火灾监测预防设备功能介绍
运维·服务器·网络·嵌入式硬件·安全·能源
课堂随想17 分钟前
【libGL error】Autodl云服务器配置ACT的conda虚拟环境生成训练数据时,遇到了libGL相关错误,涉及swrast_dri.so
运维·服务器·conda
工作不忙2 小时前
不使用docker-compose不使用zookeeper启动ApacheKafka3.8.0单机运行KRAFT模式
ubuntu·docker·zookeeper·kafka·apache
盒马盒马2 小时前
Docker:存储卷
docker·容器
大熊程序猿3 小时前
ubuntu 安装k3s
linux·运维·ubuntu
luoqice3 小时前
CentOS 自启动某个应用
linux·运维·服务器
Hadoop_Liang3 小时前
Docker Compose一键部署Spring Boot + Vue项目
vue.js·spring boot·docker
泠山3 小时前
ubuntu增加swap交换空间
linux·运维·ubuntu
JavaOpsPro4 小时前
jenkins部署手册
运维·jenkins·离线部署