DotNet Core 2.2 Docker Compose运行正常但浏览器无法访问
Let's walk through the most likely issues and fixes for your scenario where your service appears to start but isn't accessible via browser or API calls:
1. Verify Port Mapping & Container Listening Port
Your docker-compose.yml maps host port 8080 to container port 80, but we need to confirm your .NET Core app is actually listening on port 80 inside the container:
- Check your Dockerfile: Ensure you've set the
ASPNETCORE_URLSenvironment variable to bind to all interfaces on port 80. Add this line to the base stage of your Dockerfile:ENV ASPNETCORE_URLS=http://+:80 - Verify the container's listening ports: Run
docker exec <your-container-id> netstat -tulpn(replace<your-container-id>with the ID of youronlinesm.logscontainer). You should see adotnetprocess listening on0.0.0.0:80.
2. Check App Startup Logs for Hidden Errors
Even if docker-compose up says the service is "running", there might be runtime errors preventing the app from accepting requests:
- Pull the full logs for your service with:
Look for exceptions (like database connection failures, missing configs, or startup errors) that might be stopping the app from fully initializing.docker logs onlinesm.logs
3. Test Network Connectivity Directly
Rule out port mapping or firewall issues:
- Try accessing the service directly via the container's internal IP:
- Get the container IP with
docker inspect onlinesm.logs | grep "IPAddress" - Use
curl <container-ip>:80or a browser to hit that address. If this works, the issue is with port mapping on your host.
- Get the container IP with
- Check if your host firewall is blocking port
8080. Temporarily disable it (or add an inbound rule) to test. - Try using
curl localhost:8080from your host machine—sometimes browser caching or extensions can interfere with requests.
4. Validate Your Dockerfile Configuration
Make sure your Dockerfile is properly set up for .NET Core 2.2. A minimal working example looks like this:
# Base image for runtime FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base WORKDIR /app EXPOSE 80 ENV ASPNETCORE_URLS=http://+:80 # Build stage FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build WORKDIR /src COPY ["YourApp.csproj", "./"] RUN dotnet restore "./YourApp.csproj" COPY . . RUN dotnet build "YourApp.csproj" -c Release -o /app/build # Publish stage FROM build AS publish RUN dotnet publish "YourApp.csproj" -c Release -o /app/publish # Final runtime image FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "YourApp.dll"]
Replace YourApp.csproj and YourApp.dll with your actual project names.
5. Check Container Health Status
Confirm your container isn't silently restarting:
- Run
docker psand look at theSTATUScolumn foronlinesm.logs. If it showsRestarting (x) ..., that means the app is crashing repeatedly—check the logs (step 2) for the root cause.
内容的提问来源于stack exchange,提问作者mmffggss

