Django Web Dev Diary Part - 2
Prequisite:
- Python 2.7.15
- Django 1.11.6
- gunicorn 19.9.0
- Docker
Let's Begin
Create a Directory with any name you want to give for me I created a directory with "www"
Create a file called "requirementx.txt" and add the following line and save it
1
2
|
Django==1.11.6
gunicorn==19.9.0
| cs |
*requirements.txt is used to clarify which version of libraries are used in this web development so that the other developer can easily configure their environment and carry out the development.
In working root directory ("www/") let's create our first Django project by typing following command
django-admiin startproject ekstrah
| cs |
When you completed the following command it will create a project with the following structure
.
+-- requirements.txt
+-- ekstrah
| +-- db.sqlite3
| +-- manage.py
| +-- ekstrah
| +--__init__.py (exclude .pyc files)
| +--settings.py
| +--urls.py
| +--wsgi.py
| cs |
If it looks like above or looks similar, it's good! It means you do not have any problem so far!!
let's get into "ekstrah" directory and run the following command to run the server
let's get into "ekstrah" directory and run the following command to run the server
python manage.py runserver 0.0.0.0:8000
| cs |
and it will show as follows on the terminal
Let's open up the browser and direct to "localhost:8000"
If the error doesn't show it means you are almost there to setting environments!!
Next what we are going to do is that we are going to use gunicorn. If you want to learn about gunicorn search on google or write on the comment.
create file call start.sh on the root directory of the project folder and add the following line
change ekstrah to your Django app name :D try You might have to change mod of the file since it won't be able to execute straight away. You can change the mod of the file by chmod +x start.sh
run start.sh file in the ekstrah folder by ."./start.sh" If it doesn't pop up any error you are good to go.
We are almost there, now we need to initialize docker file. Create "Dockerfile" without any extension on the root folder and add following lines.
Create another file called "docker-compose.yml" also on the root folder and add following lines.
If you completed all above the following your directory structure will look like this.
*Exclude the .vscode folder. It is created when you are setting up the environment on visual studio code.
When you finish go to your root directory of the project folder and type following command to dockerize and run on docker.
Let's open up the browser and direct to "localhost:8000"
If the error doesn't show it means you are almost there to setting environments!!
Next what we are going to do is that we are going to use gunicorn. If you want to learn about gunicorn search on google or write on the comment.
create file call start.sh on the root directory of the project folder and add the following line
#!/bin/bash
# Start Gunicorn processes
echo Starting Gunicorn.
exec gunicorn ekstrah.wsgi:application \
--bind 0.0.0.0:8000 \
--workers 3
| cs |
change ekstrah to your Django app name :D try You might have to change mod of the file since it won't be able to execute straight away. You can change the mod of the file by chmod +x start.sh
run start.sh file in the ekstrah folder by ."./start.sh" If it doesn't pop up any error you are good to go.
We are almost there, now we need to initialize docker file. Create "Dockerfile" without any extension on the root folder and add following lines.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# The first instruction is what image we want to base our container on
# We Use an official Python runtime as a parent image
FROM python:2.7.15
# The enviroment variable ensures that the python output is set straight
# to the terminal with out buffering it first
ENV PYTHONUNBUFFERED 1
# create root directory for our project in the container
RUN mkdir /ekstrah
# Set the working directory to /music_service
WORKDIR /ekstrah
# Copy the current directory contents into the container at /music_service
ADD . /ekstrah
# Install any needed packages specified in requirements.txt
RUN pip install -r ./requirements.txt
# change ekstrah to your django project name
| cs |
Create another file called "docker-compose.yml" also on the root folder and add following lines.
1
2
3
4
5
6
7
8
9
10
11
|
version: '3'
services:
web:
build: .
command: bash -c "cd ekstrah && ../start.sh"
container_name: ekstrah_www
volumes:
- .:/ekstrah
ports:
- "8000:8000"
| cs |
If you completed all above the following your directory structure will look like this.
*Exclude the .vscode folder. It is created when you are setting up the environment on visual studio code.
When you finish go to your root directory of the project folder and type following command to dockerize and run on docker.
docker-compose up
| cs |
Than will show something like image below
Go to localhost:8000 and check if its running or not. If there is no error like mine then you are ready to program Django!
If you have any questions about this please let me know!!
Next post, I will go through writing web application using django in much detail. Also, when I get time I will talk little bit about Docker and Kubernetes :D
Have fun!
Go to localhost:8000 and check if its running or not. If there is no error like mine then you are ready to program Django!
If you have any questions about this please let me know!!
Next post, I will go through writing web application using django in much detail. Also, when I get time I will talk little bit about Docker and Kubernetes :D
Have fun!
Comments
Post a Comment