英文:
MongoDB Cannot run tests when building Docker image - Server selection error: server selection timeout
问题
我正在一个容器中运行一个Mongo镜像,配置如下:
version: '3'
services:
mongodb:
image: mongo
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=microservices
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
我已经开始使用Golang构建微服务,并且我也想将它们容器化以加快开发过程。然而,当我尝试运行下面的Dockerfile时,它在运行测试命令时出现错误:
database url: mongodb://user:password@127.0.0.1:27017/microservices
server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: 127.0.0.1:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
panic: server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: 127.0.0.1:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
数据库连接是正常的,因为当我尝试从Go文件中运行测试时,它们是通过的。只有在构建容器时尝试运行它们时才会出现问题。
FROM golang:1.18 as build
WORKDIR /go/src/app
COPY . .
RUN go mod download
RUN go vet -v /go/src/app/...
RUN go test -v /go/src/app/...
RUN CGO_ENABLED=0 go build -o /go/bin/app/authsvc /go/src/app/authentication/main.go
FROM gcr.io/distroless/static-debian11
COPY --from=build /go/bin/app/authsvc /
COPY --from=build /go/src/app/authentication/.env /
CMD ["/authsvc"]
英文:
I'm running a mongo image in a container with this config:
version: '3'
services:
mongodb:
image: mongo
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=microservices
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
And I have started to build microservices with golang and I want to start dockerizing those as well to speed up the development process. However when I try to run this Dockerfile below it panics at the run test command with the error:
database url: mongodb://user:password@127.0.0.1:27017/microservices
server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: 127.0.0.1:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
panic: server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: 127.0.0.1:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
The db connection works since when I try to run the test from the go files, they pass. There only seems to be any problem when I try to run them from the container when building it up.
FROM golang:1.18 as build
WORKDIR /go/src/app
COPY . .
RUN go mod download
RUN go vet -v /go/src/app/...
RUN go test -v /go/src/app/...
RUN CGO_ENABLED=0 go build -o /go/bin/app/authsvc /go/src/app/authentication/main.go
FROM gcr.io/distroless/static-debian11
COPY --from=build /go/bin/app/authsvc /
COPY --from=build /go/src/app/authentication/.env /
CMD ["/authsvc"]
答案1
得分: 1
更改数据库URL
尝试使用MongoDB
数据库URL:mongodb://user:password@mongodb:27017/microservices
英文:
Change database url
try mongodb
database url: mongodb://user:password@mongodb:27017/microservices
答案2
得分: 1
首先,你需要使用以下命令检查默认网络的 IP 地址:
docker network ls
获取网络 ID 后,使用以下命令:
docker inspect <你的网络 ID>
在结果中找到 Gateway IP:
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "111.11.0.0/11",
"Gateway": "111.11.0.1"
}
]
}
然后使用该 IP 地址:
数据库 URL: mongodb://user:password@<你的 Gateway IP>:27017/microservices
英文:
So first of all you must check your default network ip with
docker network ls
After get network id and use this command :
docker inspect <your network id>
Inside result schema get your Gateway ip
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "111.11.0.0/11",
"Gateway": "111.11.0.1"
}
]
}
and use that ip
database url: mongodb://user:password@ < your Gateway ip > :27017/microservices
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论