0% found this document useful (0 votes)
11 views

Exp 11 Deploying a Django Application with MySQL Using Docker

This lab manual outlines the process of deploying a Django application with a MySQL database using Docker containers. It includes prerequisites, objectives, and detailed steps for setting up the environment, configuring the application, and ensuring connectivity between the containers. Students are also required to submit their code, output, conclusions, observations, and responses to questions related to the experiment.

Uploaded by

Hemil Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Exp 11 Deploying a Django Application with MySQL Using Docker

This lab manual outlines the process of deploying a Django application with a MySQL database using Docker containers. It includes prerequisites, objectives, and detailed steps for setting up the environment, configuring the application, and ensuring connectivity between the containers. Students are also required to submit their code, output, conclusions, observations, and responses to questions related to the experiment.

Uploaded by

Hemil Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)


IMAD
Lab Manual
PART A
Experiment No. 11
A.1 Aim: Deploying a Django Application with MySQL Using Docker

Objective: To understand how to deploy a Django application using MySQL database within Docker
containers and establish connectivity between them using direct network links.

A.2 Prerequisite:

 Knowledge of Docker, Docker networking, Django, and MySQL.

 Basic understanding of web applications and REST APIs.

A.3 Outcome:
After successful completion of this experiment, students will be able to:

 Deploy Django applications within a Docker container.

 Connect the Django application to MySQL running in another container.

 Understand the process of containerization and networking in Docker.

A.4 Theory
In this experiment, we deploy a Django application that uses MySQL as its database. Each
service runs in its separate container, and the Django app will connect to the MySQL container
over the Docker network. This approach ensures portability and consistency across
environments.

Steps for Setting up Django with MySQL using Docker:

1. Create a Docker Network


To allow both containers to communicate, create a network:

docker network create my_django_network

2. Set up the MySQL Container


Run the MySQL container using the official image, specifying the network and
environment variables:

docker run --name mysql-db --network my_django_network

-e MYSQL_ROOT_PASSWORD=rootpass
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
IMAD
Lab Manual
-e MYSQL_DATABASE=mydb

e MYSQL_USER=myuser

-e MYSQL_PASSWORD=mypassword

-d mysql:latest

3. Build the Django Application Container


Create a Dockerfile for your Django app:

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt /app/

RUN pip install -r requirements.txt

COPY . /app/

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

Build the Docker image:

docker build -t django-app . (Don’t forget the dot)

4. Run the Django Container


Start the Django container, connecting it to the same Docker network:

docker run --name django-app --network my_django_network

-e DB_HOST=mysql-db

-e DB_USER=myuser

-e DB_PASSWORD=mypassword

-e DB_NAME=mydb

-p 8000:8000 django-app
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
IMAD
Lab Manual
5. Configure Django Settings
Update the DATABASES section of settings.py to use environment variables:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'PORT': '3306',
}
}
Or

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database_name',
'USER': 'your_username',
'PASSWORD': 'your_password',
'HOST': 'localhost', # or your database host
'PORT': '3306', # default MySQL port
}
}
6. Run Migrations
Once the containers are up, run the migrations to set up the database schema:

docker exec -it django-app python manage.py migrate

7. Access the Application


Open your browser and navigate to http://localhost:8000 to access the Django app
connected to the MySQL database.

Scenario Problem Statement:


Problem Statement 1: Deploy a Django-based web application with a MySQL database using
Docker. Ensure the Django app can communicate with the MySQL database container.
Hints:

 Use Docker networking to establish communication between the Django and MySQL
containers.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
IMAD
Lab Manual
 Ensure correct environment variables are set for database connection.

 Use Django’s migration feature to set up the database schema.

PART B

(PART B: TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the practical. The
soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties
at the end of the practical in case the there is no Black board access available)

Roll No. : Name:


Class : Batch :
Date of Experiment : Date/Time of Submission :
Grade :

B.1 Code:

(Paste your Code here)

B.2 Output

(Take screen shots of the output at run time and paste it here)

B.3 Conclusion:

(Students must write the conclusion as per the attainment of individual outcome listed above)

B.3 Observations and Learning:

(Students must write their observations and learnings as per the attainment of individual outcome listed
above)

B.4 Question of Curiosity

(To be answered by student based on the practical performed and learning/observations)

1. How do you establish communication between the Django and MySQL containers using
Docker?
2. What are the benefits of containerizing web applications and databases?
3. How do environment variables enhance the portability of Dockerized applications?
4. Why is it essential to use docker network when setting up multi-container applications?
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
IMAD
Lab Manual
5. What is docker-compose? What is the significance of running migrations in Django using
docker-compose.xml?

You might also like