2024年2月

情况1

  1. linux 使用宿主机的网络地址 使用172.17.0.1(Docker默认桥接网络的网关地址)
  2. Windows或Mac,使用实际的IP地址或特定的地址(如host.docker.internal)

情况2

如果docker版本比较低,可以使用

docker run --add-host=host.docker.internal:host-gateway

对于 docker-compose.yml

services:
  your-service:
    extra_hosts:
      - "host.docker.internal:host-gateway"

如果无法识别 host-gateway,改为宿主机host

--add-host=host.docker.internal:<宿主机IP> ...

如果访问宿主机无法识别 host.docker.internal
报错
got error dial tcp: lookup host.docker.internal on 127.0.0.11:53: no such host
宿主机手动添加host

127.0.0.1 host.docker.internal

终极大招

修改宿主机防火墙规则,增加ip来源

sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="3399" protocol="tcp" accept'

sudo firewall-cmd --reload

来源 https://www.zackwu.com/posts/2021-03-14-why-i-always-get-503-with-golang/
修复

package main

import (
    "crypto/tls"
    "fmt"
    "net/http"
)

func main() {
    defaultCipherSuites := []uint16{0xc02f, 0xc030, 0xc02b, 0xc02c, 0xcca8, 0xcca9, 0xc013, 0xc009, 
            0xc014, 0xc00a, 0x009c, 0x009d, 0x002f, 0x0035, 0xc012, 0x000a}
    client := http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{
                CipherSuites: append(defaultCipherSuites[8:], defaultCipherSuites[:8]...),
            },
        },
    }
    req, err := http.NewRequest("GET", "https://www.bundesregierung.de/breg-en", nil)
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer resp.Body.Close()
    fmt.Println(resp.Status)
}

// Output:
// 200 OK