DEV Community

Cover image for Django development using Docker as host - Part 2: Create scripts
Anuj Sharma
Anuj Sharma

Posted on

Django development using Docker as host - Part 2: Create scripts

In Part 1, we created a Dockerfile with the required system dependencies.

In this part, we will be creating the required files and scripts.

TOC

  1. Create requirements.txt
  2. Create entrypoint.sh
  3. Create docker_start.sh
  4. Create wait_for_it.sh
  5. Make scripts executable

Let's dive in

1. Create requirements.txt

First, we need to define our basic python dependency for the Django project setup. Although it is not required, to omit any error with the pip install -r requirements.txt inside the container, let's add our first dependency.

Django==3.1.5
Enter fullscreen mode Exit fullscreen mode

2. Create entrypoint.sh

Entrypoint script is executed every time the image is run. I recommend not to run the Django server using this.
Create a file scripts/docker/entrypoint.sh with the following content

#!/bin/sh

exec "$@"
Enter fullscreen mode Exit fullscreen mode

3. Create docker_start.sh

We will use this script to run the Django server.
Create a file scripts/docker/docker_start.sh with the following content

#!/usr/bin/env bash

echo -e "\e[34m >>> Migrating changes \e[97m"
python manage.py migrate
echo -e "\e[32m >>> migration completed \e[97m"

echo -e "\e[34m >>> Collecting Static files \e[97m"
python manage.py collectstatic --noinput
echo -e "\e[32m >>> Static files collect completed \e[97m"

python manage.py runserver 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

It will run the migration, copies the static files and run the server over port 8000

4. Create wait_for_it.sh

This script is needed to wait for the database connection to start before starting the Django server.
Create a file scripts/docker/wait_for_it.sh with the content from git repository vishnubob/wait-for-it

5. Make scripts executable

In order to execute the scripts, it is required to make them executable.
Run the following command in the terminal from the project directory

chmod +x scripts/docker/entrypoint.sh
chmod +x scripts/docker/docker_start.sh
chmod +x scripts/docker/wait_for_it.sh
Enter fullscreen mode Exit fullscreen mode

That's it for this part. Next, we will create the Django project using the docker image.

Top comments (0)