After having to initiate multiple Django projects (all having more or less the same setup) I decided to create this boilerplate with the most common setup and services that I use for my projects.
So this is a complete dockerized project that is orchestrated by docker-compose that also includes some extra services such as Postgres and Redis and can be used as a base for many use cases.
I hope other people find it useful as well. If you do feel free to contribute.
- Django
- django-json-widget
- django-constance
- requests
- django-crispy-forms (Optional)
- As services:
- Postgres
- Redis
- NGIX with Let's Encrypt configuration script
Install cookiecutter:
pip install cookiecutterThen start the project:
cookiecutter https://github.com/AGTGreg/DjangoBoilerplate.gitYou will be prompted to answer some questions to customize the project.
The environment variables are in dev.env that is used by docker-compose.yml and prod.env that is used by docker-compose.prod.yml.
Make sure to edit them and change at least the POSTGRES_PASSWORD and DJANGO_SECRET_KEY
First you need to install Docker and Docker compose in your machine: Install Docker Then build the docker containers. Open your terminal and write:
$ docker-compose up --buildThis will run all the build scripts that create the necessary environment for the app to run. Nothing will be installed in your computer. Instead Docker will create containers that run Linux and install all the necessary libraries and dependencies and run the app in there.
I have prepared a script called initapp that will initialize the app for you. To run it open a new terminal and type:
$ docker psThis will list all containers that are currently running. We need the CONTAINER ID for app.
Then type the following (replace CONTAINER ID with the id your container has.):
docker exec -it <CONTAINER ID> ./manage.py initappThis will initialize the database, create tables for caching and create an admin user with whom you can access the admin panel.
The admin user will be initialized with these credentials: username: admin password: admin
You can create an admin user with other credentials like so:
docker exec -it <CONTAINER ID> ./manage.py initapp --username=admin --password=mysuperstrongpasswordExecute the script that will install dummy certificates so that ngix can start:
chmod +x webserver/scripts/init_letsencrypt.sh
sudo ./webserver/scripts/init_letsencrypt.shUse the docker-compose.prod.yml instead and follow the instructions of step "4. Initialize the app":
$ docker-compose -f docker-compose.prod.yml up --buildI would like to thank:
- Philipp For the
init-letsencryptscript and for his awesome instructions on setting up nginx and certbot.
- Set specific version for postgres image:
postgres:17.7-alpine - Fix redis_pr service in docker-compose.prod.yml
- remove version in docker-compose.prod.yml
- Bumped django version to latest LTS vesion (5.2.5).
- Bumped versions for
django-json-widgetandredis. - Bumped Redis version for the
redis_primage to8.0-alpinein the docker-compose files. - Removed version from docker-compose files.
- Added option to include
django-crispy-formswith Bootstrap5
- Removed django extensions.
- Moved copying of project files bellow package and dependency installation in Dockerfile.
- Add
--fix-missingin package installation. - Add versions to dependencies.
- Allow devs to set django_secret_key, db_user and db_password on project configuration.
- Set default email address to the email address specified in the project configuration.