Dockerize Django
Docker introduction and Dockerizing Django application with PostgreSQL database.

Search for a command to run...
Docker introduction and Dockerizing Django application with PostgreSQL database.

In this series, we will be introducing to docker, write Dockerfile to build container running django application. Then dockerize Django with PostgreSQL, Gunicorn and nginx to deploy.
Optimizing Docker image and Docker permissions.
Welcome to the world of machine learning! In this beginner-friendly blog post, we'll embark on a journey to understand the fundamentals of machine learning using PyTorch, a popular deep learning framework. Whether you're completely new to the field o...

Introduction to Kubernetes

Optimizing Docker image and Docker permissions.

On this page
Hi, this is the first part of series where we will be dockerizing a django application with PostgreSQL database and deploying in server. For production environment we'll use Gunicorn and Nginx. In this post we will write a Dockerfile to build a container running a Django application. Dockerize django application with PostgreSQL database.
Docker is a platform for building, running and shipping applications in consistent manner. It helps to package our application and run it in any machine anywhere. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.
Docker provides the ability to package and run an application in a loosely isolated environment called a container. Containers are isolated environment for running the application. The isolation and security allows you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host. You can create, start, stop, move, or delete a container using the Docker API or CLI. By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine. Difference between containers and virtual machines is that virtual machines virtualize an entire machine down to the hardware layers and containers only virtualize software layers above the operating system level.
If you already have running django application you can skip this part to writing a Dockerfile here. If not let's create new django project.
django-admin startproject DjangoWithDocker
cd DjangoWithDocker
python manage.py runserver
pip install psycopg2-binary==2.9.3
pip freeze > requirements.txt
While running Docker run command docker uses Dockerfile to build the image. It is a text document that contains commands to build a docker image.
Now, create Dockerfile (no any extension) on the root of you project and add code below in file.
FROM python:3.10-alpine
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
RUN pip install --upgrade pip
ENV APP_DIR /home/DockerWithDjango
WORKDIR ${APP_DIR}
ADD requirements.txt ${APP_DIR}/
RUN pip install -r ${APP_DIR}/requirements.txt
COPY . ${APP_DIR}
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
docker build -t django-with-docker .
docker run django-with-docker
To run django application in browser run following command and visit localhost:8000 in browser.
docker run --publish 8000:8000 django-with-docker
version: "3.9"
volumes:
dbdata:
networks:
django_withdocker:
driver: bridge
services:
web:
build:
context: .
volumes:
- .:/home/django-with-docker
ports:
- 8000:8000
command: python manage.py runserver 0.0.0.0:8000
container_name: django_with_docker_web
restart: always
depends_on:
- db
links:
- db
networks:
- django_withdocker
db:
image: postgres
volumes:
- dbdata:/var/lib/postgresql
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
networks:
- django_withdocker
DB_NAME = 'postgres',
DB_USER = 'postgres',
DB_PASSWORD = 'postgres',
DB_HOST = 'db',
DB_PORT = 5432
docker-compose build
docker-compose up
Or just run following code to build and run
docker-compose up --build
docker-compose run web python manage.py makemigrations
docker-compose run web python manage.py migrate