Home > Enterprise >  Gitlab ci fails to run docker-compose for django app
Gitlab ci fails to run docker-compose for django app

Time:10-13

I am setting up a gitlab pipeline that I want to use to deploy a Django app on AWS using Terraform.

At the moment I am just setting up the pipeline so that validates the terraform and runs tests (pytest) and lynting.

The pipeline uses docker in docker and it looks like this:

image:
  name: hashicorp/terraform:1.0.5
  entrypoint:
    - '/usr/bin/env'
    - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'


stages:
  - Test and Lint


Test and Lint:
  image: docker:20.10.9
  services:
    - docker:20.10.9-dind
  stage: Test and Lint
  script:
    - apk add --update docker-compose
    - apk add python3
    - apk add py3-pip
    - docker-compose run --rm app sh -c "pytest && flake8"
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^(master|production)$/ || $CI_COMMIT_BRANCH =~ /^(master|production)$/'

The pipeline fails to run the tests due to a database error I think which is weird as I am using pytest to mock the django database.

If I just run: docker-compose run --rm app sh -c "pytest && flake8" on the terminal of my local machine all tests pass.

Any idea how can I debug this?

p.s. let me know if I need to add more info.

CodePudding user response:

I don't think you are able to run docker in the CI directly. You can specify which image to use in each step and then run the commands. For instance:

image: "python:3.7"

before_script:
  - python --version
  - pip install -r requirements.txt

stages:
  - Static Analysis
  - Test

unit_test:
  stage: Test
  script:
  - pytest

See, in this pipeline, I used the python:3.7 image. You can upload your docker image to some Registry and use it in the pipeline.

CodePudding user response:

I manage to solve it and the tests in CI pass using

  script:
    - apk add --update docker-compose
    - docker-compose up -d --build && docker-compose run --rm app sh -c "pytest && flake8"

  • Related