Exp 11 Deploying a Django Application with MySQL Using Docker
Exp 11 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:
A.3 Outcome:
After successful completion of this experiment, students will be able to:
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.
-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
FROM python:3.9-slim
WORKDIR /app
COPY . /app/
-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:
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.
PART B
(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)
B.1 Code:
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)
(Students must write their observations and learnings as per the attainment of individual outcome listed
above)
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?