当我运行docker compose时,我的golang(入口)容器失败,提示“建立Mongo会话时出错”

时间:2019-08-20 20:26:27

标签: mongodb docker go docker-compose dockerfile

我运行了一个docker-compose,我在golang容器上看到一条错误消息,提示“建立Mongo会话时出错”,然后该容器退出了。我不确定问题是否源于我的golang容器或mongo。

在这一点上,我尝试了很多事情。这是我的golang容器的docker日志文件。

golang的docker日志

To run in debug mode, run with '-d true' option

time="2019-08-20T20:12:12Z" level=info msg="Logging in INFO mode"
time="2019-08-20T20:12:12Z" level=info msg="Go version go1.7.3"
time="2019-08-20T20:12:12Z" level=info msg="Starting ingest service on port 4001"
time="2019-08-20T20:12:12Z" level=info msg="Koala Version Number: v19.33.0"
time="2019-08-20T20:12:23Z" level=error msg="Error establishing Mongo session"
panic: no reachable servers

goroutine 1 [running]:
panic(0x933c00, 0xc4201b4f70)
        /usr/local/go/src/runtime/panic.go:500 +0x1a1
ingest/mongo.InitDB(0x0, 0x0)
        /home/jsikala/Documents/koala/go/src/ingest/mongo/mongo.go:34 +0x27e
main.setup()
        /home/jsikala/Documents/koala/go/ingest.go:118 +0x3ce
main.main()
        /home/jsikala/Documents/koala/go/ingest.go:56 +0x292

这是我运行docker compose时遇到的错误。

这是我的 docker-compose.yml 文件。

version: "3"
volumes:
    data:
        external:
            name: ${MONGO_VOLUME_NAME}
services:
    rails:
        image: rails2
        container_name: koala_rails_${USER}
        environment: 
            - KOALA_ENV
            - RAILS_PORT
            - KOALA_INGEST_URL=${INGEST_PROTOCOL}://ingest:${INGEST_PORT}
            - KOALA_MONGO_URL=mongo_service:27017
            - KOALA_REDIS_URL=redis_service:6379
            - KOALA_PKI_IN_DEV
            - KOALA_USER_ID_HEADER
            - USER
            - USERNAME
            - KOALA_REGISTER_USER_URL
            - KOALA_SECURITY_VALIDATOR_URL
            - CERT_FILE_PEM=/usr/src/app/certs/public.pem
            - PRIVATE_CERT_FILE_PEM=/usr/src/app/certs/private-key.pem
            - SSL_CA_FILE=/usr/src/app/certs/ca.pem
            - LOGNAME
            - KOALA_SECRET_KEY_BASE
            - KOALA_MONGO_USERNAME
            - KOALA_MONGO_PASSWORD
            - KOALA_HELP_URL
            - KOALA_CONTACT_EMAIL
            - KOALA_USE_CERTS
            - BUNDLE_GEMFILE
            - KOALA_SERVER_URL
            - RAILS_SERVE_STATIC_FILES
            - RAILS_LOG_TO_STDOUT
        ports:
            - "${RAILS_PORT}:${RAILS_PORT}"
        volumes:
            - ${CERT_FILE_PEM}:/usr/src/app/certs/public.pem
            - ${PRIVATE_CERT_FILE_PEM}:/usr/src/app/certs/private-key.pem
            - ${SSL_CA_FILE}:/usr/src/app/certs/ca.pem
    mongo_service:
        image: mongo:3.2.0
        volumes:
            - data:/data/db
        ports:
            - "27017:27017"
    redis_service:
        image: redis:4.0.8
        ports:
            - "6379:6379"
    ingest:
        image: ingest
        container_name: koala_ingest_${USER}
        extra_hosts:
            - csie.as.northgrum.com:10.8.131.12
        environment: 
            - KOALA_ENV
            - KOALA_CONFIG_FILE=/go/config.yml
            - INGEST_PORT
            - LOGNAME
            - KOALA_MONGO_URL
            - KOALA_JIRA_URL
            - KOALA_JIRA_SESSION_URL
            - CERT_FILE_PEM=/go/certs/public.pem
            - PRIVATE_CERT_FILE_PEM=/go/certs/private-key.pem
            - SSL_CA_FILE=/go/certs/ca.pem
            - KOALA_REDIS_URL=redis_service:6379
            - KOALA_USE_CERTS
            - KOALA_MONGO_USERNAME
            - KOALA_MONGO_PASSWORD
            - JIRA_USERNAME
            - JIRA_PASSWORD
        ports:
            - "${INGEST_PORT}:${INGEST_PORT}"
        volumes:
            - ${CERT_FILE_PEM}:/go/certs/public.pem
            - ${PRIVATE_CERT_FILE_PEM}:/go/certs/private-key.pem
            - ${SSL_CA_FILE}:/go/certs/ca.pem
        links:
            - mongo_service
        depends_on:
            - mongo_service
            - redis_service

注意:摄取是golang服务

这是我用于golang的docker Dockerfile

FROM golang:1.11.5

COPY . .

WORKDIR /go
COPY config.yml /go
RUN mkdir -p $WORKDIR/logs
RUN mkdir -p $WORKDIR/certs

RUN cd $WORKDIR

ENV PATH $PATH:$WORKDIR

CMD ["./runIngest"]

如果按预期运行,golang将在mongo中提取数据。知道是什么原因导致了这个问题吗?

1 个答案:

答案 0 :(得分:-1)

要从Golang容器连接到Mongo容器,必须将Mongo Host设置为mongo_service_name:mongo_port。并确保Golang容器可以到达Mongo容器(通过使用network或links指令)。

请在KOALA_MONGO_URL服务中检查您的ingest

我不确定您的ingest服务中的Mongo客户是什么。但通常,KOALA_MONGO_URL可以是mongo_service:27017mongodb://mongo_service:27017/database_name