首页 > 解决方案 > 无法从 Docker 容器外部访问我的应用程序,但在容器内一切正常

问题描述

我正在尝试在 docker 容器中运行 Rhino Compute,但遇到了一个奇怪的问题。我使用下面的 Dockerfile 构建了一个映像,当我在本地运行它时,没有任何问题。

# escape=`

# see https://discourse.mcneel.com/t/docker-support/89322 for troubleshooting

# NOTE: use 'process' isolation to build image (otherwise rhino fails to install)

### builder image
FROM mcr.microsoft.com/dotnet/sdk:5.0 as builder

# copy everything, restore nuget packages and build app
COPY src/ ./src/
RUN dotnet publish -c Release -r win10-x64 --self-contained true src/compute.sln

### main image
# tag must match windows host for build (and run, if running with process isolation)
# e.g. 1903 for Windows 10 version 1903 host
FROM mcr.microsoft.com/windows:1809

#Copy the fonts and font install script
COPY fonts/* fonts/
COPY InstallFont.ps1 .

#Run font install scriptin powershell
RUN powershell -ExecutionPolicy Bypass -Command .\InstallFont.ps1

# install .net 4.8 if you're using the 1809 base image (see https://git.io/JUYio)
# comment this out for 1903 and newer
RUN curl -fSLo dotnet-framework-installer.exe https://download.visualstudio.microsoft.com/download/pr/7afca223-55d2-470a-8edc-6a1739ae3252/abd170b4b0ec15ad0222a809b761a036/ndp48-x86-x64-allos-enu.exe `
    && .\dotnet-framework-installer.exe /q `
    && del .\dotnet-framework-installer.exe `
    && powershell Remove-Item -Force -Recurse ${Env:TEMP}\*

# install rhino (with “-package -quiet” args)
# NOTE: edit this if you use a different version of rhino!
# the url below will always redirect to the latest rhino 7 (email required)
# https://www.rhino3d.com/download/rhino-for-windows/7/latest/direct?email=EMAIL
RUN curl -fSLo rhino_installer.exe https://www.rhino3d.com/download/rhino-for-windows/7/latest/direct?email=<myemail> `
    && .\rhino_installer.exe -package -quiet `
    && del .\rhino_installer.exe

# (optional) use the package manager to install plug-ins
# RUN ""C:\Program Files\Rhino 7\System\Yak.exe"" install jswan

# copy compute app to image
COPY --from=builder ["/src/dist", "/app"]
WORKDIR /app

# bind rhino.compute to port 5000

EXPOSE 5000

# uncomment to build core-hour billing credentials into image (not recommended)
# see https://developer.rhino3d.com/guides/compute/core-hour-billing/
#ENV RHINO_TOKEN=

CMD ["rhino.compute/rhino.compute.exe"]

应用代码在这里:https ://github.com/mcneel/compute.rhino3d

如前所述,当我对localhost:5000. 但是,当我尝试从主机卷曲时(运行后),我无法得到任何响应docker run -p nodeport:containerport imagename。我不确定它是否与防火墙有关,或者 Dockerfile 中的任何内容配置不正确。

任何帮助表示赞赏。

标签: dockerdockerfilefirewallwindows-containerrhino3d

解决方案


默认情况下,EXPOSE 指令不会公开容器的端口以供主机访问。换句话说,它只使声明的端口可用于容器间交互。例如,假设您有一个 Node.js 应用程序和一个 Redis 服务器部署在同一个 Docker 网络上。为了确保 Node.js 应用程序与 Redis 服务器通信,Redis 容器应该公开一个端口。如果您查看官方 Redis 映像的 Dockerfile,其中包含一行显示 EXPOSE 6379。这就是允许两个容器相互通信的原因。因此,当您的 Node.js 应用程序连接到 Redis 容器的 6379 端口时,EXPOSE 指令是确保容器间通信发生的原因。

您不能通过 Dockerfile 将端口发布到您的主机。您应该通过 docker-compose.yml 文件或命令行来执行此操作。使用 Dockerfile 构建映像后,您可以使用以下命令将容器的 5000 端口发布到主机的 5000 端口。

docker run -it image:tag -p 5000:5000

推荐阅读