简体   繁体   中英

Docker port mapping gets ignored

I created an image with Asp.Net. The Dockerfile looks like

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

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

FROM build AS publish
RUN dotnet publish "AspMini.csproj" -c Release -o /app/publish /p:UseAppHost=false

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

However if i start a container with

docker run theImageName -d -p 8090:80

the mapping gets ignored. I can't access the api on localhost:8090.

The docker inspect output: docker inspect 991f8f09c1a6

[
    {
        "Id": "991f8f09c1a618ad05ca3910f9f570c15217f6753ce0fbb07203de21df66310a",
        "Created": "2023-01-13T14:42:03.686922616Z",
        "Path": "dotnet",
        "Args": [
            "AspMini.dll",
            "-d",
            "-p",
            "8090:80"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1748,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-01-13T14:42:04.089973705Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },

        "Name": "/beautiful_gagarin",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "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": [
                35,
                161
            ],
            "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/9c973e90ec99d50f84e8ee2747fa4646d5f8599a2f03646ac5301418cb542a88-init/diff:/var/lib/docker/overlay2/603fdf539efa645bb21ef84b7b3f6c0d833fbd03faad8dffd5defa4b68bbb42b/diff:/var/lib/docker/overlay2/7ba1ebf47509f54abc170cf2a0d57e03513c7ed94f05f6b836abaacf9a304c18/diff:/var/lib/docker/overlay2/87a8e5b62cb7841e224f121dac133fc1de10c7e11a71d908da9991423cab318d/diff:/var/lib/docker/overlay2/ac3471c9a4388af7f1a556b6796a6d7eb640d6831983b3ff7cc9c41361d674e8/diff:/var/lib/docker/overlay2/03d18c531853da913373bae78ddba3986d90705862c1ac84a49d9ec8ba9ce9c5/diff:/var/lib/docker/overlay2/d179824e5cc7707e84a46192e2c5d93d297828703c555747395a8173034152e6/diff:/var/lib/docker/overlay2/bbb62ce4f5330dca608c3d7ce8ef9e621d12bbae16a1634b23950033e629afa3/diff",
                "MergedDir": "/var/lib/docker/overlay2/9c973e90ec99d50f84e8ee2747fa4646d5f8599a2f03646ac5301418cb542a88/merged",
                "UpperDir": "/var/lib/docker/overlay2/9c973e90ec99d50f84e8ee2747fa4646d5f8599a2f03646ac5301418cb542a88/diff",
                "WorkDir": "/var/lib/docker/overlay2/9c973e90ec99d50f84e8ee2747fa4646d5f8599a2f03646ac5301418cb542a88/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "991f8f09c1a6",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "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",
                "DOTNET_VERSION=6.0.13",
                "ASPNET_VERSION=6.0.13"
            ],
            "Cmd": [
                "-d",
                "-p",
                "8090:80"
            ],
            "Image": "aspmini",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": [
                "dotnet",
                "AspMini.dll"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "5a2a584a1c775babb79a9a1434d3762c8511f1ac124084cde9147a3427ef2c73",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/5a2a584a1c77",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "9618ce41bddab811bcecbf107db63fde2153f60ca8926f48ac4e6610edc89749",
            "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": "75751644df6485eacd29cb213ee4ab2bee73f1214b9e99371406df1df39591d8",
                    "EndpointID": "9618ce41bddab811bcecbf107db63fde2153f60ca8926f48ac4e6610edc89749",
                    "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
                }
            }
        }
    }
]

PortBindings is just empty. But if i start the Container over the GUI: 在此处输入图像描述

it works. Why is the -p flag ignored?

On the docker run command, parameters in front of the image name are Docker parameters and parameters after the image name are for the program running in the image.

-p and -d are for Docker, so they need to go in front of the image name, like this

docker run -d -p 8090:80 theImageName

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