Search⌘ K
AI Features

The Database and Config App Connection through Docker

Understand how to configure a Django application to use MySQL instead of the default SQLite database. Learn to modify the settings.py for database connection, set up MySQL as a Docker service with dependencies and health checks in docker-compose.yml, and verify the database connection through Docker commands.

Specifying the database in the settings.py file

Django automatically creates a db.sqlite3 file for us whenever we create a Django project. This is because SQLite is the default database for Django projects. However, we won't use this database. Instead, we'll delete the file and create our database of choice.

Let’s go to the settings.py file and specify the database—MySQL—we wish to use. In this file, we'll locate the DATABASES segment of the code and replace these lines:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

We'll replace the existing lines with these lines:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'config',
        'USER': 'microservice',
        'PASSWORD':
...