title: Gitlab CI how to run tests before building docker image
body: <p>I have a Python based repository and I am trying to setup Gitlab CI to Build a Docker image using Dockerfile and pushing the image to Gitlab's Registry.</p>
  
  <p>Before building and deploying the Docker image to registry, I want to run my unit tests using Python. Here is my current gitlab-ci.yml file that only does testing:</p>
  
  <pre><code>image: python:3.7-slim
  before_script:
    - pip3 install -r requirements.txt
  
  test:
    variables:
      DJANGO_SECRET_KEY: some-key-here
    script:
    - python manage.py test
  
  build:
    DO NOT KNOW HOW TO DO IT
  </code></pre>
  
  <p>I am checking some templates from Gitlab's website and found one for Docker:</p>
  
  <pre><code># This file is a template, and might need editing before it works on your project.
  # Official docker image.
  image: docker:latest
  
  services:
    - docker:dind
  
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  
  build-master:
    stage: build
    script:
      - docker build --pull -t "$CI_REGISTRY_IMAGE" .
      - docker push "$CI_REGISTRY_IMAGE"
    only:
      - master
  
  build:
    stage: build
    script:
      - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
      - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
    except:
      - master
  </code></pre>
  
  <p>However, both of these do not work for me because I need to have python for testing and docker for building the image. Is there way to do it with Gitlab CI without creating a custom Docker image that has both python and Docker installed?</p>