Deploy Django and MySQL using docker compose

Deploy Django and MySQL using docker compose

Complete Course to Deploy Container for Django and MySQL with Docker Compose | DevOps | Docker Tamil

Pre-Requisites to Deploy Container for Django and MySQL

  1. Install Docker
  2. Install Docker Compose
  3. Python 3.x or later

To Install Docker in ubuntu
https://docs.docker.com/engine/install/ubuntu/

1. Update the apt package index and install packages to allow apt to use a repository over HTTPS:

$ sudo apt-get update
$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg

2. Add Docker’s official GPG key:

$ sudo install -m 0755 -d /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
 $ sudo chmod a+r /etc/apt/keyrings/docker.gpg

3. Use the following command to set up the repository:

$ echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install Docker Engine

  1. Update the apt package index:
 sudo apt-get update

Install Docker Engine, containerd, and Docker Compose.

To install the latest version, run:

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Verify that the Docker Engine installation is successful by running the hello-world image:

 sudo docker run hello-world

Setting the Project Folder Directory

Create a project folder as “django-container-app”

$ mkdir django-container-app

Create a virtual env for django application in python

$ python3 -m venv django_app

Activate the virtual env for django application in Linux Ubuntu

$ source /django-app/bin/activate

Installing the django into the venv using pip or pip3

$ pip install django

create a django project inside the venv

$ django-admin startproject docker-app .

Now, go to the docker-app/settings.py. Add the ip address to the ALLOW_HOSTS

ALLOW_HOSTS = ["0.0.0.0",<vm_ip>,<container_ip>]

<vm_ip> – to access outside of the vm,

<container_ip> – to access inside the vm

Now, create a requirements.txt file, to install necessary python package inside the container

Django
sqlparse
psycopg2-binary
pytz
asgiref
mysqlclient

Now, create the Dockerfile to Deploy Python Django App

# Use an official Python runtime as a parent image
FROM python:3.9

ARG APP_PORT

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set the working directory to /code
WORKDIR /code

COPY requirements.txt requirements.txt

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt
	
COPY . /code

# Run migrations
RUN python manage.py makemigrations && python manage.py migrate

# Expose port 8000 for the Django app
EXPOSE $APP_PORT

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

Now, create a docker-compose.yam file, to deploy multiple micro services container like

Django as django_app as a container name,

MySQL as django_db as a container name

version: '3'

services:
  db:
    image: mysql:latest
    container_name: pure_db
    environment:
      DB_HOST: db
      MYSQL_DATABASE: sample_db
      MYSQL_USER: sample_user
      MYSQL_PASSWORD: sample_password
      MYSQL_ROOT_PASSWORD: sample123   
    restart: always 
    ports:
      - 3306:3306    

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: pure_app
    environment:
      DB_HOST: db
      DB_NAME: sample_db
      DB_USER: sample_user
      DB_PASSWORD: sample_password
    ports:
      - 8001:8000
    depends_on:
      - db
    links:
      - db

Now, Deploy the Docker container using docker-compose commands

$ docker-compose up

Go to the browser, and type http://192.168.0.8:8001

Thus, Succesfully, Deployed the Django and MySQL container

Leave a Reply

Your email address will not be published. Required fields are marked *