Docker is an open-source platform, and it makes it easy and possible to develop, run, and ship allocated applications with the help of a well-defined set of rules in controlled and distributed environments as a containerization platform.

Docker allows you to separate the code of your application from the infrastructure. Accordingly, you can build and deploy your application in a fast manner. Docker also allows you to control and manage your infrastructure. It can set up CI/CD pipelines for deploying the applications. Developers can easily deploy the application in more efficient, automated, and agile ways using containerized processes.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Docker Containers

Docker containers allow the developer to create a single package file to host or publish an application using its dependencies and transfer them from one computing environment to another with no breaking modifications.

Containerization is moderately more portable, scalable, flexible, and efficient, and it is the best preference and in demand for building applications these days.

Creating a Django Dockerfile

For creating a sample project, we need to follow the below commands:

  • pip install -r requirements.txt it installs the dependencies.
  • python -m unittest composeexample.utils it helps to run unit tests.
  • python manage.py runserver 0.0.0.0:8000 it starts the application locally.

After its launch, the application starts the Django starter page at localhost:8000.

To build a Django application is with a Dockerfile in simpler ways, we can create a Docker image. It contains everything using a pipeline: 

Below is the example of Dockerfile

FROM python:3.6-slim

ENV PYTHONDONTWRITEBYTECODE 1

ENV PYTHONUNBUFFERED 1

RUN mkdir /code

WORKDIR /code

RUN pip install --upgrade pip

COPY requirements.txt /code/

RUN pip install -r requirements.txt

COPY . /code/

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

The above example of Docker, performs as per the below: 

  1. It uses the Python image
  2. It contains the environment variables
  3. It copies the dependencies file within the container
  4. It upgrades pip and installs each dependency
  5. It copies the all source code
  6. It initialises the Django app

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

We can develop this image locally on the workstation and can perform, launch, and test the application.

DjangoDocker_1

FROM python:3.8.3-alpine

It sets and defines the base image and creates the Docker container.

WORKDIR /usr/src/app

It sets and configures the working directory within the container to /usr/src/app.

ENV PYTHONDONTWRITEBYTECODE 1

Contains Python from copying pyc files to the container.

ENV PYTHONUNBUFFERED 1

It catches the output to stdout and cleans and flushes out instantly,

It generates the logs in real-time, and monitors with the Django.

RUN pip install --upgrade pip

It installs and upgrades the pip version within the container.

COPY ./requirements.txt /usr/src/app

It copies and moves all the requirements.txt files within the container’s work directory.

RUN pip install -r requirements.txt installs

It contains each type of the important modules for the django_todo application to run in the container.

COPY . /usr/src/app

Copies and moves all the application source code to the working directory in the container.

EXPOSE 8000

Exposes port 8000 for access from other applications.

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] 

It defines the executable commands in the container.

Docker-Compose Django

Docker is a containerization, text document mechanism that can be used for creating application environments in consistent, standardized ways. It incorporates all the commands when a user sends them to construct an image.

Here is an example of how to create a DockerFile text document in the project root category with the command sequence to construct an image.

DjangoDocker_2

Shown below is the final DockerFile result for the above example:

DjangoDocker_3.

Following are the examples of writing DockerFile using Django docker file:

# It mentions Docker to use the official python 3 images using docker hub as a base image.

FROM python:3

# Here, we set the environmental variable that shows the output from python that is sent straight to the terminal without buffering it first.

ENV PYTHONUNBUFFERED 1

# Here, we set the container's working directory for this path to /app.

WORKDIR /app

# Here, we copy each file from our local project directory into the container.

ADD ./app

# Here, it runs the pip install command for all packages consolidated listed in the requirements.txt file.

RUN pip install -r requirements.txt

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Docker Compose

Docker Compose is a tremendous development mechanism that allows you to determine and execute applications that require many services running at the same time.

For configuring the services using the Docker Compose in your application, we need to create the docker-compose.yml file and it can then run in a remote environment accordingly. We can also use and compose various types of services, such as Nginx from the web server, and a database of PostgreSQL.

In the example here, we have an application that uses the SQLite database to configure external Postgres database services. For creating the features of the docker-compose.yml file for better reusability, we need to take the same root folder and use the below code:

Example:

version: '3.7'

services:

   web:

       build: .

       command: python manage.py runserver localhost:8000

       ports:

           - 8000:8000

The content of the docker-compose.yml file is as given in example below:

version: '3'

After the set configuration of the docker-compose file, you need to open the terminal and execute the docker-compose-up command to construct and perform your application. An can test and confirm in the running windows on the browser, as shown in the example below.

Compose is a technique to define and execute multi-container Docker applications. We can create ae docker-compose.yml file to set up and configure services with compose. We can create and start the services using a single command with its pre-defined configuration setting.

The compose file determines the services that construct your app. As per the below image, there are three services: Nginx, web, and DB.

DjangoDocker_4.

And the final version, as per the example, is shown below.

DjangoDocker_5.

NGINX : It is open-source software for caching, media streaming, reverse proxying, web serving, and load balancing. It can also manage large-scale numbers of requests and concurrent connections.

Web: Using the web section, we can run our Django application via Gunicorn. It is a Python WSGI HTTP Server for UNIX-based OS. It's a pre-fork worker instance. The Gunicorn server is extensively consistent with different web frameworks, entirely executed, light on server resources, and relatively quick. We can use Django 3 with an ASGI server like Uvicorn, which is established on uvloop and httptools, focusing on the best accurate speed.

db: The db extract is self-descriptive. We can use PostgreSQL as our datastore.

Conclusion

We hope this article helped you understand Django Docker. In this article, we discussed the concept of various samples using Django and compose examples. This article will be helpful to professional developers from Java and .net backgrounds, application architectures, cloud specialists, testers, and other learners looking for various ways to use Docker with the containers.

You can opt for our Full-Stack Web Development Certification Course to improve your career prospects.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 15 Apr, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 2 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 3 Apr, 2024

11 Months$ 1,499
Full Stack Developer - MERN Stack

Cohort Starts: 3 Apr, 2024

6 Months$ 1,449