Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to connect to app inside docker container inside wsl2 from Windows host?

On Windows:

I just created app via dotnet new mvc and created Dockerfile for that:

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"]

I then opened wsl2, went into /mnt/d/myproject and built built that docker build -t app . and started it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I see it running

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

also there are correct logs:

[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/

but I’m not sure how can I connect to it?

>Solution :

you need to expose port using -p

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

then you can reach your app at host_ip:80

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading