top of page
Writer's pictureRahul R

Django development using Docker and MariaDB

1. Install Docker on your local machine.

2. Create a new directory for your Django project.

3. In the new directory, create a file called Dockerfile and paste the following code:


FROM python:3.8

ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY requirements.txt /app/requirements.txt

RUN pip install -r requirements.txt

COPY . /app

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

4. Save the file and run the following command to build the Docker image:

docker build -t django-dev .

5. Pull the MariaDB image from Docker Hub:

docker pull mariadb:latest

6. Create a new Dockerfile in the root of your Django project.In the Dockerfile, add the following lines:

FROM mariadb:latest  ENV MYSQL_DATABASE='mydatabase' ENV MYSQL_USER='root' ENV MYSQL_PASSWORD='some_password' ENV MYSQL_ROOT_PASSWORD='some_password'  CMD ["mysqld"]

7. Build the Docker image:

docker build -t mariadb .

8. Create a new docker-compose.yml file in the root of your Django project. In the docker-compose.yml file, add the following lines:


version: '3'  
services:   
    django:     
        image: django-dev     
        ports:       
            - "8000:8000"     
            depends_on:       
                - mariadb    
    mariadb:     
        image: mariadb     
        ports:       
            - "3306:3306"     
        environment:       
            - MYSQL_DATABASE='mydatabase'       
            - MYSQL_USER='root'       
            - MYSQL_PASSWORD='some_password'       
            - MYSQL_ROOT_PASSWORD='some_password'

9. Run the following command to start the Docker containers:

docker-compose up -d

10. Open a web browser and navigate to http://localhost:8000. You should see the Django development server running.


Note : You can now connect the Django development server image to the MariaDB image by adding the following lines to your settings.py file:

DATABASES = {     
    'default': {         
        'ENGINE': 'django.db.backends.mysql',         
        'NAME': os.environ['MYSQL_DATABASE'],         
        'USER': os.environ['MYSQL_USER'],         
        'PASSWORD': os.environ['MYSQL_PASSWORD'],         
        'HOST': 'mariadb',         
        'PORT': '3306',     
        } 
    }

Save and restart the project and your development server will be up and running connected to mariaDB.

50 views0 comments

Recent Posts

See All

Kommentarer


bottom of page