6- Docker Compose: Nginx Django Postgresql Web Application Part1
Docker Compose is used for running multiple containers as a single service. Containers still will be isolated but they can interact with each other. You might think why we need to use docker compose instead of just docker.
Docker manages single containers, docker compose manages multi-container applications. Therefore, if you are using docker you have to pull every single image from docker registry, maintain each dockerfile and start each container seperately in the correct order because containers might depend on each other. Just imagine you deal with this everyday. On the other hand, docker compose lets us to maintain, configure, order and start multiple containers by using a single yaml file. Using docker for multi-container application can be very time consuming. Docker and Docker Compose are designed for different purposes. In short, if your application requires multiple containers, just use docker compose.
To verify that we have docker compose on our system run the following on our terminal:
docker compose version
We all know that, in general a web application in its simplest form has a backend, a frontend and a database. I am going to use docker compose to create a web application that uses nginx(frontend), django(backend) and postgresql(database). This article is written for Docker Compose and Django+Nginx+Postgresql specifically.
Creating The Django Project and App
Lets first create a Django Project on our host machine. If you have not installed python virtual environment on your host machine, then first run the command below to install it:
sudo apt install python3-virtualenv
Then create a folder on your desktop named "website" and enter that folder
mkdir website && cd website
I don't want to keep my virtual environment in the Django Project folder. I will create a virtual environment named "venv_website" in MyVEnvs folder. This is where I store all my virtual environments. Go ahead and activate the virtual environment.
virtualenv ~/Desktop/MyVEnvs/venv_website -p python3
source ~/Desktop/MyVEnvs/venv_website/bin/activate
Note that venv_website is in paranthesis and it means, the virtual environement is active. Whatever I install now, it will be installed into this particular virtual environment. So, I can have many different virtual environements and many different versions of packages which do not conflict with each other.
Let's install Django and start a Project named PrjWeb (dont forget the dot at the end) and run django server to verify everything is ok so far.
pip install django==4.1.4
django-admin startproject PrjWeb .
python manage.py runserver
Django runs at 127.0.0.1:8000, browse and test that address. Ignore the migration warning for now, we will take care of it.
If it works we can hit Ctrl + C on the terminal and stop the server now.
Create a new django app (you can use any name for your app, I used webapp)
django-admin startapp webapp
Add the new app to the end of INSTALLED_APPS dictionary in settings.py file
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webapp',
]
Our new app does not have a urls.py. So, create a file and name it "urls.py" file in websapp folder first.
Every new application’s urls.py should be included in the main urls.py like below. To do that first import include class to main urls.py. Then include the new app’s urls.py as a path in urlspattern like this.
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('webapp/', include('webapp.urls')),
path('', views.home, name='home'),
]
Create a file named "views.py" in PrjWeb folder (the same folder where main urls.py file resides)
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse("Hi, I the home page!")
Now we need to create a url, and a view in our websapp to test if everything runs correctly.
urls.py ( urls.py in webapp folder)
from django.urls import path
from . import views
urlpatterns = [
path('', views.webapp, name='webapp'),
]
Let's add a new view in our views.py file (in webapp folder)
from django.shortcuts import render
from django.http import HttpResponse
def webapp(request):
return HttpResponse("Hi, I am working just fine!")
I always extend the user class for my django applications. You can follow the steps (only step 1, 2, 3 and 4) in the below article and extend the user class in Django. Make sure you use webapp instead of moviesapp. Also DO NOT APPLY MIGRATION commands which explained in that article, only make the changes that is explained as step 1, 2, 3 and 4. MIGRATION will be done in the container via entry-point.
https://configland.com/index.php/django/209-how-to-extend-existing-user-model-in-django
Finally, deactivate the virtual environment with the following command and delete dbsqlite3 file
In part2, we are going to create docker compose configuration files.
- Hits: 253