Basic setup for running Django with Docker

Hello everyone! (`・ω・´)ゞToday I will show you the easiest way to set up a Django development environment using Docker.

I actively use Docker in my web application development because it provides an easy and clean development environment setup. I recommend all developers to use Docker, but what I find on the Internet is that many tech blog articles on Django Dockerization are not specific to the development environment, but to the production environment, i.e., what you need for local development. It is to include nginx and gunicorn configurations that are not needed.

These settings are excessive in a development environment. In the development phase, only the Django and database parts need to be containerized, and a local server is sufficient for the web server. This minimal configuration allows for quick setup and prevents the local database from becoming so bloated with past development data that you don't know where everything is.

So in this article, I will summarize my simple setup of a development environment using Docker. The files to create are a Dockerfile and a docker-compose.yaml file.

So let's get started!

Considerations:

I am using pycharm for development.

1. Create a Django project

2. Create Dockerfile and docker-compose.yaml files

3. Create a requirements.txt file

4. Place environment variables in the settings.py file

5. Build the Dockerfile

6. Confirm Building Django

7. Make the manage.py is available

8. Adding template, static folder, and STATICFILES_DIRS

The minimum setup is now complete. However, more work is needed to turn it into an application.

To configure the template and static folder settings, create a very simple application that only displays "hello world" in the template.

The next steps are as follows:

  • 8-1. Create a new application in the project.
  • 8-2. Create a template folder in the root directory and add a template such as `base.html`.
  • 8-3. Create a static folder in the root directory to add CSS and js files.

Let's walk through the details of these steps.

8-2. Create a template folder in the root directory and add a base.html template and a home.html template

9. Add minimal code to urls.py and views.py to display hello world.

Avoid Using the Same Port

That's all. This basic setup will be used as a premise in other articles on this blog. It is also helpful for quick environment creation in everyday app development. Please make use of it!

Lastly, the GitHub repository for this project can be found here.