我想用gitlab ci中的mongo db访问来测试我的nodejs应用程序。因此,我设置了mongo服务并尝试连接到它。 Docs说,应该可以通过主机名访问它。但似乎我的应用程序找不到mongo服务。 显式地图端口有可能吗?由于Dockerimage公开了27017,但我不知道gitlab的作用是什么。
.gitlab-ci.yml
test:
stage: test
image: docker:latest
services:
- docker:dind
- mongo:latest
variables:
MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: test
before_script:
- docker build -t app -f Dockerfile.test .
script:
- docker run app npm run test
错误
MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongo mongo:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
答案 0 :(得分:0)
您可以为mongo服务的主机名配置别名:
services:
- name: docker:dind
- name: mongo:latest
alias: mongo
否则,您可以转发您的mongo端口:
before_script:
- apt install -y socat
- socat tcp-listen:27017,fork tcp:mongo:27017 &
- docker build -t app -f Dockerfile.test .