Do Interviews Actually Work?

Based on 100 years of analysis of selection methods, interviews have been found to be 33% reliable at predicting performance in work (Schmidt, Oh & Shaffer, 2016). What does this mean for the most…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Dockerize your Django and React App

Recently I was working on my project of Bug Tracking App and wanted to try out Docker to run my app on different containers. The application I created was backed by a standard set of backend APIs created using Django REST Framework which would then serve React Frontend.

I had to create a user manual for the project on GitHub describing how to clone the Repo and setup Virtual Environment, install dependencies, create a new Database in MySQL, migrate the migrations in Django App, build the React App then finally run Django and Web-Pack-dev server.

Now, this is a lot of commands. Can we skip to a couple of commands to handle all this for us?

Answer: Yes. Use Docker.

According to Docker documentation.

“Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.

Well, that’s the “definition” stuff, what it actually means? Let’s say you made your web app and now want to use it in production. One option is to use web servers which is nothing but dedicated computers for web hosting and services. But with the age of Cloud Server Hosting, this method is neither optimized nor cost-efficient. How??

It's simple when your app is small enough why to use an entire server for its hosting when you should pay for only the services you use. That my friend is solved using virtualization which is nothing but breaking down hardware (actual server) to smaller (actually needed) ‘compartments’ and hence uses a single server for deploying many services in an efficient way. These smaller ‘compartments’ are called Virtual Machines.

But there’s a problem, say your app is something like 100–300MB but to use VMs you need to use a whole OS which can easily go GigaBytes.

Docker solves it by using containers to run different apps. First Docker creates an image from your app then runs them as containers. In this way instead of using an entire OS for only a single app, you use some common shared resources and run your app in containers using Docker Engine which sits on top of the OS (currently only Linux because it needs some Linux kernel features) and remove OS dependency from your app completely!!!

Dockerfile: A set of commands to build an image to be run as a container.

Docker-Compose: A file defining how to run a multi-container Docker application.

You can refer to my project structure to properly see where to add the required files.

Step 1: Create Dockerfile for backend image.

Let’s go line-by-line.

NOTE: The downloading does not occur in your local directory, it occurs in your image.

Step 2: Create Dockerfile for your frontend image.

Let’s go line-by-line again.

Step 3: Create Docker-Compose file

Let’s first discuss the basic syntax.

To use 3306 you would need to use sudo service mysql stop but your app needs MySQL so we can not stop it.

2. project service :

3. frontend service:

Note how you are supposed to set your settings.py in your Django App.

Note: I recommend using python-decouple for storing sensitive information.

The reason for the above is that localhost for the container is not 127.0.0.1 and hence it will through an error if you used localhost.

(There will be another IP instead of localhost as you are running on Docker container)

This is solved by checking the port connection.

The command

in project service checks if db service is properly set up and only then runs the migrate command.

You first need to build your images. Navigate to the folder containing docker-compose

Run your application.

-d stands for detached mode. You may need to give your app a minute to migrate the migrations, set up the backend, and run the servers. To check currently running containers

To stop your App.

Possible Error no. 1:

Here test is your MySQL user. This is solved by creating a MySQL user with root rights and allowing it to access MySQL via any IP.

Restart your MySQL.

Now use this user in your docker-compose and Django app. Solved 👍.

Possible Error no. 2:

It means MySQL is listening to only the localhost. To solve this you need to change mysqld.cnf

Comment the line

Restart your MySQL

Solved 👍.

Still, have any doubts or errors popping out? Write to me, I’ll try my best.

Add a comment

Related posts:

Cleaning after construction by Netcorp Ltd.

After months of planning and countless hours of hard work, your building project is finally complete. However, the work is not yet finished. Once the dust has settled, it’s time to transform your…

Housing Forecast for Next Year Shows Similar Buyer Conditions Continuing into 2022

2021 has been a very challenging year for homebuyers around the country. Home prices are reaching incredible highs. Homes are selling for well over listing prices. Numbers of transactions are at…

Soaring Above Body Image Anxieties

I had been pole dancing for five years when I was confronted with this question. My studio had arranged for a Vogue-worthy professional photoshoot so its mainstay dancers could be immortalized on…