docker中如何启动已存在容器
作者:x县豆瓣酱
这篇文章主要介绍了docker中如何启动已存在容器问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
docker启动已存在容器
- 查看所有容器
docker ps -a
- 启动已存在容器
docker start 容器ID
docker查看容器启动命令(已运行的容器)
eg:
1 docker ps 查看正在运行的容器:
通过docker ps命令
该命令主要是为了详细展示查看运行时的command参数
docker ps -a --no-trunc | grep container_name # 通过docker --no-trunc参数来详细展示容器运行命令
通过docker inspect命令
使用docker inspect,但是docker inspect打印的是json文件,需要挑选字段查看,容器的参数信息都能从json文件中解析出来。
1,在容器外部,物理机上,可以用下面命令
docker inspect <container_name> # 可以是container_name或者container_id
eg:
[root@128 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 547880f43cf1 mysql:8.0.31 "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 33060/tcp, 0.0.0.0:3301->3306/tcp, :::3301->3306/tcp mysql-8.0.31 e02f22dc442c mysql:5.7 "docker-entrypoint.s…" 25 hours ago Up 18 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql [root@128 ~]# docker ps -a --no-trunc | grep 547880f43cf1 547880f43cf176d2a826ae26f9f04bbe637ca56ec5d92df25a78d24ec23ed232 mysql:8.0.31 "docker-entrypoint.sh mysqld" 15 minutes ago Up 15 minutes 33060/tcp, 0.0.0.0:3301->3306/tcp, :::3301->3306/tcp mysql-8.0.31 [root@128 ~]# docker inspect 547880f43cf1 [ { "Id": "547880f43cf176d2a826ae26f9f04bbe637ca56ec5d92df25a78d24ec23ed232", "Created": "2022-12-07T10:13:46.80942044Z", "Path": "docker-entrypoint.sh", "Args": [ "mysqld" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 5469, "ExitCode": 0, "Error": "", "StartedAt": "2022-12-07T10:13:47.159621124Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:a3a2968869cf080dbbd2adaac9e4075cc358b50a1451ff5e2b9ae90551a4735f", "ResolvConfPath": "/var/lib/docker/containers/547880f43cf176d2a826ae26f9f04bbe637ca56ec5d92df25a78d24ec23ed232/resolv.conf", "HostnamePath": "/var/lib/docker/containers/547880f43cf176d2a826ae26f9f04bbe637ca56ec5d92df25a78d24ec23ed232/hostname", "HostsPath": "/var/lib/docker/containers/547880f43cf176d2a826ae26f9f04bbe637ca56ec5d92df25a78d24ec23ed232/hosts", "LogPath": "/var/lib/docker/containers/547880f43cf176d2a826ae26f9f04bbe637ca56ec5d92df25a78d24ec23ed232/547880f43cf176d2a826ae26f9f04bbe637ca56ec5d92df25a78d24ec23ed232-json.log", "Name": "/mysql-8.0.31", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": [ "/mydata/mysql-8.0.31/log:/var/log/mysql", "/mydata/mysql-8.0.31/data:/var/lib/mysql" ], "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "3306/tcp": [ { "HostIp": "", "HostPort": "3301" } ] }, "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/b4d5fed9eb97224cd6834e36509728032ceac386e863a483d7e2770f611eb8d1-init/diff:/var/lib/docker/overlay2/e6ce4810d05ed0bdca975287151d5edcb8f5b4e3472fba848ecbd0858a83c27d/diff:/var/lib/docker/overlay2/0bd4abf22bc8fd1840c48d50034d525576608654eac3696820bdecca885869e1/diff:/var/lib/docker/overlay2/aa9ffa3d8ca28910870afed61cbb7a8b5954b633b6c51ddec94b33f45fd7c69a/diff:/var/lib/docker/overlay2/7832e98417573f093c97491e70941e1a09e9a6e4e49b4407644cc367fdbeb37e/diff:/var/lib/docker/overlay2/74b73e21fc743a650a4064f4e771fd65aca511cf433aee93cc6eef2f3a91d787/diff:/var/lib/docker/overlay2/0df122c8a1629d74c09d97e489932b0812da34d28734118287934acbf3e8e985/diff:/var/lib/docker/overlay2/6f48c0c0c5bb4f2d96900b9eb4d20409a066a9bf3c104171e5dd33f28dbe6301/diff:/var/lib/docker/overlay2/37869951120d01076a5bc57e0c04c8f466a0b0b37e06357653b27b2fc9638a1d/diff:/var/lib/docker/overlay2/c2414c698ebdc9f0bacedf53387c75aa6cb000720ff019d989e4daa9d9c0929f/diff:/var/lib/docker/overlay2/099e69b07f239a7747f754a2598aec804655f5d6d7beb9ffb85932c490ec13d1/diff:/var/lib/docker/overlay2/9be3057383e74825462c49286ffa30853fb87f6d975ad40459e12972538ce21c/diff", "MergedDir": "/var/lib/docker/overlay2/b4d5fed9eb97224cd6834e36509728032ceac386e863a483d7e2770f611eb8d1/merged", "UpperDir": "/var/lib/docker/overlay2/b4d5fed9eb97224cd6834e36509728032ceac386e863a483d7e2770f611eb8d1/diff", "WorkDir": "/var/lib/docker/overlay2/b4d5fed9eb97224cd6834e36509728032ceac386e863a483d7e2770f611eb8d1/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "bind", "Source": "/mydata/mysql-8.0.31/log", "Destination": "/var/log/mysql", "Mode": "", "RW": true, "Propagation": "rprivate" }, { "Type": "bind", "Source": "/mydata/mysql-8.0.31/data", "Destination": "/var/lib/mysql", "Mode": "", "RW": true, "Propagation": "rprivate" } ], "Config": { "Hostname": "547880f43cf1", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "3306/tcp": {}, "33060/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "MYSQL_ROOT_PASSWORD=123456", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.14", "MYSQL_MAJOR=8.0", "MYSQL_VERSION=8.0.31-1.el8", "MYSQL_SHELL_VERSION=8.0.31-1.el8" ], "Cmd": [ "mysqld" ], "Image": "mysql:8.0.31", "Volumes": { "/var/lib/mysql": {} }, "WorkingDir": "", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "f82246141707b2ab4578ce193cdf42b9dd82fef9d8ca805c4b07b6af90867ffc", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "3306/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "3301" }, { "HostIp": "::", "HostPort": "3301" } ], "33060/tcp": null }, "SandboxKey": "/var/run/docker/netns/f82246141707", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "386a6348d0fd2e9119e7d427ed2df21bf03f4a88c485b50cf142d6c0b0f08f83", "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": "65f89f72b915b6ae63526860f40f2e701bfcc1c689301d757230f7a522379d35", "EndpointID": "386a6348d0fd2e9119e7d427ed2df21bf03f4a88c485b50cf142d6c0b0f08f83", "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 } } } } ]
如上所示,这个是我这547880f43cf1 容器的详细信息
然后可以定制化输出上面的信息:显示上面内容的某一部分信息
docker inspect --format='{{.NetworkSettings.Networks.bridge.IPAddress}}' <container_name> # format是go语言的template,还有其他的用法 docker inspect --format='{{.Mounts}}' <container_name> docker inspect --format='{{.HostConfig.Binds}}' <container_name> docker inspect --format='{{.HostConfig.PortBindings}}' <container_name>
eg: 还可以将上面的修改为别的内容,我这边关注file和port两个
[root@128 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 547880f43cf1 mysql:8.0.31 "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 33060/tcp, 0.0.0.0:3301->3306/tcp, :::3301->3306/tcp mysql-8.0.31 e02f22dc442c mysql:5.7 "docker-entrypoint.s…" 25 hours ago Up 22 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql [root@128 ~]# docker inspect --format='{{.Mounts}}' 547880f43cf1 [{bind /mydata/mysql-8.0.31/log /var/log/mysql true rprivate} {bind /mydata/mysql-8.0.31/data /var/lib/mysql true rprivate}] [root@128 ~]# docker inspect --format='{{.NetworkSettings.Networks.bridge.IPAddress}}' 547880f43cf1 172.17.0.2 [root@128 ~]# docker inspect --format='{{.HostConfig.Binds}}' 547880f43cf1 [/mydata/mysql-8.0.31/log:/var/log/mysql /mydata/mysql-8.0.31/data:/var/lib/mysql] [root@128 ~]# docker inspect --format='{{.HostConfig.PortBindings}}' 547880f43cf1 map[3306/tcp:[{ 3301}]] [root@128 ~]#
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。