简体   繁体   中英

Docker: Cannot launch .Net Core 3.1 Web Api on browser after docker run

I'm new to Docker and, as a start, I'm trying to accomplish a basic task, dockerize a .Net Core 3.1 Web Api and run it from the command line (not From Visual Studio where it actually works).

I create my project image using the following Dockerfile and with the next command:

docker build -t concepttest_crud1 .

Dockerfile (created by Visual Studio):

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Gfi_ConceptTest_CRUD1/Gfi_ConceptTest_CRUD1.csproj", "Gfi_ConceptTest_CRUD1/"]
RUN dotnet restore "Gfi_ConceptTest_CRUD1/Gfi_ConceptTest_CRUD1.csproj"
COPY . .
WORKDIR "/src/Gfi_ConceptTest_CRUD1"
RUN dotnet build "Gfi_ConceptTest_CRUD1.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Gfi_ConceptTest_CRUD1.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Gfi_ConceptTest_CRUD1.dll"]

4) I run my image with the following command:

docker run -d -p 8080:44390 --name crud1 concepttest_crud1

where 44390 is the port I have my api configured on in Visual Studio.

When debugging I use to access the api through:

https://localhost:44390/api/Authors

5) I'm trying to test my dockerized api in Chrome with the next url:

https://localhost:8080/api/Authors

to no avail. No matter with url I try, my api won't start when the docker run command executes with no errors.

docker ps output:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
8df6c92c6678        concepttest_crud1   "dotnet Gfi_ConceptT…"   22 minutes ago      Up 22 minutes       0.0.0.0:8080->44390/tcp   crud1

docker images output:

REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
concepttest_crud1                      latest              878e4c4845f6        About an hour ago   228MB
<none>                                 <none>              36a29990113c        About an hour ago   1GB

In the Docker images output I don't know why I see two images (a second with no name) when I expect only one.

I've also tried http://192.168.99.100:8080/api/Authors but it is not working either.

Edit 1: Adding docker inspect.

[
    {
        "Id": "0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57",
        "Created": "2019-12-23T12:30:06.3947619Z",
        "Path": "dotnet",
        "Args": [
            "Gfi_ConceptTest_CRUD1.dll"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 83477,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-12-23T12:30:08.9701219Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:92b9217abf5dc6a7fc6c10aac2ac5549d6873adfc1b3ed30264d6e1fd4a971c7",
        "ResolvConfPath": "/var/lib/docker/containers/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57/hostname",
        "HostsPath": "/var/lib/docker/containers/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57/hosts",
        "LogPath": "/var/lib/docker/containers/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57-json.log",
        "Name": "/crud1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "443/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8080"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "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": [
                28,
                165
            ],
            "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/3781dd694db551736c551397e9a0834ebd6fa653e0b78b6d6244e15ef7b8c291-init/diff:/var/lib/docker/overlay2/025232b693c4a6470e0b26e5302e5165757dbc4a44d5af5b7b8aa8dacd77ee03/diff:/var/lib/docker/overlay2/bcca2356fe199bbaa471c3590a3f55df343eecd84899cf570197d97ed8111d95/diff:/var/lib/docker/overlay2/5f2d77acbd50bc4a8cde011128fb43b2a0cff888e716c5ba882d4dd906bc1901/diff:/var/lib/docker/overlay2/ca57ac72ac1d48b622745bb473fd71c18d67392b02cdf938835f05cf9b547681/diff:/var/lib/docker/overlay2/0b894b31b26df6e40b57a86dc66bbd4eda71d0ebf6d5755c4c43bc296e9a24de/diff:/var/lib/docker/overlay2/b6ca0109d2718605ccba2585e52ae1718cbecd2fcd62197f1f7fd505cf2be358/diff:/var/lib/docker/overlay2/e0ff62d02fe725053674ce9e3b59a4004c101fd2e98f919e5037a412f1c3a4e8/diff",
                "MergedDir": "/var/lib/docker/overlay2/3781dd694db551736c551397e9a0834ebd6fa653e0b78b6d6244e15ef7b8c291/merged",
                "UpperDir": "/var/lib/docker/overlay2/3781dd694db551736c551397e9a0834ebd6fa653e0b78b6d6244e15ef7b8c291/diff",
                "WorkDir": "/var/lib/docker/overlay2/3781dd694db551736c551397e9a0834ebd6fa653e0b78b6d6244e15ef7b8c291/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "0f0bf4b4de30",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "443/tcp": {},
                "8080/tcp": {}
            },
            "Tty": true,
            "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"
            ],
            "Cmd": null,
            "Image": "crud1",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": [
                "dotnet",
                "Gfi_ConceptTest_CRUD1.dll"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "ed889f3303ca17ad4e79ae917d8b17c5e590af502adb8049dbd7e0fcd46b323c",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "443/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8080"
                    }
                ],
                "8080/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/ed889f3303ca",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "001cd49640c4211e5eeb1eedacedd6de07022cc3362acaca59b82f0e537f6238",
            "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": "ccea227190a1f04d0043015cf3d9932ba41f4a96dfb9bc2a175a06483a9ca66e",
                    "EndpointID": "001cd49640c4211e5eeb1eedacedd6de07022cc3362acaca59b82f0e537f6238",
                    "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
                }
            }
        }
    }
]

Edit 2: Added curl.

docker exec -it 0f0bf4b4de30 /bin/bash . root@4237f947b2b0:/app# curl https://localhost:8080/api/Authors

.: .: Is a directory

Edit 3: New curl

docker exec -it 0f0bf4b4de30 /bin/bash . root@4237f947b2b0:/app# curl https://localhost/api/Authors
.: .: Is a directory

In my case it was due to having SSL enabled inthe project, but after removing everything related to https it started working as expected.

Having SSL enabled implies installing a SSL certificate inside the docker container and enabling it, things I wasn't able to do as I don't have a certificate and I'm just learning docker.

Steps I've done:

In Startup.cs I've disabled https redirection

//app.UseHttpsRedirection();

And in project properties under "Debug" section I unchecked "Enable SSL".

Then I was able to run the container as expected.

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