How to Run Django Application in Docker

Posted on

Introduction

In this article, the main focus will be about how to show to run a Django application in Docker. The process for running the Docker and also the Django application which is running inside that running Docker will use a local device. The local device will run using a Microsoft Windows operating system. In other words, the Docker service will use a Docker-based service exist in Microsoft Windows. It is a Docker Desktop application where running it in the local device will automatically start the Docker service. So, before going on running Django in Docker, just perform the following step :

  1. First of all, just install Docker Desktop for those using device running using Microsoft Windows. For more information on how to install Docker Desktop in Microsoft Windows, just see ‘How to Install Docker Desktop in Microsoft Windows‘.

  2. Next step, the reason for installing Docker Desktop for the device running using Microsoft Windows is for starting the Docker service. So, in the next step, just make sure to run the Docker service. In other words, just make sure to start the Docker service if it is not running. Just run the Docker Desktop application or run the ‘Docker’ service.

How to Run Django Application in Docker

In this part, there will be an information in sequences for running Django in Docker. After Docker service is running, just perform the following task :

  1. First of all, just look for a folder where to put all of the sources. For an example, it will choose ‘C:\django’.  In this folder, just create a new folder for an example ‘app’ to be able to store or to put the Django source code application or project. Below is the execution of it :

    C:\django>mkdir app
    
    C:\django>cd app
    
  2. Just in case, try to create a new python virtual enviroment for the testing purpose. It will be very useful to create a Django project in the next step. For more information about creating python virtual environment, just read ‘How to Create a Python Virtual Environment with a specific version of Python in Microsoft Windows‘ or ‘How to Specify Specific Python Version for Creating a Virtual Python Environment in Microsoft Windows‘. In this article, the example for creating the python virtual environment exist as follows :

    C:\django\app>python -m venv env
    
    C:\django\app>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\django\app
    
    01/11/2023 01:44 PM <DIR> .
    01/11/2023 01:43 PM <DIR> ..
    01/11/2023 01:44 PM <DIR> env
    0 File(s) 0 bytes
    3 Dir(s) 11,714,838,528 bytes free
    
    C:\django\app>cd env
    
    C:\django\app\env>cd Scripts
    
    C:\django\app\env\Scripts>activate
    
    (env) C:\django\app\env\Scripts>cd ..
    
    (env) C:\django\app\env>cd ..
    
    (env) C:\django\app>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\django\app
    
    01/11/2023 01:44 PM <DIR> .
    01/11/2023 01:43 PM <DIR> ..
    01/11/2023 01:44 PM <DIR> env
    0 File(s) 0 bytes
    3 Dir(s) 11,714,564,096 bytes free
    
    (env) C:\django\app>
    
  3. Next, just create a Django project. Read ‘How to Create Django Project in Microsoft Windows using Command Line‘ or ‘How to Create a New Django Project in Linux via Command Line‘ for more information about how to create a Django project. Below is the example for creating the Django project :

    (env) C:\django\app> django-admin startproject myapp
    Fatal error in launcher: Unable to create process using '"C:\Users\Personal\AppData\Local\Programs\Python\Python310\python.exe" "C:\Users\Personal\AppData\Local\Programs\Python\Python310\Scripts\django-admin.exe" startproject myapp': The system cannot find the file specified.
    
    (env) C:\django\app>python --version
    Python 3.10.5
    
    (env) C:\django\app>pip -V
    pip 22.0.4 from C:\django\app\env\lib\site-packages\pip (python 3.10)
    
    

    If it ends in failure, just install the Django library first using pip as follows :

    (env) C:\django\app>pip install django
    Collecting django
    Downloading Django-4.1.5-py3-none-any.whl (8.1 MB)
    ---------------------------------------- 8.1/8.1 MB 1.2 MB/s eta 0:00:00
    Collecting asgiref<4,>=3.5.2
    Downloading asgiref-3.6.0-py3-none-any.whl (23 kB)
    Collecting tzdata
    Downloading tzdata-2022.7-py2.py3-none-any.whl (340 kB)
    ---------------------------------------- 340.1/340.1 KB 1.4 MB/s eta 0:00:00
    Collecting sqlparse>=0.2.2
    Using cached sqlparse-0.4.3-py3-none-any.whl (42 kB)
    Installing collected packages: tzdata, sqlparse, asgiref, django
    Successfully installed asgiref-3.6.0 django-4.1.5 sqlparse-0.4.3 tzdata-2022.7
    WARNING: You are using pip version 22.0.4; however, version 22.3.1 is available.
    You should consider upgrading via the 'C:\django\app\env\Scripts\python.exe -m pip install --upgrade pip' command.
    
    (env) C:\django\app>
    

    Do not forget to update the pip version if there is a message informing it to do it. So, upgrade the ‘pip’ version as follows  :

    (env) C:\django\app>python -m pip install --upgrade pip
    Requirement already satisfied: pip in c:\django\app\env\lib\site-packages (22.0.4)
    Collecting pip
    Using cached pip-22.3.1-py3-none-any.whl (2.1 MB)
    Installing collected packages: pip
    Attempting uninstall: pip
    Found existing installation: pip 22.0.4
    Uninstalling pip-22.0.4:
    Successfully uninstalled pip-22.0.4
    Successfully installed pip-22.3.1
    
    (env) C:\django\app>
    

    After that, just create a Django project with the name of ‘myapp’ as follows :

    (env) C:\django\app>django-admin startproject myapp
    
    (env) C:\django\app>
    

    Below is the result of structure from the process for creating the Django project :

    (env) C:\django\app>cd myapp
    
    (env) C:\django\app\myapp>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\django\app\myapp
    
    01/11/2023 01:46 PM <DIR> .
    01/11/2023 01:46 PM <DIR> ..
    01/11/2023 01:46 PM           683 manage.py
    01/11/2023 01:46 PM <DIR>         myapp
    1 File(s) 683 bytes
    3 Dir(s) 11,662,913,536 bytes free
    
    (env) C:\django\app\myapp>
    
  4. Following after, just create a Dockerfile in the folder app. Fill the Dockerfile with the following content :
    FROM python:3.9
    ADD . /usr/src/app
    WORKDIR /usr/src/app
    COPY requirements.txt ./
    RUN pip install -r requirements.txt
    EXPOSE 8000
    CMD exec gunicorn myapp.wsgi:application--bind0.0.0.0:8000--workers3
    
  5. Another one, create the requirements.txt file. Fill it with the following content :

    Django
    gunicorn
    

    After creating those two files, the following is the structure of the content of the folders :

    C:\django\app\myapp>dir 
    Volume in drive C is Windows-SSD 
    Volume Serial Number is CA30-19A4 
    
    Directory of C:\django\app\myapp 
    
    01/11/2023 01:43 PM <DIR> . 
    01/11/2023 11:24 AM <DIR> .. 
    01/11/2023 01:46 PM <DIR>           myapp 
    01/11/2023 01:44 PM             575 Dockerfile 
    12/24/2021 06:55 AM           1,236 requirements.txt 
    2 File(s) 1,811 bytes 
    3 Dir(s) 10,869,399,552 bytes free 
    
    C:\django\app\myapp>
    
  6. Continue on the step, build the Docker image by executing the following command  :

    (env) C:\django\app\myapp>docker build -t django_image .
    [+] Building 464.0s (10/10) FINISHED
    => [internal] load build definition from Dockerfile 0.1s
    => => transferring dockerfile: 522B 0.0s
    => [internal] load .dockerignore 0.1s
    => => transferring context: 2B 0.0s
    => [internal] load metadata for docker.io/library/python:3.9 9.9s
    => [internal] load build context 0.1s
    => => transferring context: 1.48kB 0.0s
    => [1/5] FROM docker.io/library/python:3.9@sha256:5d0b605039499444251643500686a6e52f2549f4bb1b16cbbb67a4ea04f4c306 430.3s
    => => resolve docker.io/library/python:3.9@sha256:5d0b605039499444251643500686a6e52f2549f4bb1b16cbbb67a4ea04f4c306 0.0s
    => => sha256:62601620b18c3c37d803b937b12621a5ecd0808ab68e228b26bd4345d84e4016 2.22kB / 2.22kB 0.0s
    => => sha256:8f1c967991c4c44b126c74b97427c57242e5f1b624ee3a0b1842c3284f9302da 8.51kB / 8.51kB 0.0s
    => => sha256:56261d0e6b05ece42650b14830960db5b42a9f23479d868256f91d96869ac0c2 10.88MB / 10.88MB 37.3s
    => => sha256:5d0b605039499444251643500686a6e52f2549f4bb1b16cbbb67a4ea04f4c306 1.86kB / 1.86kB 0.0s
    => => sha256:bbeef03cda1f5d6c9e20c310c1c91382a6b0a1a2501c3436b28152f13896f082 55.03MB / 55.03MB 181.0s
    => => sha256:f049f75f014ee8fec2d4728b203c9cbee0502ce142aec030f874aa28359e25f1 5.16MB / 5.16MB 26.2s
    => => sha256:9bd150679dbdb02d9d4df4457d54211d6ee719ca7bc77747a7be4cd99ae03988 54.58MB / 54.58MB 273.4s
    => => sha256:5b282ee9da04b94adc14866f2da98baed3cdf897c27f7490b8d3b1ae1522ddc7 196.88MB / 196.88MB 422.7s
    => => sha256:03f027d5e312b7dc7e22a0da00fb3baa10fcbb6b4426fc57412a15fc1f8f8ff6 6.29MB / 6.29MB 200.8s
    => => extracting sha256:bbeef03cda1f5d6c9e20c310c1c91382a6b0a1a2501c3436b28152f13896f082 4.4s
    => => extracting sha256:f049f75f014ee8fec2d4728b203c9cbee0502ce142aec030f874aa28359e25f1 0.5s
    => => extracting sha256:56261d0e6b05ece42650b14830960db5b42a9f23479d868256f91d96869ac0c2 0.4s
    => => sha256:286b1eb8968191fe88a5abaef38fa18e152d35a0df47d6548d31e4986bcc0332 18.32MB / 18.32MB 257.8s
    => => sha256:ac2f1c18cd990ffa45314bceab6ca6e8d58c914dcb2552c04e2cea78de56ce3a 234B / 234B 260.7s
    => => sha256:a9498bae351fe4699584736ffbf41025c4da662b285ea4036519f8a96a865618 2.88MB / 2.88MB 271.7s
    => => extracting sha256:9bd150679dbdb02d9d4df4457d54211d6ee719ca7bc77747a7be4cd99ae03988 3.3s
    => => extracting sha256:5b282ee9da04b94adc14866f2da98baed3cdf897c27f7490b8d3b1ae1522ddc7 5.3s
    => => extracting sha256:03f027d5e312b7dc7e22a0da00fb3baa10fcbb6b4426fc57412a15fc1f8f8ff6 0.3s
    => => extracting sha256:286b1eb8968191fe88a5abaef38fa18e152d35a0df47d6548d31e4986bcc0332 0.7s
    => => extracting sha256:ac2f1c18cd990ffa45314bceab6ca6e8d58c914dcb2552c04e2cea78de56ce3a 0.0s
    => => extracting sha256:a9498bae351fe4699584736ffbf41025c4da662b285ea4036519f8a96a865618 0.3s
    => [2/5] ADD . /usr/src/app 0.5s
    => [3/5] WORKDIR /usr/src/app 0.1s
    => [4/5] COPY requirements.txt ./ 0.1s
    => [5/5] RUN pip install -r requirements.txt 22.1s
    => exporting to image 0.8s
    => => exporting layers 0.7s
    => => writing image sha256:b57a07bb4ce0fa959052c6d3cc866a7ffc731c72bd79126a1974d702e3452292 0.0s
    => => naming to docker.io/library/django_image 0.0s
    
    Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
    
    (env) C:\django\app\myapp>
    

    After successfully creating the Docker image, check it first whether it is available or not by typing the following command :

    (env) C:\django\app\myapp>docker image ls
    REPOSITORY   TAG    IMAGE ID     CREATED        SIZE
    django_image latest b57a07bb4ce0 53 minutes ago 957MB
    
    (env) C:\django\app\myapp>
    
  7. Finally, run the Django for running a new Django container using the Django image with the name ‘django_image’ as follows :

    (env) C:\django\app\myapp>docker run -p 8000:8000 django_image
    [2023-01-12 00:43:59 +0000] [1] [INFO] Starting gunicorn 20.1.0
    [2023-01-12 00:43:59 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
    [2023-01-12 00:43:59 +0000] [1] [INFO] Using worker: sync
    [2023-01-12 00:43:59 +0000] [7] [INFO] Booting worker with pid: 7
    [2023-01-12 00:43:59 +0000] [8] [INFO] Booting worker with pid: 8
    [2023-01-12 00:43:59 +0000] [9] [INFO] Booting worker with pid: 9
    Not Found: /static/admin/css/fonts.css
    Not Found: /favicon.ico
    [2023-01-12 01:11:18 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:7)
    [2023-01-12 01:11:18 +0000] [7] [INFO] Worker exiting (pid: 7)
    [2023-01-12 01:11:19 +0000] [10] [INFO] Booting worker with pid: 10
    
  8. Access the Django application in the web browser as follows :

    How to Run Django in Docker
    How to Run Django in Docker

Leave a Reply