简体   繁体   English

docker-compose 无法请求外部网络

[英]docker-compose can't request to outside network

I'm stuck on the problems.我被困在这些问题上。 I know It maybe is a bad question, but I can't do anything.我知道这可能是一个不好的问题,但我无能为力。 I have some code test like that:我有一些这样的代码测试:

        [HttpGet]
        public async Task<object> Get()
        {
            try
            {
                Console.WriteLine("Start");
                HttpClient httpClient = new HttpClient()
                {
                    BaseAddress = new Uri("https://pikbest.com")
                };
                Console.WriteLine("Start call");
                var response = await httpClient.GetStringAsync("?m=download&id=123&flag=1&free_zone=0");
                Console.WriteLine("Call ok");
            } catch(Exception ex)
            {
                Console.WriteLine($"Exception: {ex.InnerException}\n{ex.Message}\n{ex.StackTrace}");
                return "Not ok";

            }
            return "Ok";
        }

When I ran with Docker only, It's return Ok .当我只使用Docker运行时,它返回Ok But when I ran with Docker-compose , It's return Not ok .但是当我使用Docker-compose运行时,它返回Not ok

Dockerfile : Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build
WORKDIR /app
COPY . ./
RUN dotnet restore
RUN dotnet publish -c Release -o ./publish

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build /app/publish .
EXPOSE 80/tcp
ENTRYPOINT ["dotnet", "testhttpclient.dll"]

And here is docker-compose.yml :这是docker-compose.yml

version: "3"

services:
    serverside:
        build: ./testhttpclient
        container_name: testhttpclient
        ports:
            - '80:80'

This is Console.Write logs:这是 Console.Write 日志:

testhttpclient | Start
testhttpclient | Start call
testhttpclient | Exception: System.Net.Sockets.SocketException (11): Resource temporarily unavailable
testhttpclient |    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
testhttpclient | Resource temporarily unavailable
testhttpclient |    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
testhttpclient |    at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
testhttpclient |    at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
testhttpclient |    at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
testhttpclient |    at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
testhttpclient |    at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
testhttpclient |    at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
testhttpclient |    at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
testhttpclient |    at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
testhttpclient |    at testhttpclient.Controllers.WeatherForecastController.Get() in /app/Controllers/WeatherForecastController.cs:line 40
tController.Get(                                                                                                             ) in /app/Controllers/WeatherForecastController.cs:line 40

Really thank you!真的很谢谢你!

Try adding the network mode to the docker-compose.yml :尝试将网络模式添加到docker-compose.yml

version: "3"

services:
    serverside:
        build: ./testhttpclient
        container_name: testhttpclient
        network_mode: "host"
        ports:
            - '80:80'

If that doesn't work try network_mode : bridge如果这不起作用,请尝试network_mode : bridge

or create a network:或创建一个网络:

version: "3"

services:
    serverside:
        build: ./testhttpclient
        container_name: testhttpclient
        ports:
            - '80:80'
        networks:
            - "serverside_nw"

networks:
  serverside_nw:
    external: true

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM