首页 > 解决方案 > Mongodb gitlab CI docker连接被拒绝

问题描述

我正在尝试为我们的开源爱好项目创建一个 docker 测试环境。我们的 python 和 Angular 代码运行没有错误。我需要一些帮助来配置 docker runner 以包含 mongodb。

stages: 
- build
- test

services:
  - mongo

variables:
  MONGODB_URI: "mongodb://mongo/projekt_eszkozok"


build: 
  stage: build
  image: "python:3.8.2"
  script:
    - "pip3 install -r sources/backend/requirements.txt"

test:
  stage: test
  image: "python:3.8.2"
  script:
    - "pip3 install -r sources/backend/requirements.txt"
    - cd sources/backend
    - "python -m unittest discover tests/"

错误:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

此外,项目具有 Angular 前端可能很重要,一旦我们进行了第一次 Angular 测试,它将被添加到测试环境中。

Python 数据库配置:

MONGODB_SETTINGS = {
    'host': 'mongodb://127.0.0.1:27017/projekt_eszkozok'
}

还尝试过:

MONGODB_SETTINGS = {
    'host': 'mongodb://mongo:27017/projekt_eszkozok'
}

标签: pythonmongodbdockergitlab-ci

解决方案


正如 yippie-flu 和 Vishesh Kumar Singh 在MongoDB server doesn't start at gitlab runner using gitlab-ci 中localhost:27017回答的那样,您必须使用 gitlab-ci而不是,mongo:27017因为 mongo 服务在其自己的 Docker 容器中运行。


推荐阅读