无法访问在具有暴露端口的Docker容器内运行的节点服务

s1ag04yj  于 2023-01-29  发布在  Docker
关注(0)|答案(1)|浏览(148)

我正在尝试访问局域网上运行在Docker容器中的Web服务器。目前,我可以确认容器已正确启动,应用程序已正确引导,但无法通过http://<device IP>:<container port>通过网络访问容器。
当我inspect容器时,我看到的值为:"IPAddress": "172.20.0.2",,这让我觉得我应该能够从http://172.20.0.2:8057访问服务,但似乎什么也没有发生-我的浏览器只是挂起。
我如何通过我的网络访问这个Docker容器中的服务器?主机是Synology NAS,如果这有关系的话。
这是我的应用程序的Dockerfile的外观:

# syntax=docker/dockerfile:1

FROM node:18
ENV NODE_ENV=production
ENV SECRET_KEY=${SECRET_KEY}

WORKDIR /app

COPY ["package.json", "package-lock.json*", "./"]

RUN npm install --production

COPY . .

# public port
EXPOSE 8057

CMD [ "npm", "run", "start" ]

然后构建/标记图像:

docker build . -t myname/myapplication:latest

并引用docker-compose中的图像:

my-application:
    image: myname/myapplication:latest
    container_name: my-application
    environment:
      - SECRET_KEY=${SECRET_KEY} #defined in .env
    ports:
      - 8057:8057
    restart: unless-stopped

并运行容器:

docker compose up -d

当我检查集装箱时,我看到:

docker ps
CONTAINER ID   IMAGE                              COMMAND                  CREATED          STATUS                  PORTS                                                                NAMES
44866d03deaa   myname/myapplication:latest      "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes           0.0.0.0:8057->8057/tcp                                               my-application
sudo docker inspect 44866d03deaa
[
    {
        "Id": "44866d03deaa68765da5a04f8b8ca053370db17f62a4b7dbff2cce3c6b933266",
        "Created": "2022-11-16T06:27:38.945500018Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "npm",
            "run",
            "start"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 17977,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-11-16T06:27:39.599626086Z",
            "FinishedAt": "0001-01-01T00:00:00Z",
            "StartedTs": 1668580059,
            "FinishedTs": -62135596800
        },
        "Image": "sha256:8f058ea7b45a4313c9b51b89ea9e7ff4db0aeb1c11ef9774caba2b07145a2789",
        "ResolvConfPath": "/volume1/@docker/containers/44866d03deaa68765da5a04f8b8ca053370db17f62a4b7dbff2cce3c6b933266/resolv.conf",
        "HostnamePath": "/volume1/@docker/containers/44866d03deaa68765da5a04f8b8ca053370db17f62a4b7dbff2cce3c6b933266/hostname",
        "HostsPath": "/volume1/@docker/containers/44866d03deaa68765da5a04f8b8ca053370db17f62a4b7dbff2cce3c6b933266/hosts",
        "LogPath": "/volume1/@docker/containers/44866d03deaa68765da5a04f8b8ca053370db17f62a4b7dbff2cce3c6b933266/log.db",
        "Name": "/my-application",
        "RestartCount": 0,
        "Driver": "aufs",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "db",
                "Config": {}
            },
            "NetworkMode": "htpc-box_default",
            "PortBindings": {
                "8057/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8057"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "unless-stopped",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "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",
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NODE_VERSION=18.12.1",
                "YARN_VERSION=1.22.19",
                "NODE_ENV=production",
                "SECRET_KEY=XXXXX"
            ],
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "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": null,
            "Name": "aufs"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "44866d03deaa",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8057/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NODE_VERSION=18.12.1",
                "YARN_VERSION=1.22.19",
                "NODE_ENV=production",
                "SECRET_KEY=XXXXX"
            ],
            "Cmd": [
                "npm",
                "run",
                "start"
            ],
            "Image": "myname/myapplication:latest",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "0f7bb51e7232e9783e77dc2abbeb9ecd525f5cbf7c730cacf870424bc38da477",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "htpc-box",
                "com.docker.compose.project.config_files": "docker-compose.yml",
                "com.docker.compose.project.working_dir": "/volume1/projects/htpc-box",
                "com.docker.compose.service": "my-application",
                "com.docker.compose.version": "1.28.5"
            },
            "DDSM": false
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "46f942ea9196d2ed3ad7f7f5cdf729215c2d97a2b0bda1a00f20b4bf655e861c",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8057/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8057"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/46f942ea9196",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "htpc-box_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "my-application",
                        "44866d03deaa"
                    ],
                    "NetworkID": "aad1105e5347f4774f6f4f9c2ae6d405674d2ca5aa6f6a68bfd145b99086eb5f",
                    "EndpointID": "f0b770b34bdb72c5cab3bc370fdf0f669d6a89f072f1e9250316a484a8bfeeec",
                    "Gateway": "172.20.0.1",
                    "IPAddress": "172.20.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:14:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

运行Docker容器的服务器上netstat的输出:

sudo netstat -tnulp | grep 8057
tcp        0      0 0.0.0.0:8057            0.0.0.0:*               LISTEN      17942/docker-proxy
pgvzfuti

pgvzfuti1#

服务器中的主机名可能有误。如果使用默认localhost或127.0.0.1,则无法连接到容器的服务。需要使用0.0.0.0才能连接到容器中运行的服务。

相关问题