Skip to main content

IIEC Docker Project Documentation

IIEC Docker Project

My Docker Project is based on setup of Django web application by using nginx as a webserver , mysql as a database and PhpMyAdmin.

Prerequisites

Before you begin this setup, ensure the following is installed to your system:

  • Python 3.7 or greater,
  • Python Pip, the package manager,
  • Docker,
  • Git and a GitHub account.

INTRODUCTION



About Docker

Docker is the software development and devops tool everyone wants to use for their projects. It’s a platform that allows you to easily create isolated environments (aka Containers) that can run specific pieces of software. In other words, it provides a lightweight mechanism to “virtualize” environments in a host computer.

This is a very powerful tool for development since it simplifies the process of configuring and setting up environments or switching between completely different projects in the same development box. But, most importantly, it allows developers to consistently reproduce production environments and avoid problems due to configuration differences.

Particularly in Django there are some common errors that can arise with the default settings configuration and their differences with a typical production deployment. When using SQLite as a development database, which lacks DDL constraints, you can encounter some migrations failing in production even if you were able to run them perfectly on your local environment. Another common source of problems can be the difference between serving static files using the runserver command and the existence of a Web server with a reverse proxy in production. Also, if you use Memcached in production you might not be able to reproduce invalidation errors, and so on.

ABOUT NGINX

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

NGINX as a Web Server

The goal behind NGINX was to create the fastest web server around, and maintaining that excellence is still a central goal of the project. NGINX consistently beats Apache and other servers in benchmarks measuring web server performance. Since the original release of NGINX however, websites have expanded from simple HTML pages to dynamic, multifaceted content. NGINX has grown along with it and now supports all the components of the modern Web, including WebSocket, HTTP/2, and streaming of multiple video formats (HDS, HLS, RTMP, and others).

ABOUT MySQL
 
MySQL is a freely available open source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL).
SQL is the most popular language for adding, accessing and managing content in a database. It is most noted for its quick processing, proven reliability, ease and flexibility of use. MySQL is an essential part of almost every open source PHP application. Good examples for PHP & MySQL-based scripts are WordPress, Joomla, Magento and Drupal.
One of the most important things about using MySQL is to have a MySQL specialized host. Here are some of the things SiteGround can offer:
  • We have long experience in providing technical support for MySQL-based web sites. Thanks to it our servers are perfectly optimized to offer the best overall performance for most MySQL applications.
  • We offer a lot of free MySQL tools including CMS systems, forums, galleries, blogs, shopping carts and more.
  • We support MySQL 5 and we provide unlimited MySQL databases on all our hosting plans.
ABOUT Django
 
Django is a free and open source web application framework written in Python. A framework is nothing more than a collection of modules that make development easier. They are grouped together, and allow you to create applications or websites from an existing source, instead of from scratch.This is how websites - even simple ones designed by a single person - can still include advanced functionality like authentication support, management and admin panels, contact forms, comment boxes, file upload support, and more. In other words, if you were creating a website from scratch you would need to develop these components yourself. By using a framework instead, these components are already built, you just need to configure them properly to match your site.
The official project site describes Django as "a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source."


PhpMyAdmin 


phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySQL over the Web. phpMyAdmin supports a wide range of operations on MySQL and MariaDB. Frequently used operations (managing databases, tables, columns, relations, indexes, users, permissions, etc) can be performed via the user interface, while you still have the ability to directly execute any SQL statement.

phpMyAdmin comes with a wide range of documentation and users are welcome to update our wiki pages to share ideas and howtos for various operations. The phpMyAdmin team will try to help you if you face any problem; you can use a variety of support channels to get help.
phpMyAdmin is also very deeply documented in a book written by one of the developers – Mastering phpMyAdmin for Effective MySQL Management, which is available in English and Spanish.
To ease usage to a wide range of people, phpMyAdmin is being translated into 72 languages and supports both LTR and RTL languages.
phpMyAdmin is a mature project with a stable and flexible code base; you can find out more about the project and its history and the awards it earned. When the project turned 15, we published a celebration page.
The phpMyAdmin project is a member of Software Freedom Conservancy. SFC is a not-for-profit organization that helps promote, improve, develop, and defend Free, Libre, and Open Source Software (FLOSS) projects.

Using Docker for development

we are going do the process of creating a Django application from scratch as setting up two of the most common external services you would use in production; MySQL and NGINX.

Create a Django project

First we create an empty virtual environment and initialize our Django project. I’m using virtualenv wrapper.

1. ** Creating Virtual Environment **
python3.7 -m virtualenv djangoproject
2. ** To Activate the virtual environment use **
source djangoproject/bin/activate

3. ** Then Go to my github project page **
Clone this repo using git clone https://github.com/Moiz-Ali-Moomin/docker-django-project.git command.

4 ** Now For Making the Process more easy and efficient i have 
created a python script which will help you to setup the django docker project very easily this a TUI program which i have learned about from IIEC Rise python classes ** 

The name of the file is setupserver.py

use the below command to give executable permission to this script
1. chmod +x setupserver.py
2. You can also use python3 setupserver.py to execute it

You should follow all the steps one by one there are 10 options provided in this script you have to follow all the steps from option 1 to 10 to successfully setup this project

Press 1 to run Docker-compose up for building the Docker images
Press 2 to create a new project 
Press 3 to make migration 
Press 4 to create admin account
Press 5 to edit settings.py file of your django project 
Press 6 to collect static 
Press 7 to connect to MySQl server 
Press 8 to run python django-server 
Press 9 to run docker-compose down to shut-down docker process 
Press 10 to exit

Choose Below options to perfom tasks:


After executing the setupserver.py the above options will come up

OPTION 1
This will run the docker-compose up command for you and will download all the docker images i.e python3.7 image, nginx image and mysql 5.7 image and build these images into a single image file


OPTION 2
This will help you to create a django website project

OPTION 3 
This will help you to make project migrations in django

OPTION 4 
This will create a admin account inside django project

OPTION 5
This is a very important step you have to paste the following code inside setting.py file in django project to connect mysql database to django

If an internal server error occurs use the IP address of the mysql container using the command

docker inspect `CONTAINER_ID` | grep IP

# replace your default DATABASES section for this one
DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql', 
    'NAME': 'mydb',
    'USER': 'user',
    'PASSWORD': 'password',
    'HOST': 'db', # Or an IP Address that your DB is hosted on if internal server error occurs
    'PORT': '3306',
  }
}
 
OPTION 6
This will collect all the static files like css, JS , images used in django 

For doing this you have to open the setting.py file with option 5
and at the last of the page paste this line

STATIC_URL = '/static/'
STATIC_ROOT = '/static' 

then again choose this option again

OPTION 7
This options is optional you can connect to mysql database using this option and check if the database is propely configured


OPTION 8
This will run the django server for you and you can go check the website is running using http://localhost

OPTION 9
This will stop all the docker processes using docker-compose down command

OPTION 10
This will exit from the menu  

UPDATE:
I have also added PhpMyAdmin in this project to acces the PhpMyAdmin Panel use port 8082

NOTE:
If the Internal server error is showing when you try to access the website then remove the images and rebuild again

My Github code Link 


 email :- moiz.7152@gmail.com
RISE_2020.68.23.5

Comments