在Docker中进行容器化之后,dotnet core 2.2 webAPI不会响应

时间:2019-06-07 19:39:39

标签: docker .net-core asp.net-core-webapi

我正在学习Docker。我创建了.net core 2.2 webAPI,并能够使用Postman与之交互。然后,我创建了一个Dockerfile并将其添加到Docker容器中。我无法再与api进行交互。我认为我配置错误。另外,我正在Ubuntu 16.04机器上进行开发。

我在github上的.net核心项目中查看了几个Dockerfile,并阅读了关于SO的几个问题,但无法弄清楚。

我的docker run命令:

docker run -d -p 80:5001 blogapi:latest

我在邮递员中的获取请求:

https://0.0.0.0:80/api/blog

我的项目文件结构如下:

blog
  |
  --BlogApi
       |
       --BlogApi.csproj
       --Dockerfile
       --Program.cs
       --Startup.cs
       --...

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy everything else and build app
COPY . ./
WORKDIR /app/
RUN dotnet publish -c Release -o out


FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app/out .
EXPOSE 80
ENTRYPOINT ["dotnet", "BlogApi.dll"]

在正在运行的容器上运行docker inspect的结果:

[
    {
        "Id": "88c7c5584c3e29423a2ce49deb9e50a52c131621d5cbb994751a645aba797c8f",
        "Created": "2019-06-07T18:49:32.911199534Z",
        "Path": "dotnet",
        "Args": [
            "BlogApi.dll"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 26762,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-06-07T18:49:39.970680347Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:b5e50590a06afae8eab09c5b748fdd97a22252a69fef3e8eb9a3916bbe1f8fd8",
        "ResolvConfPath": "/var/lib/docker/containers/88c7c5584c3e29423a2ce49deb9e50a52c131621d5cbb994751a645aba797c8f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/88c7c5584c3e29423a2ce49deb9e50a52c131621d5cbb994751a645aba797c8f/hostname",
        "HostsPath": "/var/lib/docker/containers/88c7c5584c3e29423a2ce49deb9e50a52c131621d5cbb994751a645aba797c8f/hosts",
        "LogPath": "/var/lib/docker/containers/88c7c5584c3e29423a2ce49deb9e50a52c131621d5cbb994751a645aba797c8f/88c7c5584c3e29423a2ce49deb9e50a52c131621d5cbb994751a645aba797c8f-json.log",
        "Name": "/peaceful_buck",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "5001/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "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,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "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/9993c5bb1be9533897834e03038a2247ac52f0b2043414807e437d075e5448e6-init/diff:/var/lib/docker/overlay2/b53ea9ebd00b2c4cdc83d5df8905241dbd1b2413abbe38161336543e04ab7b32/diff:/var/lib/docker/overlay2/eeac3efbd1b7e13370ee7b640173a07e1b375969dcd75df142409d5c3653688f/diff:/var/lib/docker/overlay2/7d24ebd98305848e3a3f3494d327826b27fc5cc116995df2720aa3754b37579f/diff:/var/lib/docker/overlay2/dbe73936117865157ca2d89f86909bf2ce32443a48ca368dcbb3ef934fbee775/diff:/var/lib/docker/overlay2/13f3a01d82b32c692d8b533cebc22aedaf7691e8c4bc3a2a6d91bf028289a605/diff:/var/lib/docker/overlay2/b9554cb92ce0399f6964958300ac2af95cfec4eac08acc0ed01fa8fe26d1def1/diff",
                "MergedDir": "/var/lib/docker/overlay2/9993c5bb1be9533897834e03038a2247ac52f0b2043414807e437d075e5448e6/merged",
                "UpperDir": "/var/lib/docker/overlay2/9993c5bb1be9533897834e03038a2247ac52f0b2043414807e437d075e5448e6/diff",
                "WorkDir": "/var/lib/docker/overlay2/9993c5bb1be9533897834e03038a2247ac52f0b2043414807e437d075e5448e6/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "88c7c5584c3e",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5001/tcp": {},
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "ASPNETCORE_URLS=http://+:80",
                "DOTNET_RUNNING_IN_CONTAINER=true",
                "ASPNETCORE_VERSION=2.2.5"
            ],
            "Cmd": null,
            "ArgsEscaped": true,
            "Image": "blogapi:latest",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": [
                "dotnet",
                "BlogApi.dll"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "172dbe50c926349277a2ae67e09639d2e3296625dbec218e51a7526066e78b03",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5001/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    }
                ],
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/172dbe50c926",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "4e830cf57d2e4534df061a56cf09dd88c10771345cd5e0212612ea8073f0d537",
            "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": "57c61c9decd1fbc78a15bc72207a406f767bc6516299a6e5ad33b439ae655520",
                    "EndpointID": "4e830cf57d2e4534df061a56cf09dd88c10771345cd5e0212612ea8073f0d537",
                    "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
                }
            }
        }
    }
]

邮递员返回“无法获得任何回复”

2 个答案:

答案 0 :(得分:0)

您的应用未监听您正在映射的端口。 50005001是ASP.net Core的常见开发默认值,但是您的项目当前仅在端口80上运行而没有SSL。因此,您应该能够像这样运行它:

docker run -d -p 8080:80 blogapi:latest

到达http://localhost:8080/api/blog

答案 1 :(得分:0)

我知道了。根据docker论坛在这里的帖子:https://forums.docker.com/t/unable-to-launch-the-net-core-website-in-docker/48129,您需要运行docker inspect [containerid]并找到容器的IP地址。当我运行docker inspect时,我发现了以下内容:

master

然后我就可以在此地址与api进行交互:

origin/master

邮递员回复:

"Networks": {
    "bridge": {
        "IPAMConfig": null,
        "Links": null,
        "Aliases": null,
        "NetworkID": "57c61c9decd1fbc78a15bc72207a406f767bc6516299a6e5ad33b439ae655520",
        "EndpointID": "36a50f4be28ced0f8751222df789dabf8f7bf9dc4934b8c075c25fc775a74097",
        "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
    }
}