您的浏览器过于古老 & 陈旧。为了更好的访问体验, 请 升级你的浏览器
j0785 发布于2022年12月26日 11:57 最近更新于 2022年12月27日 16:24

原创 Docker如何设置容器自动重启?如何查询容器重启策略?

1905 次浏览 读完需要≈ 25 分钟 Docker

内容目录

设置容器自动重启方法

  • 创建容器的时候设置自动重启

    • 创建容器命令:docker run -d --name=容器名称 -p 宿主机端口号:容器端口号 -v 宿主机目录:容器目录 --restart=always 镜像名称
    --restart参数详解:
           no         //默认参数,容器退出时不重启
           on-failure    //容器非正常退出则重启容器(退出状态非0则表示非正常退出)
           on-failure:2   //容器非正常退出时重启容器,最多重启2次(次数自己配置)
           always      //只要容器退出都自动重启容器,不限制退出状态
           unless-stopped //在容器退出时总是重启容器,排除在Docker进程启动时就已经停止运行的容器
  • 修改已有容器设置

    • 创建容器命令:docker run -d --name=容器名称 -p 宿主机端口号:容器端口号 -v 宿主机目录:容器目录 镜像名称
    • 修改容器配置命令:docker update --restart=always 容器名称/容器ID *为了方便,一般使用容器名称

      C:\Users\Shinelon>docker update --restart=always mynginx
      mynginx

查询容器重启策略的方法

  • 使用docker inspect mynginx命令查询容器详细信息,然后找到HostConfig下的RestartPolicy就可以看到容器的重启策略

    C:\Users\Shinelon>docker inspect mynginx
    [
        {
            "Id": "63e104fde8b195f24b11728e55034cd6173fad8ee60053daac9bbcbe671a9fc3",
            "Created": "2022-11-29T09:29:11.1964788Z",
            "Path": "/docker-entrypoint.sh",
            "Args": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 25155,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2022-12-17T09:09:44.5014594Z",
                "FinishedAt": "2022-12-17T09:09:31.9082462Z"
            },
            "Image": "sha256:88736fe827391462a4db99252117f136b2b25d1d31719006326a437bb40cb12d",
            "ResolvConfPath": "/var/lib/docker/containers/63e104fde8b195f24b11728e55034cd6173fad8ee60053daac9bbcbe671a9fc3/resolv.conf",
            "HostnamePath": "/var/lib/docker/containers/63e104fde8b195f24b11728e55034cd6173fad8ee60053daac9bbcbe671a9fc3/hostname",
            "HostsPath": "/var/lib/docker/containers/63e104fde8b195f24b11728e55034cd6173fad8ee60053daac9bbcbe671a9fc3/hosts",
            "LogPath": "/var/lib/docker/containers/63e104fde8b195f24b11728e55034cd6173fad8ee60053daac9bbcbe671a9fc3/63e104fde8b195f24b11728e55034cd6173fad8ee60053daac9bbcbe671a9fc3-json.log",
            "Name": "/mynginx",
            "RestartCount": 0,
            "Driver": "overlay2",
            "Platform": "linux",
            "MountLabel": "",
            "ProcessLabel": "",
            "AppArmorProfile": "",
            "ExecIDs": [
                "ca8b435e1e151bedb485726f39742c69eae245f520cd4a54c03e670f5af2787c"
            ],
            "HostConfig": {
                "Binds": [
                    "d:/docker/www:/usr/share/nginx/www",
                    "d:/docker/nginx/conf.d:/etc/nginx/conf.d",
                    "d:/docker/nginx/logs:/var/log/nginx"
                ],
                "ContainerIDFile": "",
                "LogConfig": {
                    "Type": "json-file",
                    "Config": {}
                },
                "NetworkMode": "myphp-net",
                "PortBindings": {
                    "80/tcp": [
                        {
                            "HostIp": "",
                            "HostPort": "81"
                        }
                    ]
                },
                "RestartPolicy": {
                    "Name": "always",
                    "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": [
                    30,
                    120
                ],
                "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/30d6da4377891284073d4e5ce764432c0ea43fbbaabcee8ad13288fc5df3b7cf-init/diff:/var/lib/docker/overlay2/e53ff7f53da6e5223a04d4d1e3d3ca3c21bd5b0846a4a2e3e68516e3ef7de40f/diff:/var/lib/docker/overlay2/e616dfb859d4da39a3ccf7002f2144c0c56ccdcda8957c546ebb8eb009e435e9/diff:/var/lib/docker/overlay2/c9d447a3efe1a1fe1b0bee2bb573126927a4500ad1d2da26a716576b98a90fbd/diff:/var/lib/docker/overlay2/9822792c25de2fc5921e128323ff22e50516e72c60adc1f299ced464c35a8d56/diff:/var/lib/docker/overlay2/7bcfa53038a71463a14725bdfe748103e325335deff904dadd4218ea1e0d6704/diff:/var/lib/docker/overlay2/80eab8ecbc127ccda509e6d232240c9535378dfbdfc615ce9a295ce2005fdb8b/diff",
                    "MergedDir": "/var/lib/docker/overlay2/30d6da4377891284073d4e5ce764432c0ea43fbbaabcee8ad13288fc5df3b7cf/merged",
                    "UpperDir": "/var/lib/docker/overlay2/30d6da4377891284073d4e5ce764432c0ea43fbbaabcee8ad13288fc5df3b7cf/diff",
                    "WorkDir": "/var/lib/docker/overlay2/30d6da4377891284073d4e5ce764432c0ea43fbbaabcee8ad13288fc5df3b7cf/work"
                },
                "Name": "overlay2"
            },
            "Mounts": [
                {
                    "Type": "bind",
                    "Source": "d:/docker/www",
                    "Destination": "/usr/share/nginx/www",
                    "Mode": "",
                    "RW": true,
                    "Propagation": "rprivate"
                },
                {
                    "Type": "bind",
                    "Source": "d:/docker/nginx/logs",
                    "Destination": "/var/log/nginx",
                    "Mode": "",
                    "RW": true,
                    "Propagation": "rprivate"
                },
                {
                    "Type": "bind",
                    "Source": "d:/docker/nginx/conf.d",
                    "Destination": "/etc/nginx/conf.d",
                    "Mode": "",
                    "RW": true,
                    "Propagation": "rprivate"
                }
            ],
            "Config": {
                "Hostname": "63e104fde8b1",
                "Domainname": "",
                "User": "",
                "AttachStdin": false,
                "AttachStdout": false,
                "AttachStderr": false,
                "ExposedPorts": {
                    "80/tcp": {}
                },
                "Tty": false,
                "OpenStdin": false,
                "StdinOnce": false,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                    "NGINX_VERSION=1.23.2",
                    "NJS_VERSION=0.7.7",
                    "PKG_RELEASE=1~bullseye"
                ],
                "Cmd": [
                    "nginx",
                    "-g",
                    "daemon off;"
                ],
                "Image": "nginx:latest",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": [
                    "/docker-entrypoint.sh"
                ],
                "OnBuild": null,
                "Labels": {
                    "maintainer": "NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e"
                },
                "StopSignal": "SIGQUIT"
            },
            "NetworkSettings": {
                "Bridge": "",
                "SandboxID": "9e0def1acc1fc52d2067a508a5f0d38a2c08a0b821881c03b379072de53fc547",
                "HairpinMode": false,
                "LinkLocalIPv6Address": "",
                "LinkLocalIPv6PrefixLen": 0,
                "Ports": {
                    "80/tcp": [
                        {
                            "HostIp": "0.0.0.0",
                            "HostPort": "81"
                        }
                    ]
                },
                "SandboxKey": "/var/run/docker/netns/9e0def1acc1f",
                "SecondaryIPAddresses": null,
                "SecondaryIPv6Addresses": null,
                "EndpointID": "",
                "Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "IPAddress": "",
                "IPPrefixLen": 0,
                "IPv6Gateway": "",
                "MacAddress": "",
                "Networks": {
                    "myphp-net": {
                        "IPAMConfig": null,
                        "Links": null,
                        "Aliases": [
                            "63e104fde8b1"
                        ],
                        "NetworkID": "dbf05a83cd49ec55950893e4ca93e812cb7fad808b9175334921a7c2a6197ce1",
                        "EndpointID": "a95306bfe4bbc8a6b8ce2f81c9b499dc87e66d7ab9d6a25da53750d9bfe23959",
                        "Gateway": "172.19.0.1",
                        "IPAddress": "172.19.0.4",
                        "IPPrefixLen": 16,
                        "IPv6Gateway": "",
                        "GlobalIPv6Address": "",
                        "GlobalIPv6PrefixLen": 0,
                        "MacAddress": "02:42:ac:13:00:04",
                        "DriverOpts": null
                    }
                }
            }
        }
    ]
  • CodePlayer技术交流群1
  • CodePlayer技术交流群2

0 条评论

撰写评论