简体   繁体   中英

Express in Docker Container: Connection Refused

I'm trying to run an express server inside a Docker container however I am unable to make requests to it. I have these in place

Express:

var port = normalizePort(process.env.PORT || '3012');

app.set('port', port);

Docker inspect:

{
    "Id": "eeeadafe15ffd538921ec94980f1dfe0f3ba8089055f38f8267c0299c8a70797",
    "Created": "2019-11-14T21:19:21.725785713Z",
    "Path": "docker-entrypoint.sh",
    "Args": [
        "node",
        "app.js"
    ],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 3300,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2019-11-14T21:19:22.412780136Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "sha256:c5ed3927b147e8c58705c6e1e5512a355d97c3aac1a7ec0e395f4b893f8e7cd9",
    "ResolvConfPath": "/var/lib/docker/containers/eeeadafe15ffd538921ec94980f1dfe0f3ba8089055f38f8267c0299c8a70797/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/eeeadafe15ffd538921ec94980f1dfe0f3ba8089055f38f8267c0299c8a70797/hostname",
    "HostsPath": "/var/lib/docker/containers/eeeadafe15ffd538921ec94980f1dfe0f3ba8089055f38f8267c0299c8a70797/hosts",
    "LogPath": "/var/lib/docker/containers/eeeadafe15ffd538921ec94980f1dfe0f3ba8089055f38f8267c0299c8a70797/eeeadafe15ffd538921ec94980f1dfe0f3ba8089055f38f8267c0299c8a70797-json.log",
    "Name": "/dashboard",
    "RestartCount": 0,
    "Driver": "overlay2",
    "Platform": "linux",
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "",
    "ExecIDs": null,
    "HostConfig": {
        "Binds": null,
        "ContainerIDFile": "",
        "LogConfig": {
            "Type": "json-file",
            "Config": {}
        },
        "NetworkMode": "default",
        "PortBindings": {
            "3012/tcp": [
                {
                    "HostIp": "",
                    "HostPort": "3012"
                }
            ]
        },
        "RestartPolicy": {
            "Name": "always",
            "MaximumRetryCount": 0
        },
        "AutoRemove": false,
        "VolumeDriver": "",
        "VolumesFrom": null,
        "CapAdd": null,
        "CapDrop": null,
        "Capabilities": null,
        "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/1d158fec262e117181f82b367e1f0692dc0ea8eacdf154e00cb41647f00e6640-init/diff:/var/lib/docker/overlay2/5d01e4052396cc00744963f6725858e9cad2e582d79ca71199910cc905a3d2fb/diff:/var/lib/docker/overlay2/5ad59bd1090bf8b1a6627a88d085f20dd52456d844d7c017c3cfa14cbe3bfa4b/diff:/var/lib/docker/overlay2/0691a89e85eb440426df6e341056466b39edd0ae488073df82a58dc174680813/diff:/var/lib/docker/overlay2/c35fa725edf35a6e3ab289e9c16b1946c821f9c3abe5b481b4de0b10f9a1cdb5/diff:/var/lib/docker/overlay2/872cc7f83e4bb169a616534f37c5133da98d897b05091b356692b8b4a3aa5fd8/diff:/var/lib/docker/overlay2/341c54026baf0ef085e2c763e936eacb36fc0ff0c247a4b0fd636ccea3008546/diff:/var/lib/docker/overlay2/ab010142a8affdfc82bb341a665d81fb99379b530efe3824d7efa630d7e239c1/diff:/var/lib/docker/overlay2/d5005d78d7ecf164eaec93058b675f37b744becb4476037d80b1b308f14fe40e/diff",
            "MergedDir": "/var/lib/docker/overlay2/1d158fec262e117181f82b367e1f0692dc0ea8eacdf154e00cb41647f00e6640/merged",
            "UpperDir": "/var/lib/docker/overlay2/1d158fec262e117181f82b367e1f0692dc0ea8eacdf154e00cb41647f00e6640/diff",
            "WorkDir": "/var/lib/docker/overlay2/1d158fec262e117181f82b367e1f0692dc0ea8eacdf154e00cb41647f00e6640/work"
        },
        "Name": "overlay2"
    },
    "Mounts": [],
    "Config": {
        "Hostname": "eeeadafe15ff",
        "Domainname": "",
        "User": "node",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "ExposedPorts": {
            "3012/tcp": {}
        },
        "Tty": true,
        "OpenStdin": true,
        "StdinOnce": false,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "NODE_VERSION=10.17.0",
            "YARN_VERSION=1.19.1"
        ],
        "Cmd": [
            "node",
            "app.js"
        ],
        "Image": "dashboard",
        "Volumes": null,
        "WorkingDir": "/home/node/app",
        "Entrypoint": [
            "docker-entrypoint.sh"
        ],
        "OnBuild": null,
        "Labels": {}
    },
    "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "e8fc1ac64e42bacde84d4e1d57ebbdcae419f259bdfb91979bc6d592fda37998",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "3012/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "3012"
                }
            ]
        },
        "SandboxKey": "/var/run/docker/netns/e8fc1ac64e42",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "b208178ddcdf6af4d538aeb2ac9c915365834ac2ea97bee0f7368b444f20f667",
        "Gateway": "172.17.0.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.3",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "MacAddress": "02:42:ac:11:00:03",
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "9d63da1dea4f0eb08fb45bfeeeaa8b5655dc1d96644741dd9aeaa1ca82988e54",
                "EndpointID": "b208178ddcdf6af4d538aeb2ac9c915365834ac2ea97bee0f7368b444f20f667",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.3",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:03",
                "DriverOpts": null
            }
        }
    }
}

I ran the command with:

docker run --name dashboard -it -p 3012:3012 --restart=always -d dashboard

docker ps shows:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES

eeeadafe15ff        dashboard           "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:3012->3012/tcp   dashboard

Host IP is at: 192.168.138.87

Trying to reach it at: 192.168.138.87:3012

Also tried: 172.17.0.1:3012

I am getting an error: net::ERR_CONNECTION_REFUSED

Am I missing something? or Is there something wrong with what I did?

Dockerfile:

FROM node:10-alpine

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app

WORKDIR /home/node/app

COPY package*.json ./

USER node

RUN npm install

COPY --chown=node:node . .

EXPOSE 3012

CMD [ "node", "app.js" ]

Are there any other steps I should be performing or a missing configuration?

PS: It actually works fine without docker and even with docker it starts up normally and i am able to see the logs running perfectly (docker logs - f ). Just that I am unable to make a request to it. Host is a Linux machine.

Thanks in advance!

Just to add: Server is also running SocketIO

I think u need to use

http://localhost:3012

Please refer to nodejs Docker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM