How to use Docker with an existing Django project on my local machine ?

Published: September 24, 2020

Tags: Django; Docker;

DMCA.com Protection Status

An example (step by step) of how to use Docker with an existing Django project on my local machine:

An example of an existing Django project structure

So let's consider the following django project called hereafter "myproject" with the following structure (which is a common structure of a Django project):

myproject/

    manage.py

    mydatabase.db

    myproject/

            settings.py

    myfirstapp/

            models.py

            views.py

            templates/

media/

This project is working well on my local machine and in production as well. However I wanted to learn how to use Docker with an existing Django project to be able to share the code with others and work on the same environnement to continue developing the project. I went through a lot of online articles but the best answer of my problem was the video created by Christian Kreuzberger (that can be found here) which help me a lot to understand how to start with Docker for a Django project. Below you will find my notes on how to do that and the errors I had to fix.

How to use Docker to run a django project

To run a django project using Docker, we need to create two files "docker-compose.yml" (at the beginning of the project ) and "requirements.txt" (on the first folder myproject) using for example

touch docker-compose.yml

and

touch requirements.txt

So the structure of the Django project should now look like this:

docker-compose.yml

myproject/

    requirements.txt

    manage.py

    mydatabase.db

    myproject/

            settings.py

    myfirstapp/

            models.py

            views.py

            templates/

media/

Lets first edit the docker-compose.yml file with the minimal information required to be able to run Django with Docker:

version: '3'

services:
    python:
        image: python:3.6
        volumes:
            - ./myproject:/myproject
        ports:
            - 8000:8000
        command: bash -c "cd ./myproject && pip install -r requirements.txt && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"

In the requirements.txt file add all python modules required by your django project. For example in my case the requirements.txt file contains:

Django==2.2.7
Markdown==2.6.11
Pillow==3.4.2

Then to create and run the container just enter the following command:

docker-compose up

and enter

0.0.0.0:8000

in a web browser.

How to use Docker with an existing Django project on my local machine ?
How to use Docker with an existing Django project on my local machine ?

Errors I have made during the example presented above

In my first attempt with the docker-compose.yml file presented above, the execution worked correctly, however the url 0.0.0.0:8000 on the web browser returned an empty page. So first error I found was:

Invalid HTTP_HOST header: '0.0.0.0:8000'. You may need to add '0.0.0.0' to ALLOWED_HOSTS.

To fix that , I edited the settings.py file and added '0.0.0.0' in the ALLOWED_HOSTS list:

ALLOWED_HOSTS = ['0.0.0.0']

Second error, I forgot some python packages (i.e Markdown==2.6.110) in the requirements.txt file. So it is important to check first all the dependencies of the django project.

And the last one I got was:

You have 4 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth.
Run 'python manage.py migrate' to apply them.

So I added the "python manage.py migrate" in the docker-compose.yml file.

A simple solution to do an error analysis is to change the docker-compose.yml:

version: '3'

services:
    python:
        image: python:3.6
        volumes:
            - ./myproject:/myproject
        ports:
            - 8000:8000
        command: sleep infinity

and enter the following command in the terminal

 docker-compose exec python bash

which will open a console from the container (99b2d5d86c9d is the id of your container and should be different)

root@99b2d5d86c9d:

now we can enter some commands and try to understand why "python manage.py runserver 0.0.0.0:8000" returns an empty page. For example, I just did:

root@99b2d5d86c9d:  ls
root@99b2d5d86c9d:  cd myproject/
root@99b2d5d86c9d:  ls
root@99b2d5d86c9d:  pip install -r requirements.txt
root@99b2d5d86c9d:  python manage.py migrate
root@99b2d5d86c9d:  python manage.py runserver 8000

to find the errors that we talked previously.

Using a dockerfile

The example above presented a minimal configuration to run django with Docker. Let's now do that using a dockerfile. First create, for example, a folder Docker that contains another folder called python:

mkdir docker
mkdir docker/python

and create a Dockerfile

touch docker/python/Dockerfile

So the structure of the Django project should now look like this:

docker-compose.yml

docker/

    python/

        Dockerfile

myproject/

    requirements.txt

    manage.py

    mydatabase.db

    myproject/

            settings.py

    myfirstapp/

            models.py

            views.py

            templates/

media/

Edit the dockerfile:

From python:3.6

COPY ./myproject myproject

WORKDIR /myproject

RUN pip install -r requirements.txt

RUN python manage.py migrate

CMD ["python","manage.py","runserver", "0.0.0.0:8000"]

and update the docker-compose.yml file to use now the Dockerfile:

version: '3'

services:
    python:
        build:
            context: .
            dockerfile: docker/python/Dockerfile
        volumes:
            - ./myproject:/myproject
        ports:
            - 8000:8000

Then enter the following two commands:

docker-compose build

and

docker-compose up

And finally enter

0.0.0.0:8000

in a web browser.

That's it !. Hope you find something useful in that note.

References

Image

of