提问者:小点点

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


在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应用程序。并启动了它。

我看到它在跑

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/

但是我不确定我怎么能连接到它?


共1个答案

匿名用户

您需要使用-p公开端口

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

然后,您可以通过主机ip:80