简体   繁体   English

Docker-compose 无法访问 .NET Core

[英]Docker-compose with .NET Core unreachable

Im new to Docker and im trying to set up 2 containers, one running mongoDB and one running the web application.我是 Docker 的新手,我正在尝试设置 2 个容器,一个运行 mongoDB,一个运行 Web 应用程序。

The problem is that I can not access my .NET core application via localhost:5000 or 0.0.0.0:5000.问题是我无法通过 localhost:5000 或 0.0.0.0:5000 访问我的 .NET 核心应用程序。

Mongo is running fine. Mongo 运行良好。

Here is my docker-compose.yml这是我的 docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
     - "5000:5000"
  mongo:
    build: ../../../docker/wishare-mongo/
    volumes:
     - ../../../docker/wishare-mongo:/data/db
    ports:
     - "27017:27017"

Dockerfile for .NET Core app .NET Core 应用程序的 Dockerfile

FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
ENV ASPNETCORE_URLS="http://*:5000"

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
EXPOSE 5000
ENTRYPOINT ["dotnet", "Wishare-Integration-Api.dll"]

The docker-compose build and docker-compose up runs without an error with this resulting for web container: docker-compose build 和 docker-compose up 运行没有错误,这会导致 web 容器:

web_1 | web_1 | Hosting environment: Production web_1 |托管环境:生产web_1 | Content root path: /app web_1 |内容根路径:/app web_1 | Now listening on: http://[::]:80 web_1 |现在收听: http://[::]:80 web_1 | Application started.应用程序启动。 Press Ctrl+C to shut down.按 Ctrl+C 关闭。

And this is the result of docker ps, port 5000 should be exported based on docker ps result.这是 docker ps 的结果,端口 5000 应该根据 docker ps 结果导出。

Docker ps 结果

Any ideas?有任何想法吗?

Looks like .NET uses port 80 in production, so when Ive changed docker file and exported port 80 instead of 5000 and also changed port 5000 to port 80 in docker-compose it works as supposed.看起来 .NET 在生产中使用端口 80,所以当我更改了 docker 文件并导出端口 80 而不是 5000 并且还在 docker-compose 中将端口 5000 更改为端口 80 时,它会按预期工作。

So I assume in order to run it on port 5000 it will need some configuration on .NET side.所以我假设为了在端口 5000 上运行它,它需要在 .NET 端进行一些配置。

Problem & Solution Summary:问题和解决方案摘要:
By default, ASP.NET Core only listens to localhost, instead of any IP addresses, usually this is configured in code level or appSettings.json or launchSettings.json: http://localhost:5000 .默认情况下,ASP.NET Core 只侦听 localhost,而不是任何 IP 地址,通常这是在代码级别或 appSettings.json 或 launchSettings.json: http://localhost:5000 中配置的 You can easily change this to " http://*:5000 " to solve the problem.您可以轻松地将其更改为“ http://*:5000 ”来解决问题。 (You can specify the IPs which you want the app to listen to) (您可以指定您希望应用程序侦听的 IP)

Verification:确认:

Works in container with localhost:在具有本地主机的容器中工作:

We can get into container bash,我们可以进入容器 bash,

docker exec -ti <container name> /bin/bash

Verify by running通过运行验证

curl -X GET http://localhost:5000

Doesn't work in container with IP在具有 IP 的容器中不起作用

curl -X GET http://{thecontainer IP}:5000

Doesn't work in the local machine with localhost在使用 localhost 的本地机器上不起作用

In docker compose, even mapped 5000:5000.在 docker compose 中,甚至映射了 5000:5000。 But it doesn't work when access http://localhost:5000 on local pc但是在本地电脑上访问 http://localhost:5000 时不起作用

Doesn't work in the local machine with IP在有 IP 的本地机器上不起作用

  1. use ipconfig to get your local ip.使用 ipconfig 获取您的本地 ip。 ie 192.168.1.123即 192.168.1.123
  2. run your asp.net core app运行您的 asp.net 核心应用程序
  3. access your app within browser by using IP instead of localhost: http://19.2.168.1.123 You will see it won't work.使用 IP 而不是 localhost 在浏览器中访问您的应用程序: http://19.2.168.1.123您将看到它不起作用。

Why, Here is how the browser routes to the App in docker container?为什么,这是浏览器如何路由到 docker 容器中的应用程序?

  1. your localhost (type in browser) -> your local IP 192.168.1.123您的本地主机(在浏览器中输入)-> 您的本地 IP 192.168.1.123
  2. your local IP 192.168.1.123 -> Container IP: 192.168.4.45 (per your docker desktop setting)您的本地 IP 192.168.1.123 -> 容器 IP:192.168.4.45(根据您的 docker 桌面设置)
  3. Container IP: 192.168.4.45 -> Container localhost容器 IP:192.168.4.45 -> 容器本地主机
  4. Container localhost -> the App running Container. Container localhost -> 运行容器的应用程序。

If the app only listens localhost, then,如果应用程序只侦听本地主机,那么,

Step 4: works.第 4 步:工作。

Step 3 won't work,第 3 步不起作用,

Step 2 and Step 1 won't work.第 2 步和第 1 步将不起作用。

So, the solution is to change the application to make sure it listens to both localhost & the desired IPs.因此,解决方案是更改应用程序以确保它同时侦听本地主机和所需的 IP。

Here is one example (ASP.NET Core 3.1) how to solve the problem.这是一个如何解决问题的示例(ASP.NET Core 3.1)。 Definitely, you can put these URLs into appSettings, or other configurations.当然,您可以将这些 URL 放入 appSettings 或其他配置中。

        internal static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    // In order to use Docker Container, we need to use *:5000
                    // to listen all IPs instead of only localhost.
                    // feel free to move these IPs settings into appSettings.json
                    webBuilder.UseUrls("http://*:5000","https://*:5001");
                })
                .UseAutofac()
                .UseSerilog();

Note: How to get container IP注意:如何获取容器IP

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

ServerUrls explaination from MSDN来自 MSDN 的 ServerUrls 解释

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-5.0#server-urls (Instead of hardcode in configuration, you can use environment variable ASPNETCORE_URLS, too) https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-5.0#server-urls (您也可以使用环境变量 ASPNETCORE_URLS,而不是配置中的硬编码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker环境多容器结构中PostgreSQL和.Net Core的docker-compose文件应该怎么做? - How should be docker-compose file for PostgreSQL and .Net Core in Docker Environment multi-container structure? Docker:无法使用 docker-compose 运行 .net core api:以代码 145 退出 - Docker: Unable to run .net core api with docker-compose : exited with code 145 Docker 设置中的 .Net Core 3.1 MSSQL 后端:Docker-compose up 产生连接失败 - .Net Core 3.1 MSSQL backend in Docker setup: Docker-compose up producing connection failure docker-compose up 失败:当前 .NET SDK 不支持面向 .NET Core 2.2 - docker-compose up failes: The current .NET SDK does not support targeting .NET Core 2.2 Docker-Compose YML 与 ASP.NET Core 2.2 Web API 和 SQL Server 2017 - Docker-Compose YML with ASP.NET Core 2.2 Web API and SQL Server 2017 从C#.Net 4.7运行docker-compose命令 - Run docker-compose command from C# .Net 4.7 Docker 组合 .Net Core NUnit 测试和 Redis - Docker compose .Net Core NUnit Test and Redis 为什么我的 .NET Web API 应用程序在使用 docker-compose 时无法连接到 docker 上的 MySQL? - Why does my .NET web API application not connect to MySQL on docker when using docker-compose? .NET Core/SQL Server docker 组合网络问题 - .NET Core/ SQL Server docker compose networking issue Docker-compose 未能读取 Dockerfile - Docker-compose failed to read Dockerfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM