Developing Django application using Docker Container

Akash V
3 min readMar 2, 2022

--

Long story short, here I will explain how I develop Django Applications using VS Code and Docker.

Prerequisites

  1. Docker installed
  2. VS Code installed

Steps

  1. Open VS Code
  2. Open Extensions from the sidebar and search for Remote — Containers extension and install it
  3. Create a new folder and open it in VS Code
  4. Create a docker-compose.yml file in the folder
  5. Add your docker-compose content. Mine looks like shown in the below image. I prefer creating .env files to hold the environment variables. This includes the Django Secret key and database secrets and anything you want to have in your environment variables. Note that, I also add a dependent database service. Will discuss more on this towards the end.
  6. Once created, open the command palette and type “reopen in container” and choose the docker-compose.yml file we created earlier in the command palette.
  7. Choose the service as web as we will be developing on top of the Python container
  8. Create a requirements.txt and add Django, psycopg2, and other dependencies
  9. This will create an additional “.devcontainer” folder containing a devcontainer.json and a different docker-compose.yml later it will build a docker container (2 in our case)
  10. Add python and python test explorer extensions to the extensions list in the devcontainer.json
  11. Add any ports to be forwarded
  12. Add any OS level dependencies in the “postCreateCommand” section. For eg: “pip install -r requirements.txt”. (It is also essential to add any commands you run to make changes within the container here. Else during rebuilding, the changes will be lost and you will have to re-run them again)
  13. Open command palette and type rebuild container.
  14. Once the container is successfully rebuilt, you can open a terminal and boom! You will see a terminal opened within the container.
  15. Now create your Django project as you do always by running “django-admin startproject project_name . ”
  16. Kaboom! Your new django-project is created and you can continue your development here.
Adding the remote — containers extensions
docker-compose.yml
step 6 to reopen in container
Adding extensions to the remote vs code
My basic requirements.txt

Notes:

  1. It is important to add the python extension to the extension list
  2. Developing using docker also allow me to build a separate database for each project and hence I attach it as a dependent service in my docker-compose. You can change this to any other service you like.
  3. Make use of the “postCreateCommand” to set up your docker for the development. This includes installing the python libraries, OS packages, and updates.
  4. This approach can be used for many applications supported by VS Code.
  5. This is the same approach you follow on Windows, Linux, and Mac.
  6. If you are using database containers, you can refer to the service name in place of the hostname in the settings.py of Django configurations.

Like the content? Please share the read with your fellow developers. Knowledge not shared is a crime!!

--

--