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
- Install Docker
- Install Docker Compose
- 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 \
    gnupg2. 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.gpg3. 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/nullInstall Docker Engine
- Update the aptpackage index:
 sudo apt-get updateInstall 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-pluginVerify that the Docker Engine installation is successful by running the hello-world image:
 sudo docker run hello-worldSetting the Project Folder Directory
Create a project folder as “django-container-app”
$ mkdir django-container-appCreate a virtual env for django application in python
$ python3 -m venv django_appActivate the virtual env for django application in Linux Ubuntu
$ source /django-app/bin/activateInstalling the django into the venv using pip or pip3
$ pip install djangocreate 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
mysqlclientNow, 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:
      - dbNow, 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
 
							