Skip to content

Learn how to containerize your Django project using Docker, including two different methods: using only a Docker file or using Docker Compose. Explore the syntax of both approaches and discover how to make real-time changes to your dockerized application as you develop it locally.

Notifications You must be signed in to change notification settings

Sudhanshu1304/Dockerizing-Django-Application

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dockerizing Django Application

Step 1


Create the django project

    # Let's name the project to be Dockertest 
    django-admin startproject dockertest

The basic file structure after running above command will look like

dockertest
|
|-- dockertest
|    |-- __init__.py
|    |-- asgi.py
|    |-- setting.py
|    |-- urls.py
|    |-- wsgi.py
|
|-- manage.py
|-- db.sqlite3

Step 2


Now create a requirements.txt file

This file will contain all the libraries to be installed to run the program

    # Run below command to create the requirements.txt
    pip freeze > requirements.txt

After running the above command the file structure should look like

dockertest
|
|-- dockertest
|    |-- __init__.py
|    |-- asgi.py
|    |-- setting.py
|    |-- urls.py
|    |-- wsgi.py
|
|-- manage.py
|-- db.sqlite3
|-- requirements.txt

Step 3


Create the docker file in the dir where requirements.txt exists. Although the files can be kept at different locations as well but then you have to prove the paths accordingly

    # Docker file content
    FROM python:3.8-slim-buster
    
    WORKDIR /app
    
    COPY requirements.txt requirements.txt
    
    RUN pip3 install -r requirements.txt
    
    COPY . .
    
    CMD [ "python3", "manage.py", "runserver" ,"0.0.0.0:8080" ]

Step 4


Run the below commands to create and run the image in the bash/cmd

    # here you can name anything to the docker image and the ".(dot)" represents the location where the dockerfile is kept. This file will create the docker image.
    
    docker build --tag MydockerImage .

    # This line will create a container out of the docker image you created above and run it.
    docker run -it -p 8000:8000 MydockerImage

Learn More in detail


To learn more in detail visit the medium blog

About

Learn how to containerize your Django project using Docker, including two different methods: using only a Docker file or using Docker Compose. Explore the syntax of both approaches and discover how to make real-time changes to your dockerized application as you develop it locally.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published