Docker容器主机端口不起作用,而是在容器端口中工作

发布于 2025-02-01 05:27:18 字数 2292 浏览 5 评论 0原文

我没有在裸露的端口中输出输出,而是在Docker容器外壳中的容器端口中获取输出。我找不到其背后的根本原因。

我正在Windows容器环境中使用Docker。

我已经使用以下Docker文件创建了Docker映像。

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019

COPY ./PublishOutput/ /inetpub/wwwroot

# Give Full Access To Folder
RUN icacls 'c:/inetpub/wwwroot' /grant 'Everyone:(OI)(CI)F'
# Check that the files have been successfully copied
RUN dir 

在下面创建图像命令:

docker build -t projectimage .

创建图像详细信息后:

REPOSITORY    TAG     IMAGE ID       CREATED       SIZE
                 
projectimage  latest  4654c6606bbd   4 days ago    9.06GB

从上图中,我创建了一个Docker容器。

在命令下方创建一个容器:

docker container run -d -p 8080:80 -–name projectcontainer projectimage

创建容器详细信息后:

CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS              PORTS                  NAMES
73abe46e99e9   projectimage   "C:\\ServiceMonitor.e…"   2 minutes ago   Up About a minute   0.0.0.0:8080->80/tcp   projectcontainer

现在,我转到浏览器,尝试使用裸露的端口IE 8080访问容器,

http://localhost:8080

我将获得“ 404-文件或目录找不到”。在浏览器屏幕上输出。 以下是浏览器输出的屏幕截图。 浏览器输出的屏幕截图

容器的网络详细信息:

 "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "73abe46e99e9231b2d3ae67c436220e789b84990499b853310d4e9b1840b2118",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "80/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "8080"
                }
            ]
        },

现在,我进入Docker Container's Shell使用Docker shell的shell' Docker容器的外壳内的命令

docker exec -it projectcontainer cmd.exe

,我尝试使用80个容器端口访问容器

curl localhost:80

,然后我获得了输出“正确配置为工作” 这是预期的输出。 下面是附加的屏幕截图。 预期输出

那么,为什么我没有在浏览器中的暴露端口中获得预期的输出?如何解决这个问题。

另外,我能够从容器中ping托管机器,反之亦然。

您可以提供的任何帮助将不胜感激。

I am Not getting Output in the exposed port but got the output in the container port in the Docker container's shell. I am not able to find the root cause behind it.

I am using docker in a windows container environment.

I have created a docker image using the below docker file.

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019

COPY ./PublishOutput/ /inetpub/wwwroot

# Give Full Access To Folder
RUN icacls 'c:/inetpub/wwwroot' /grant 'Everyone:(OI)(CI)F'
# Check that the files have been successfully copied
RUN dir 

Create an image below command:

docker build -t projectimage .

After creating the image Details:

REPOSITORY    TAG     IMAGE ID       CREATED       SIZE
                 
projectimage  latest  4654c6606bbd   4 days ago    9.06GB

From the above image, I have created a docker container.

Create a container below command:

docker container run -d -p 8080:80 -–name projectcontainer projectimage

After creating the container Details:

CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS              PORTS                  NAMES
73abe46e99e9   projectimage   "C:\\ServiceMonitor.e…"   2 minutes ago   Up About a minute   0.0.0.0:8080->80/tcp   projectcontainer

Now, I go to the browser and try to access the container using the exposed port i.e 8080

http://localhost:8080

I get the "404 - File or directory not found." output on the browser screen.
below is the screenshot of the browser output.
screenshot of the browser output

NetWork Details of container:

 "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "73abe46e99e9231b2d3ae67c436220e789b84990499b853310d4e9b1840b2118",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "80/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "8080"
                }
            ]
        },

Now, I go into the Docker container's shell using the command

docker exec -it projectcontainer cmd.exe

Inside the Docker container's shell, I try to access the container using the container port i.e 80

curl localhost:80

and I got my output " correctly configured to work" which is the Expected Output.
below is the screenshot attached.
Expected Output

So, why I am not getting the Expected output in the exposed port in the browser? How to Resolve this Problem.

Also, I am able to ping the host machine from the container and vice-versa.

Any assistance you can provide would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文