Dockerizing a Django + MySql project

Blog
Spread the love

In this guide I show you how to run your Django app in one Docker container and your MySQL database in another and make them talk to each other. We are going to manage and run these two containers using Docker Compose.

I am assuming you already have a Django project locally that is using a local database and that your project has a requirements.txt file. You should also install Docker on your machine if you haven’t already.

Steps for Installing Docker:

1. Open the terminal on Ubuntu.

2. Remove any Docker files that are running in the system, using the following command:

$ sudo apt-get remove docker docker-engine docker.io

After entering the above command, you will need to enter the password of the root and press enter.

3. Check if the system is up-to-date using the following command:

$ sudo apt-get update

4. Install Docker using the following command:

$ sudo apt install docker.io

You’ll then get a prompt asking you to choose between y/n – choose y

5. Install all the dependency packages using the following command:

$ sudo snap install docker

6. Before testing Docker, check the version installed using the following command:

$ docker –version

About docker and Docker Compose:-

what is docker:-

docker is tool which is help full for making containers

docker is advance version of virtualization

Docker is a software platform that allows you to build, test, and deploy applications quickly.

Docker is written in the Go programming language

What is Docker Compose?

Docker Compose is a tool that assists in defining and sharing multi-container applications. By using Compose, we can define the services in a YAML file, as well as spin them up and tear them down with one single command.

Let’s dive right in:

Below is the project structure.

Magento to configuration

The first step is to create a Dockerfile in the root of your project (this is where you have your requirements.txt and

Inside docker file

Let’s go through that line by line:

1. FROM python:3.8 is the base image your Docker image will be built upon. This will be pulled from DockerHub.

2. ENV PYTHONUNBUFFERED 1 ensures that the output Django writes to the terminal comes out in real time without being buffered somewhere. This makes your Docker logs useful and complete.

3. WORKDIR /app creates a new folder in your container called app which will be your project’s root inside the container. It then sets that as the work directory in which the subsequent commands will be executed in.

4. COPY requirements.txt /app/requirements.txt copies your local requirements.txt file to the Docker container.

5. RUN pip install -r requirements.txt will make sure you have all your project dependencies installed inside the container.

6. COPY . /app and finally it’s time to copy your project’s content into the Docker container.

Now create a new file in your local project root. This one is called docker-compose.yml and is used for configuring Docker Compose.

Inside docker compose file

This defines two services: db and web.

Db uses an official MySQL image from DockerHub. We attach local port 3306 to port 3306 of this container. We then set a bunch of environment variables that are required for MySQL. Make sure you also set these in your Django database settings.

Volume define two volume mappings:

1. The first one maps the contents of var/run/mysqld of the container to a local folder on your machine. This file contains socket information that enables the web service (your Django app) to talk to the database service.

2. ./db:/var/lib/mysql creates a local db folder in your project root so that database information can be saved if you destroy the db service.

Web build its image using the Dockerfile we created earlier. It then runs the Django server and exposes port 8000 to your machine. We map your local project folder to the app folder and we bring in the socket file. We tell Docker Compose that this service is dependent on the db service.

Now all we have to do is to start everything up. In your project root run:

command:- $ docker-compose up

This should build your images and start the containers. Open a browser and go to 0.0.0.0:8000. You should see your app. Now if you open a new terminal and run a docker ps you will see the web and db containers are running:

command:- docker ps

If you want to stop Docker Compose, press Ctrl+C in the first terminal or run docker-compose stop in the project root folder in another terminal.

To run manage.py commands in the Django container (web):

command: – $ docker-compose run – -rm web python manage.py migrate

Now Run your project on map port inside docker-compose like: -8000

bluethinkinc_blog

bluethinkinc_blog

2022-11-04

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

Object Pool Pattern in Magento2

Spread the love What is Object Pool Pattern in Magento 2 An

JENKINS

Spread the love JENKINS Introduction and Installation

Virtual Type and Type

Spread the love Virtual type is one of the most common

Customize the length of Input field

Spread the love Sometimes we need to change the limit the

How to Add Custom Tab on Admin Dashboard and Count Website Total Visitor in Magento 2?

Spread the love In this blog we’ll see How to Add Custom

How to Add Sign Out Tab in Customer Account in Magento 2?

Spread the love In this blog we’ll see How to Add

How to create custom tab in product detail page in magento 2?

Spread the love In this blog we’ll see how to add

Understanding and Implementing CORS & CSRF

Spread the love What is security? The world has become more

bluethinkinc Blogs