Friday, June 23, 2023

Dockerizing a Fresh Python Django Web Application

Python is a computer programming language. often used to build websites and software, automate tasks, and conduct data analysis.

It is used for:-

  • web development (server-side),
  • software development,
  • system scripting,
  • automate tasks & conduct data analysis.

What is Django?

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development.

What is Docker?

Docker is a platform for building, shipping, and running applications in containers.

In Simple Term: Docker gives you the ability to run your applications within a controlled environment, known as a container, built according to the instructions you define. A container leverages your machine’s resources much like a traditional virtual machine (VM). However, containers differ greatly from traditional virtual machines in terms of system resources. Traditional virtual machines operate using Hypervisors, which manage the virtualization of the underlying hardware to the VM. This means they are large in terms of system requirements.


Step 1) Install Required Software


  •  Install Python
$ sudo apt update
$ sudo apt install software-properties-common -y
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt install python3.8 -y
$ python3 --version

Step 2) Install Django & Dependencies

$ sudo apt install python3-pip
$ sudo apt-get install python3.8-venv -y
$ sudo apt-get install python3-django -y


Step 3) Prepare Your Virtual Environment

$ cd /var/www/html/
$ sudo python3 -m venv env
$ source env/bin/activate

$ python3 -m pip install django
$ python -m pip freeze > requirements.txt
  •  Install & Update All Packages & Dependencies Via Single File Every Time.
$ python -m pip install -r requirements.txt


Step 4) Set Up a Django Project


$ django-admin startproject myproject

Running this command creates a default folder structure,
which includes some Python files and your management app that has the same name as your project:-


In the above picture, you can see the folder structure that the startproject command created for you:

  • myproject/ is your top-level project folder.
  • myproject/myproject/ is your lower-level folder that represents your management app.
  • manage.py is a Python file that serves as the command center of your project. It does the same as the django-admin command-line utility.
  • The myproject/myproject/ folder contains files that you’ll edit when you work on your web application.

Step 5) Start a Django App

$ cd /var/www/html/myproject

  •  This Command Will Run The Project On Python Default Port 8000
$ python manage.py runserver

  •  Run With A Specific Port Number
$ python manage.py runserver 4022

  • check In Browser
http://127.0.0.1:4022

Step 6) Now Let's create a Dockerfile and add following content.




Step 7) Build the Docker image.

$ docker build -t your_image_name .

Step 8) Run the Docker container: After building the image, you can start a container based on it using the following command.

$ docker run -p 8000:8000 -d your_image_name

Step 9) Check the container status.

$ sudo ps -a

Step 10) check In Browser this time we are checking for docker application

http://127.0.0.1:4022

No comments:

Post a Comment

testing