I am trying to run a docker container with the mongo image inside my .gitlab-ci.yml file. Some of my python files talk to a mongo db. Here is what my .gitlab-ci.yml looks like:
default:
image: python:3.10
variables:
DOCKER_HOST: "tcp://docker:2375"
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
services:
- docker:dind
test-job:
stage: test
script:
# install virtualenv (installs into /usr/local/bin)
- pip3 install virtualenv
# create virtual env
- virtualenv venv
# activate it
- source venv/bin/activate
# install necessary packages into virtual env
- pip3 install -r requirements.txt
# Need to download docker, otherwise get a docker: command not found error
- apt-get update && apt-get install -y docker.io
# Run the mongo container
- docker run -d --name mongo-container mongo:latest
- python -m pytest tests/
However, I am getting a docker: error during connect: Post "http://docker:2375/v1.24/containers/create?name=mongo_": dial tcp 198.82.184.81:2375: connect: no route to host.
with this.
Does anyone have any advice on where to go with this? Thank you
Consider instantiating the mongo database as your gitlab service instead of using docker:dind
. This will create the mongo container for you in parallel with your main (python) container, which can then interact with the mongo container via the pymongo library.
.gitlab-ci.yml:
stages:
- test
variables:
MONGO_INITDB_ROOT_USERNAME: "admin"
MONGO_INITDB_ROOT_PASSWORD: "password"
services:
- name: mongo:4
alias: mongodb
test:
stage: test
image: python:3.11
before_script:
- python -m ensurepip
- pip install pymongo
script:
- python example.py
example.py:
from pymongo import MongoClient
# Connect to host "mongodb" (alias of the gitlab service) at default port (27017)
client = MongoClient("mongodb", 27017)
# Interact with the DB
db = client["my_database"]
collection = db["my_collection"]