TravisCI & Flake8 with Docker for Test-Driven Development (TDD)


Django, as we know, is all-encompassing, with a DB with an ORM , Authentication system, Django Rest Framework , built-in tests, and a web server gateway interface, its a power-house of a framework for web development.

Docker as we know changed the entire software development paradigm by containerizing what these frameworks could do and automation for CI/CD from Travis-CI makes it a great combination.

In this article, let's see how to get started with a Django project using Travis-CI, Docker, and to check for lint errors, we'll use Flake8. I Will also use PostgreSQL as a database for our project due to its popularity with this combination. I intend to only show how to set up the project. To know more on how to write tests, build this entire project, you may choose to follow this on GitHub here

It's done in the order of the following steps

Part 1: Set up the project

Create a new repository on Github

Add Dockerfile

Configure Docker Compose

Create a Django project

Part 2: Set up automation

   5. Enable Travis-CI 

   6. Create Travis-CI configuration file

       7. Start writing tests
  1. Open Github and create a new repository, initiate it with a, and an MIT license perhaps.

  2. Create a new file called 'Dockerfile' and write the code below

FROM python:3.8-alpine
MAINTAINER Madhusudhan Anand


COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client
RUN apk add --update --no-cache --virtual .tmp-build-deps \
 gcc libc-dev linux-headers postgresql-dev

RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
COPY ./app /app

RUN adduser -D user
USER user
  1. Then create another file docker-compose.yml
version: "3.3"

 context: .
            - "8000:8000"
            - ./app:/app
 command: >
            sh -c "python wait_for_db &&
                   python migrate &&
                   python runserver"
            - DB_HOST=db
            - DB_NAME=app
            - DB_USER=postgres
            - DB_PASS=supersecretpassword
            - db
 image: postgres:10-alpine
            - POSTGRES_DB=app
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=supersecretpassword

Then create a file where we will include all our file requirements for docker to build from. name it requirements.txt


  1. Open terminal(Mac or Linux)/Powershell(windows), (assuming you have already cloned your git repo) CD into your project, and type the command

django-admin startproject mysite This will create a new django project.

  1. Now go to and sign up with your GitHub login and activate the repository that you created

6 & 7. Then create a new file, with file name .travis.yml, and write the code below

language: python
    - 3.8

    - docker

before_script: pip install docker-compose

    - docker-compose run app sh -c "python test && flake8"
  1. That's it, now you can start running your tests first. I have worked a bit on this and I am halfway through the project. You can follow and look at all the test files to know how to write tests and also in addition to this, you may review the documentation.

Happy Coding!