Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Dockerize, Automate the build and deployment process for Python(Django)??

Hello folks,
It is the third blog of dockerizing and deployment process explanation for different platforms. Previously, How to Dockerize on the .NET platform. (Click on the context to reach that article directly)

Now before we go further, Let’s talk a little about us. we are an IT service firm called 9spl. We are known as a DevOps service provider or you can say DevOps advisory provider company that provides different kinds of services varying from IT consultancy services, making an application of iOS, Android, or build a website and containerization. We do all this. We often do webinars to helps those students who want to learn to Code.

Thanks to our habit of sharing knowledge through various mediums, we would get the idea of generating a series of blogs where everyone who wants to learn the dockerization on each different platform would be able to learn it. So, Without further delay let’s learn how to dockerize, automate, and deployment process for Python(Django)

You know If you want to cook anything you need some ingredients like water, oil, vegs, bread and so on. Similarly, before we build our deployment process let’s do a quick check whether you have the below necessary ingredients or not…

  • Python 3.7 or greater,
  • Python Pip, the package manager,
  • Docker,
  • Git and a GitHub account.

Check all the stuff. Now, let’s move on to coding. Below are the steps that you should follow in order to create the deployment process:

  • Clone the code to your machine using git
    1. git clone YOUR_REPOSITORY_URL
    2. cd
  •  Create a new file called Dockerfile in your project directory
    1. The Dockerfile defines an application’s image content via one or more build commands that configure that image. Once built, you can run the image in a container.
  • Add the following content to the Dockerfile


FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

This Dockerfile starts with a Python 3 parent image. The parent image is modified by adding a new code Directory. The parent image is further modified by installing the Python requirements defined in the requirements.txt file.

  • Save and close the Dockerfile
  • Create a requirements.txt in your project directory.
    1. This file is used by the RUN pip install -r requirements.txt command in your Dockerfile
  • Add the required software in the file
    1. Django>=3.0,=2.8
    2. psycopg2-binary>=2.8

Save and close the requirements.txt file
Create a file called docker-compose.yml in your project directory
The docker-compose.yml file describes the services that make your app. In this example, those services are a web server and database. The compose file also describes which Docker images these services use, how they link together, any volumes they might need to be mounted inside the containers. Finally, the docker-compose.yml file describes which ports these services expose.

Add the following configuration to the file


version: "3.9"
services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db

This file defines two services: The db service and the web service. Below are the Cautions that one should need to remember during or afterward process…

NOTE: This uses the build-in development server to run your application on port 8000. Do not use this in a production environment.

  • Create a Django project
    In this step, you create a Django starter project by building the image from the build context defined in the previous procedure.
  1. Change to the root of your project directory
  2. Create the Django project by running the docker-compose run command as follows.


sudo docker-compose run web django-admin startproject composeexample .

This instructs Compose to run Django-admin startproject composeexample in a container, using the web service’s image and configuration. Because the web image doesn’t exist yet, Compose builds it from the current directory, as specified by the build: . line in docker-compose.yml.

Once the web service image is built, Compose runs it and executes the django-admin startproject command in the container. This command instructs Django to create a set of files and directories representing a Django project.

  • Connect the database
    1. In your project directory, edit the composeexample/settings.py file.
    2. Replace the DATABASES = … with the following:


# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}

These settings are determined by the Postgres Docker image specified in docker-compose.yml.

  • Save and close the file.
  • Run the docker-compose up command from the top-level directory for your project.

At this point, your Django app should be running at port 8000 on your Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, go to http://localhost:8000 on a web browser to see the Django welcome page.

That’s it.

Now, you know how to create the code for the doockerizing, and deployment process for Python(Django). In the next article, we will talk about Android Native so stay tuned and meanwhile look our some of the other articles.

The post How to Dockerize, Automate the build and deployment process for Python(Django)?? appeared first on 9SPL.



This post first appeared on 9series Blog : Website Tech Blog | Mobile App Techinal, please read the originial post: here

Share the post

How to Dockerize, Automate the build and deployment process for Python(Django)??

×

Subscribe to 9series Blog : Website Tech Blog | Mobile App Techinal

Get updates delivered right to your inbox!

Thank you for your subscription

×