Setting Up a Django Web App with Docker and MySQL

Sep 22, 2024 | Programming

Welcome to the exciting world of web development with Django! In this guide, we will walk through the steps required to set up a Django web application using Docker, MySQL, and other supporting technologies like SaltStack and Zabbix. Whether you are a beginner or an experienced developer, this article aims to provide clear, user-friendly instructions to facilitate your development journey.

Prerequisites

  • Operating System: CentOS 7.0
  • Python: 3.6.0
  • Django: 1.9.5
  • Docker: Installed and running
  • Basic familiarity with command line operations

Installation Steps

Follow these steps to create your Django application:

yum -y install docker
systemctl start docker
docker pull mysql
docker pull django:1.9.5
docker pull python:3.6.0
mkdir -p mysite/DjangoWeb

Creating Your Dockerfile

Now, let’s create a Dockerfile to define our custom environment:

FROM python:3.6.0
ENV PYTHONUNBUFFERED 1
RUN mkdir code
WORKDIR code
ADD . DjangoWeb/requirements.txt code
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
ADD . code

This Dockerfile sets up a Python environment, ensures that we have the correct versions of Django and other dependencies, and prepares the working directory.

Understanding the Dockerfile with an Analogy

Think of the Dockerfile as the blueprint for our home (application). Just as a blueprint specifies every aspect of our house – the structure, materials, and layout – the Dockerfile dictates how our software environment should be set up. The blocks of code inside the Dockerfile are like a list of instructions for builders to follow, ensuring that our house is both functional and attractive. The environment variables we set are like custom preferences in our home where we decide how we want to live.

Defining docker-compose.yml

Next, we’ll create a docker-compose.yml file that defines how our application and database will interact:

version: '3'
services:
  db:
    image: mysql
    expose:
      - 3306
    environment:
      - MYSQL_DATABASE=mysitedb
      - MYSQL_ROOT_PASSWORD=888888

  web:
    build: .
    command: python DjangoWeb/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:code
    ports:
      - 8000:8000
    links:
      - db

Building and Running the Application

Now that we have our Docker and docker-compose files in place, let’s build and run the application:

cd mysite
docker-compose build
docker-compose up

Setting Up Django Database Configuration

Edit the settings.py file in your Django application to configure the database:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysitedb',
        'USER': 'root',
        'PASSWORD': '888888',
        'HOST': 'db',
        'PORT': '3306',
    }
}

Troubleshooting Tips

If you encounter issues during your setup, here are a few troubleshooting ideas:

  • Ensure Docker is running by checking the service status with systemctl status docker.
  • Check if your Docker containers are running with docker ps.
  • If you face network issues, verify that your database configurations in settings.py are accurately defined.
  • Run docker-compose down and then docker-compose up to restart your services.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

Congratulations! You have successfully set up a Django web application running on Docker with a MySQL database. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox