docker环境下,nginx容器无法启动的解决方法,很实用

有时候,在配置docer容器内的配置文件的时候,出现错误,导致容器无法启动,此时有两种方法进行急救。
1.cp大法
你知道而且确定是哪个配置文件的问题,你可以将容器内的配置文件复制到本机,进行修改,然后再复制到容器中进行启动容器操作。

#nginx也可以是容器id
docker cp nginx:/etc/nginx/conf.d/default.conf ./ ###这里是将容器内文件复制到本地当前目录
docker cp ./default.conf nginx:/etc/nginx/conf.d/ ###这里是将本地当前目录的default.conf复制到容器内指定目录

确定没问题就可以用docker start nginx 看容器是否能启动
如果不行,则需要继续修改配置文件

2.直接进入到容器内的虚拟目录进行修改。

#其中nginx也可以是容器id
docker inspect nginx 
#结果如下
[
    {
        "Id": "f94ac2d60a37d3f7357d6584c17c076165f14cfce8efdf09cf2f26cf077d4f53",
        "Created": "2022-01-20T03:43:12.469011952Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 37586,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-20T07:50:05.452446525Z",
            "FinishedAt": "2022-01-20T07:40:15.682771701Z"
        },
        "Image": "sha256:53f3fd8007f76bd23bf663ad5f5009c8941f63828ae458cef584b5f85dc0a7bf",
        "ResolvConfPath": "/var/lib/docker/containers/f94ac2d60a37d3f7357d6584c17c076165f14cfce8efdf09cf2f26cf077d4f53/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/f94ac2d60a37d3f7357d6584c17c076165f14cfce8efdf09cf2f26cf077d4f53/hostname",
        "HostsPath": "/var/lib/docker/containers/f94ac2d60a37d3f7357d6584c17c076165f14cfce8efdf09cf2f26cf077d4f53/hosts",
        "LogPath": "/var/lib/docker/containers/f94ac2d60a37d3f7357d6584c17c076165f14cfce8efdf09cf2f26cf077d4f53/f94ac2d60a37d3f7357d6584c17c076165f14cfce8efdf09cf2f26cf077d4f53-json.log",
        "Name": "/nginx",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/var/wwwroot:/www"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "php-net",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"
                    }
                ]
            },
            "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": null,
            "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/f8fe847ff8f849359d483da256a09890bfade6d0d5ebebc0f415bf081698e445-init/diff:/var/lib/docker/overlay2/d2e65fe228acd5e4d716fbf80a27e6f62097ed0242308fbd4b65f097b7a06070/diff:/var/lib/docker/overlay2/abacc4dc89265295c3df401232481d780611c75fcad3f1a6231d6f47270be30a/diff:/var/lib/docker/overlay2/03f340d741e8351f04eb6e5b989bc7580fc7d1c59f5dc33136615ff94057195e/diff",
                "MergedDir": "/var/lib/docker/overlay2/f8fe847ff8f849359d483da256a09890bfade6d0d5ebebc0f415bf081698e445/merged",
                "UpperDir": "/var/lib/docker/overlay2/f8fe847ff8f849359d483da256a09890bfade6d0d5ebebc0f415bf081698e445/diff",
                "WorkDir": "/var/lib/docker/overlay2/f8fe847ff8f849359d483da256a09890bfade6d0d5ebebc0f415bf081698e445/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/var/wwwroot",
                "Destination": "/www",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "f94ac2d60a37",
            "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.15.12-1~stretch",
                "NJS_VERSION=1.15.12.0.3.1-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx:1.15",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers "
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "f6ad37af3ec53cdd246a99360cc0c0594b657d87d9a3b9a46bfa2ae6625f9d21",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/f6ad37af3ec5",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "php-net": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "f94ac2d60a37"
                    ],
                    "NetworkID": "5f5358edfd195d0f584a6bbfe67615368951a230cf58cc5ae41e7ff4df0018c4",
                    "EndpointID": "156e6b15db92d014cdc145441990ef21479a011c538b7375aaa10a396822c1b0",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]

找到 MergedDir,然后cd 进入,如果提示找不到目录,则将最后一级目录删除再试试,不行继续删除,直到能进入,然后一级级的cd到刚刚删除的目录,等你cd进入到MergedDir目录的时候会发现,这里不就是容器里面的目录么,那么接下来就很容易了,只需要进行修改文件就可以了。
个人推荐第一种方法,这样虽然麻烦,但是修改的东西最少,不会影响其他的功能。

你可能感兴趣的:(docker,容器,运维)