如何从Windows主机连接到wsl2内docker容器内的应用程序



在Windows上:

我刚刚通过dotnet new mvc创建了应用程序,并为此创建了Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/App.csproj", "src/"]
RUN dotnet restore "src/App.csproj"
COPY . .
WORKDIR "/src/src"
RUN dotnet build "App.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "App.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "App.dll"]

然后我打开wsl2,进入/mnt/d/myproject,构建docker build -t app .并启动它

我看到它在运行

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9e17cbwe6e4c        test1               "dotnet App.dll"    5 days ago          Up About a minute   80/tcp, 443/tcp     affectionate_goldstine

还有正确的日志:

[10:54:13 WRN] Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
[10:54:13 INF] User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
[10:54:13 INF] Now listening on: http://[::]:80
[10:54:13 INF] Application started. Press Ctrl+C to shut down.
[10:54:13 INF] Hosting environment: Production
[10:54:13 INF] Content root path: /app/

但我不确定如何连接它?

您需要使用-p 公开端口

docker run -itd --name myapp -p 80:80 <image>

然后你可以在host_ip:80上访问你的应用程序

相关内容

  • 没有找到相关文章

最新更新