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…
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:
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 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.
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.
# 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.
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.