MongoDB在构建Docker镜像时无法运行测试 – 服务器选择错误:服务器选择超时。

huangapple go评论111阅读模式
英文:

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

Result

After get network id and use this command :

docker inspect &lt;your network id&gt;

Inside result schema get your Gateway ip

&quot;IPAM&quot;: {
    &quot;Driver&quot;: &quot;default&quot;,
    &quot;Options&quot;: null,
    &quot;Config&quot;: [
        {
            &quot;Subnet&quot;: &quot;111.11.0.0/11&quot;,
            &quot;Gateway&quot;: &quot;111.11.0.1&quot;
        }
    ]
}

and use that ip

database url:  mongodb://user:password@ &lt; your Gateway ip &gt; :27017/microservices

huangapple
  • 本文由 发表于 2022年7月3日 18:14:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/72845643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定