登录
首页 >  Golang >  Go问答

无法在 Docker Compose 中连接到 Mongo Atlas Cloud:消息头读取不完整和证书错误

来源:stackoverflow

时间:2024-04-20 19:18:43 496浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《无法在 Docker Compose 中连接到 Mongo Atlas Cloud:消息头读取不完整和证书错误》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我有一个 go 应用程序,它执行基本的 crud 操作并使用 mongodb 进行数据持久化。

我正在使用 mongo atlas 连接字符串将应用程序连接到 mongodb,当我在本地开发计算机上运行应用程序时,一切正常。但是,当我在 docker-compose 工作流程中将所有内容推送到 docker 容器时,我开始收到此错误:

server selection error: server selection timeout, current topology: { type: replicasetnoprimary, servers: [{ addr: app-test-shard-00-00-zfzs6.mongodb.net:27017, type: unknown, state: connected, average rtt: 0, last error: connection() : x509: certificate signed by unknown authority }, { addr: app-test-shard-00-01-zfzs6.mongodb.net:27017, type: unknown, state: connected, average rtt: 0, last error: connection() : x509: certificate signed by unknown authority }, { addr: app-test-shard-00-02-zfzs6.mongodb.net:27017, type: unknown, state: connected, average rtt: 0, last error: connection() : x509: certificate signed by unknown authority }, ] }

这就是我的 docker-compose.yml 的样子:

services: 
redis:
    image: redis
    restart: always
app:
    image: docker.pkg.github.com/<user>/<project>/<image>:latest
    ports: 
        - 80:8080
    environment: 
        - app_env=docker
        - gin_mode=release
    depends_on: 
        - redis
    restart: on-failure

这就是我的连接字符串的样子: mongodb://用户:[电子邮件受保护]:27017,app-test-shard-00-01-zfzs6.mongodb.net:27017,app-test-shard-00-02-zfzs6.mongodb.net:27017/test ?ssl=true&replicaset=app-test-shard-0&authsource=admin&retrywrites=true&w=majority

我已经检查了这个问题和其他几个问题,但似乎没有一个能解决这个问题。

我还检查了docker的网络配置,我正在使用桥接驱动程序,它应该允许访问外部应用程序(即mongo atlas)

go 版本:1.13

mongo 驱动程序:mongodb/mongo-go-driver v1.2.0

dockerfile

from golang:alpine as builder
workdir /build
copy . .
run apk add git openssh build-base
run git config --global url."[email protected]:".insteadof "https://github.com/"
copy keys/id_rsa /root/.ssh/id_rsa
run chmod 600 /root/.ssh/id_rsa
run ssh-keyscan -t rsa github.com >> /root/.ssh/known_hosts
env goprivate=github.com/contributor-org
run go build

from alpine
workdir /org
copy --from=builder /build/app .
copy config/*.yml config/
run mkdir -p /var/log
cmd ["./app"]

数据库连接代码:

type databaseclient struct { // implements the database interface
    collection *mongo.collection
}

func getnewclient(cfg *config.config, log *logger.logger) (database, error) {
    clientoptions := options.client().applyuri(cfg.database.connectionstring)
    client, err := mongo.newclient(clientoptions)
    if err != nil {
        log.errorln(err)
    }
    err = client.connect(context.background())

    if err != nil {
        log.fatalln("failed to connect to mongo!", err)
        return nil, err
    }

    log.info("connected to mongo!")

    db := client.database(cfg.database.database)
    if db == nil {
        err := fmt.errorf("db (%s) not found", cfg.database)
        log.error(err)
        return nil, err
    }
    collection := db.collection(cfg.database.collection)
    if collection == nil {
        err := fmt.errorf("collection (%s) not found on db (%s)", cfg.database.collection, cfg.database.database)
        log.error(err)
        return nil, err
    }
    return &databaseclient{collection: collection}, nil
}

数据库接口:

type Database interface {
    GetByBookname(context.Context, string) (*model.Book, error)
    GetOneByFilter(context.Context, map[string]interface{}) (*model.Book, error)
    GetAll(context.Context) ([]*model.Book, error)
    GetAllByFilters(context.Context, map[string]interface{}) ([]*model.Book, error)
    AddBook(context.Context, *model.Book) (string, error)
    UpdateBook(context.Context, string, map[string]interface{}) (*model.Book, error)
    DeactivateBook(context.Context, string) error
    OverwriteBook(context.Context, string, *model.Book) (*model.Book, error)
    OnboardBook(*model.Book, string, bool) (string, error)
}

解决方案


我通过将 &tlsinsecure=true 添加到 mongodb uri 快速修复了此问题。尽管如此,仍在寻找正确的方法来修复它。

编辑:

也解决了这个问题,将 ca-certificates 添加到 docker 映像:

# builder step...

from alpine:3
run apk update \
    && apk upgrade \
    && apk add --no-cache \
    ca-certificates \
    && update-ca-certificates 2>/dev/null || true
copy --from=builder /build/main ./
cmd ["/main"]

较小图像的另一个选项:

# Builder step...

FROM alpine:3 as certs
RUN apk --no-cache add ca-certificates

FROM scratch as app
COPY --from=builder /build/main ./
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ENTRYPOINT ["/main"]

好了,本文到此结束,带大家了解了《无法在 Docker Compose 中连接到 Mongo Atlas Cloud:消息头读取不完整和证书错误》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>