gpt4 book ai didi

mongodb - GitLab CI:Docker容器无法连接到MongoDB服务

转载 作者:行者123 更新时间:2023-12-02 19:17:42 26 4
gpt4 key购买 nike

在GitLab CI管道中,我需要将自己的C++代码从Docker容器连接到以servive身份运行的MongoDB,但无法连接。

这是显示问题的最小gitlab-ci.yml示例:

stages:
- connect1
- connect2

image: docker:latest

variables:
MONGOIMAGE: "mongo:4.2.3-bionic"
MONGO_INITDB_ROOT_USERNAME: "root"
MONGO_INITDB_ROOT_PASSWORD: "geheim"

connect1:
stage: connect1
services:
- name: $MONGOIMAGE
image: mongo
script:
- mongo --host mongo --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"

connect2:
stage: connect2
services:
- name: $MONGOIMAGE
script:
- docker run --rm mongo mongo --host mongo --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"

connect2的错误消息是找不到主机:
connecting to: mongodb://mongo:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-06-04T16:20:47.614+0000 E QUERY [js] Error: couldn't connect to server mongo:27017, connection attempt failed: HostNotFound: Could not find address for mongo:27017: SocketException: Host not found (authoritative) :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-06-04T16:20:47.615+0000 F - [main] exception: connect failed
2020-06-04T16:20:47.615+0000 E - [main] exiting with code 1

我已经尝试过 --host localhost,但这正在工作中。我怎样才能获得以 docker run开头的容器(如connect2中的容器)可以连接到MongoDB服务?

最佳答案

终于我找到了解决方案:

connect2:
stage: connect2
services:
- name: $MONGOIMAGE
script:
- ping mongo -c 5
- docker run --rm mongo mongo --add-host mongo:`cat /etc/hosts | grep mongo | awk '{print $1}'` --net host --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"

关于mongodb - GitLab CI:Docker容器无法连接到MongoDB服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62269708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com