Having Issues Accessing My ASP.NET Core Website in a Docker Container

0
1
Asked By EagleEye01 On

Hey everyone! I've been diving into Docker for the past couple of weeks, primarily on my old HP ProLiant server running Ubuntu Server with Docker installed. So far, I've successfully set up Minecraft and SQL Server containers, but now I'm trying to host an ASP.NET Core website and I'm running into some problems.

My goal is to make this website accessible via a subdomain so my friends can access it. Currently, when I run fresh containers on both my development machine and the server, the website just isn't reachable. In Docker Desktop, my ASP.NET container doesn't show any mapped ports, unlike another container I had running previously. I noticed that when inspecting the running container on my server, its IP starts with 172.x.x.x, while my server's LAN IP is 10.x.x.x, which has me puzzled.

I've developed the website using Visual Studio 2022 with .NET 8, ASP.NET Core, and MVC, and it connects to an SQL Server container on the same server. Initially, I added Docker support in VS, but had to remove it due to some issues with my MacBook not supporting virtualization. However, I've managed to re-add Docker support in a new branch to keep my work organized. After setting things to release mode and building, I was able to push my image to Docker Hub, pull it down on the server, and run it.

When I boot up the ASP.NET Core image, it runs fine and logs various info messages to the console, but I have to use Ctrl+C to regain control, which also stops the container.

I'm used to checking `docker ps -a` to see my containers and their status. My other containers show proper port mappings like 0.0.0.0:1433->1433/tcp, while my new ASP.NET container doesn't show any. I even tried using the `--network host` option, but it didn't change the port mapping either.

I've attached my Dockerfile below in case it helps:

```Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["hcMvc8/hcMvc8.csproj", "hcMvc8/"]
RUN dotnet restore "./hcMvc8/hcMvc8.csproj"
COPY . .
WORKDIR "/src/hcMvc8"
RUN dotnet build "./hcMvc8.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./hcMvc8.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "hcMvc8.dll"]
```

I'm really looking for help to get this website up and running. Thanks in advance!

2 Answers

Answered By CrazyCoder87 On

It sounds like you're doing a lot of things right, but the main issue might be with how you're running your container. Since your ASP.NET container doesn’t show any mapped ports, make sure when you run your container, you're using the `-p` flag to map your container ports to your host ports. It should look something like this: `docker run -p 8080:8080 -p 8081:8081 myContainer`. This way, Docker knows to expose those ports on your host machine!

TechGuru91 -

Definitely check the port mapping! If the container is running correctly and you see logs that indicate it's listening, but you still can't access it, it might also be a firewall issue on your server. Make sure the firewall allows incoming traffic on the specified ports.

Answered By DockerDynamo On

You mentioned trying `--network host`, which can sometimes work better for local testing if you want to directly use the host's network. Make sure the application within the container is actually listening on the correct ports as well. You can verify this by exec'ing into the container and checking with commands like `netstat` or `ss` to ensure the app is bound to the ports you expect.

DevNinja22 -

Good point! Also, check your application configuration to ensure it’s not just listening on localhost. It should be set to listen on all available interfaces. If you’re binding only to localhost, it won’t be accessible from outside the container.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.