Dockerize Django 2 app with docker-compose – Part 1

Français Français

This post will explain how to start from scratch to dockerize Django 2 app with docker compose.

Tutorial on Youtube

Dockerize Django 2 app with docker-compose

Lets not waste time and go to the following steps.

1. Let’s make an empty directory named dockerize-django-app and add another folder inside name it webweb should contain the Django project.

mkdir dockerize-django-app
mkdir dockerize-django-app/web

2. Lets put a requirements.txt file inside the web folder and write this line in it to install Django version 2.0 inside the Docker container:

Django==2.0

3. Now let’s create a Dockerfile inside the web folder. This should contain the following lines ( all lines are commented ):

FROM python:3.4

# Ensure that Python outputs everything that's printed inside
# the application rather than buffering it.
ENV PYTHONUNBUFFERED 1


# Creation of the workdir
RUN mkdir /code

WORKDIR /code

# Add requirements.txt file to container
ADD requirements.txt /code/

# Install requirements
RUN pip install --upgrade pip
RUN pip install -r /code/requirements.txt

# Add the current directory(the web folder) to Docker container
ADD . /code/

We extend from the official Docker image for Python 3.4, copy the folder containing our requirements.txt files to the container, and then install Docker with pip install

4. Let’s create a file called docker-compose.yml in the root of dockerize-django-app directory.

The docker-compose.yml is used to describes the services that make our app. Here we only need a web service(Django). It also describes which Docker images these services will use, how they will link together, any volumes they might need mounted inside the containers. Finally, the docker-compose.yml file describes which ports these services expose.

version: "3"

services:

  web:
    build: ./web/
    ports:
      - "8000:8000"
    volumes:
      - ./web:/code
    command: django-admin startproject mysite .

Here we specify that we use the version 3 of docker-compose and we have only one service called web. The web service will use the 8000 port inside the Docker container and expose it for the host machine.

The command  django-admin startproject mysite . is used to generate a Django project named mysite in the current directory (/web/).

5. Let’s Build and Run the project to create a Django project directory structure.

docker-compose up --build

6. Now we have a new directory named mysite, but it’s protected for writing because Docker run with root privilege. So let’s set writable the generated files. At the root of project directory (dockerize-django-app) type:

sudo chown -R $USER:$USER web/mysite/
sudo chown -R $USER:$USER web/manage.py

7. After generating empty Django application, finally we can run it. Just change the docker-compose command for the web service to run Django server with the command   python manage.py runserver 0.0.0.0:8000

version: "3"

services:

  web:
    build: ./web/
    ports:
      - "8000:8000"
    volumes:
      - ./web:/code
    command: python manage.py runserver 0.0.0.0:8000

Now start the Docker container with: docker-compose up and open browser at http://localhost:8000

You should see this page:

Docker start page
Docker start page

And this console output:

Docker Django start project
Docker Django start project

Conclusion

In this tutorial we have see how to setup and run a very simple Django application with Docker using docker-compose.

You can find the project source code on Github: https://github.com/wassim6/dockerize-django-app-part-1/

In the next tutorial we will see how to apply Django migrations using a Docker service, because as you can see in the console output we have a warning message:

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.

Cheers!!