微服务无法使用 Docker for Mac 进行通信

Microservices can't communicate using Docker for Mac

我正在尝试获取两个容器,每个容器都是 运行 不同的 Go 服务。这两项服务都是使用 net/http 包构建的。我有一个 API 前端和一个身份验证服务后端。

这是我的撰写文件:

version: "2"
services:
  staticfiles:
    build: ./files
    volumes:
      - /public
      - /views
  api:
    build: ./api
    environment:
      - PORT=8080
      - BASE_URL=https://example.org
      - AUTH_HOST=auth
      - AUTH_PORT=8080
      - VIEW_DIR=/views
      - PUBLIC_DIR=/public
    ports:
      - "80:8080"
    volumes_from:
      - staticfiles:ro
    links:
      - auth
    depends_on:
      - staticfiles
  db:
    build: ./postgres
    environment:
      - POSTGRES_USER=inheritor
      - POSTGRES_DB=inheritor
  auth:
    build: ./auth
    expose:
      - "8080"
    environment:
      - PORT=8080
      - DB_USER=inheritor
      - DB_NAME=inheritor
      - DB_HOST=db
      - DB_Port=5432
    links:
      - db

我知道链接有效,因为从 api 容器我可以 ping authcurl -X Post http://auth:8080/validate 但在 Go 中我得到一个 dial address tcp i/o timeout。这是代码。

var (
    authString = "http://" + env.AuthHost + ":" + env.AuthPort
)

//ValidateToken validates a token using the session in DB
func ValidateToken(req *model.ValidateRequest) (*model.JWTClaims, error) {
    client := new(http.Client)
    api := authString + "/validate"
    cont, err := model.Jsonify(req)
    if err != nil {
        return nil, exception.NewInternalError("Could not turn the request into a json object.")
    }

    request, err := http.NewRequest("POST", api, bytes.NewBuffer(cont))
    if err != nil {
        return nil, exception.NewInternalError("Could not create request: " + err.Error())
    }
    request.Header.Set("Content-type", "application/json")
    response, err := client.Do(request)
    if err != nil {
        return nil, exception.NewInternalError("Could not make the request: " + err.Error())
    }
    defer response.Body.Close()

    res := new(model.AuthResponse)
    res.Claims = new(model.JWTClaims)
    decoder := json.NewDecoder(response.Body)
    err = decoder.Decode(&res)
    spew.Dump(response.Body)
    if err != nil {
        return nil, exception.NewInternalError("Could not parse response back from auth service. " + err.Error())
    }

    if response.StatusCode != http.StatusOK {
        return nil, exception.NewInvalidJWTError(res.Error)
    }

    return res.Claims, nil
}

client.Do(request) 是引发拨号错误的原因。现在我的身份验证服务甚至都没有被触及,因为我有一个记录器可以打印以筛选传入的每个请求。

非常感谢您的帮助。

如果有帮助我是运行MacOSX.

Client:
 Version:      1.12.0-rc4
 API version:  1.24
 Go version:   go1.6.2
 Git commit:   e4a0dbc
 Built:        Wed Jul 13 03:28:51 2016
 OS/Arch:      darwin/amd64
 Experimental: true

Server:
 Version:      1.12.0-rc4
 API version:  1.24
 Go version:   go1.6.2
 Git commit:   e4a0dbc
 Built:        Wed Jul 13 03:28:51 2016
 OS/Arch:      linux/amd64
 Experimental: true

两个 Dockerfile 看起来像这样:

FROM golang:1.6
RUN mkdir -p /go/src/github.com/dixonwille/Inheritor/api
WORKDIR /go/src/github.com/dixonwille/Inheritor/api

COPY . /go/src/github.com/dixonwille/Inheritor/api

RUN go build -v -o Inheritor cmd/Inheritor/main.go

USER nobody
ENTRYPOINT ["./Inheritor"]

编辑:

I 运行 net.LookupHost(env.AuthHost) 在 Go 中,它返回一个与 pingcurl 甚至 docker inspect 不同的 IP 地址。这是 Go 的东西吗?

编辑:

如果我删除 authString 的端口部分,请求会通过但在解析响应时出现错误。响应是 NGINX 的 301 重定向,我认为它是 st运行ge 因为它甚至不在我的堆栈中。重定向的位置 header 是 localhost,我认为也是 st运行ge。

我曾尝试在主机上公开一个端口并使用该端口访问它,但没有更好的运气(相同的主机名)。

编辑:

所以我认为这是 Mac 唯一的事情。我在 Windows 10 上克隆了 repo 和 运行,并且能够连接到我的身份验证服务。这会是 Docker for Mac 错误吗?我可能会向他们报告,但我认为这不会关闭,因为它仍然是 Mac 用户的问题。

所以 Docker for Mac 今天刚刚发布了一个新的测试版。这似乎解决了我的连接问题。现在,当我发现它在我的 Windows 电脑上工作时,我确实对源代码进行了更改。

这里是 Docker 的修复版本:

Client:
 Version:      1.12.0
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   8eab29e
 Built:        Thu Jul 28 21:04:48 2016
 OS/Arch:      darwin/amd64
 Experimental: true

Server:
 Version:      1.12.0
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   8eab29e
 Built:        Thu Jul 28 21:04:48 2016
 OS/Arch:      linux/amd64
 Experimental: true

这是撰写文件:

version: "2"
services:
  staticfiles:
    build: ./files
    volumes:
      - /public
      - /views
      - /migrations
  databasefiles:
    build: ./databasefiles
    volumes:
      - /var/lib/postgresql/data
  db:
    build: ./postgres
    depends_on:
      - databasefiles
    volumes_from:
      - databasefiles
    environment:
      - POSTGRES_USER=inheritor
      - POSTGRES_DB=inheritor
  auth:
    build: ./auth
    expose:
      - "8080"
    depends_on:
      - staticfiles
    volumes_from:
      - staticfiles:ro
    environment:
      - PORT=8080
      - DB_USER=inheritor
      - DB_NAME=inheritor
      - DB_HOST=db
      - DB_PORT=5432
      - MIGRATION_DIR=/migrations
    links:
      - db
  api:
    build: ./api
    environment:
      - PORT=8080
      - BASE_URL=https://example.org
      - AUTH_HOST=auth
      - AUTH_PORT=8080
      - VIEW_DIR=/views
      - PUBLIC_DIR=/public
    ports:
      - "80:8080"
    volumes_from:
      - staticfiles:ro
    links:
      - auth
    depends_on:
      - staticfiles

我确实移动了服务,但我没有看到任何会改变容器之间通信的不同之处。这只是为了防止其他人有同样的问题。