我试图获得两个分别运行不同的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 auth
,curl -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)
是引发Dial错误的原因。现在,我的身份验证服务甚至没有被触及,因为我有一个记录器,可以打印出来以筛选每个传入的请求。
env.AuthHost
映射到AUTH_HOST
环境变量。env.AuthPort
映射到Auth_PORT
环境变量。非常感谢您的帮助。
如果有帮助,我正在运行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"]
编辑:
我net.LookupHost(env.AuthHost)
在Go中运行,它返回一个不同的IP地址,然后ping
,curl
甚至docker inspect
。那是Go的事吗?
编辑:
如果我删除的端口部分authString
,则请求会通过,但解析响应时会出错。响应是NGINX的301重定向,我认为这很奇怪,因为它甚至不在我的堆栈中。重定向的位置标头是localhost
,我也觉得很奇怪。
我尝试在主机上公开一个端口,并使用该端口访问它而没有更好的运气(相同的主机名)。
编辑:
因此,我认为这仅是Mac。我克隆了存储库并在Windows 10上运行,并且能够连接到我的身份验证服务。这会是Docker for Mac错误吗?我可能会向他们报告,但我不会认为已关闭,因为对于Mac用户而言,这仍然是一个问题。
因此,适用于Mac的Docker今天才推出新的Beta版本。这似乎已经解决了我的连接问题。现在,当我发现源代码可以在我的Windows pc上运行时,确实对源代码进行了更改。
这是修复的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
我确实移动了服务,但没有发现会改变容器之间的通信的任何不同之处。这只是在其他人有相同问题的情况下。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句