Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 19, 2021 05:25 pm GMT

Dockerzing a Python Django Web App on Ubuntu 21.04

Hey Developer!Today we're learn dockerize a django application on ubuntu It's worked all linux distro ...
This article covered a simple hello world django application In which we talk about Docker

prerequisite :

1.Ubuntu (any linux Distro)
2.Python Pip, the package manager
3.Python2 and Python3
4.Docker
5.Django

What is Docker
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

What is Use of Docker
When building web applications, you have probably reached a point where you want to run your application in a fashion that is closer to your production environment. Docker allows you to set up your application runtime in such a way that it runs in exactly the same manner as it will in production, on the same operating system, with the same environment variables, and any other configuration and setup you require.

Means,Docker facilitate to Developer to work with from another existing project own system Docker container holds the entire package of application with own resources. like below example.

Alt Text

Now we are going for Docker installation in Ubuntu for that follow official link .Docker Installation Link

Check Docker version in your system like $ docker --version if it show like this Docker version 20.10.7, build f0df350
mean docker installed your system

Create Django Project in Pip Environment

Let us, make a directory and install pipenv in that directory

$ mkdir docker-project && cd docker-project$ pipenv install django

Now we Activate our Environment .

$ pipenv shell

Now We are create a Django project and Django app..

$ django-admin startproject hello-project .$ cd hello-project$ python3 manage.py startapp pages$ python3 manage.py migrate

Now We are going to runserver for all set..

$ python3 manage.py runserver

It's look like this.

Alt Text

At Now we are configure file and render hello world on web page let get start..

  1. Change in setting.py:
# hello_project/settings.pyINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','pages.apps.PagesConfig', # new

2.Change in hello-project urls.py:

Now we can set the URL route for the pages app. Since we want our message to appear
on the homepage well use the empty string '' . Dont forget to add the include import
on the second line as well

# hello_project/urls.pyfrom django.contrib import adminfrom django.urls import path, include # newurlpatterns = [path('admin/', admin.site.urls),path('', include('pages.urls')), # new]
  1. Change in pages view.py:Rather than set up a template at this point we can just hardcode a message in ourview layer at pages/views.py which will output the string Hello, World!.
# pages/views.pyfrom django.http import HttpResponsedef home_page_view(request):    return HttpResponse('Hello, World!')

Note:We are create a file in pages (app directory) which name should be urls.py.

4.Change in page urls.py:
Within your text editor import path on the top line, add the home_page_view , and then
set its route to again be the empty string of '' . Note that we also provide an optional
name, home , for this route which is a best practice.

# pages/urls.pyfrom django.urls import pathfrom .views import home_page_viewurlpatterns = [path('', home_page_view, name='home')]

Here we are done for configure and runserver and see like this page.

$ python3 manage.py runserver

Alt Text

Notice: Now are exit the Django virtual environment for docker configuration for quit pipenv.
(docker-project)$ exit

Lets create our first Dockerfile to see all of this theory in action.

$ touch Dockerfile$ touch docker-compose.yml 
Note: Here Dockerfile name should be D in Capital letter. And Project file structure must be like following image.

Alt Text

# Docerfile#pull base imageFROM python:3.9# Set environment variablesENV PYTHONDONTWRITEBYTECODE 1ENV PYTHONUNBUFFERED 1# Set work directoryWORKDIR /code# Install dependenciesCOPY Pipfile Pipfile.lock /code/RUN pip install pipenv && pipenv install --system# Copy projectCOPY . /code/

Description:

Dockerfiles are read from top-to-bottom when an image is created. The first instruc-
tion must be the FROM command which lets us import a base image to use for our
image, in this case Python 3.9.
Then we use the ENV command to set two environment variables:
PYTHONUNBUFFERED ensures our console output looks familiar and is not buffered
by Docker, which we dont want
PYTHONDONTWRITEBYTECODE means Python will not try to write .pyc files which we
also do not desire

Next we use WORKDIR to set a default work directory path within our image called code
which is where we will store our code.

For our dependencies we are using Pipenv so we copy over both the Pipfile and
Pipfile.lock files into a /code/ directory in Docker and so on.....

Note:
Our image instructions are now done so lets build the image using the command
docker build . The period, . , indicates the current directory is where to execute the command.

Commad line$ docker build ................................................Sending build context to Docker daemon154.1kBStep 1/7 : FROM python:3.7...Step 7/7 : COPY . /code/---> a48b2acb1fccSuccessfully built a48b2acb1fcc

Moving on we now need to create a docker-compose.yml file to control how to run the
container that will be built based upon our Dockerfile image.

version: '3.7'services:     web:        build: .        command: python /code/manage.py runserver 0.0.0.0:8000        volumes:             - .:/code        ports:             - 8000:8000

The final step is to run our Docker container using the command docker-compose up .
This command will result in another long stream of output code on the command line.

$ docker-compose up........................................................Creating network "hello_default" with the default driverBuilding webStep 1/7 : FROM python:3.7...Creating hello_web_1 ... doneAttaching to hello_web_1web_1 | Performing system checks...web_1 |web_1 | System check identified no issues (0 silenced).web_1 | September 20, 2019 - 17:21:57web_1 | Django version 2.2.5, using settings 'hello_project.settings'web_1 | Starting development server at http://0.0.0.0:8000/web_1 | Quit the server with CONTROL-C.

To confirm it actually worked, go back to http://127.0.0.1:8000/ in your web browser.
Refresh the page and the Hello, World page should still appear. because this application are running locally your's system.

Stop the container with Control+c (press the Control and c button at the same
time) and additionally type docker-compose down . Docker containers take up a lot of
memory so its a good idea to stop them in this way when youre done using them.
Containers are meant to be stateless which is why we use volumes to copy our code
over locally where it can be saved.

$ docker-compose down.............................Removing hello_web_1 ... doneRemoving network hello_default

Great Developer's we are create Django application custom image and now we can upload this project image on Docker Hub

and fetch it and worked without any package installation error own your system locally.
Thank You!!


Original Link: https://dev.to/gautamankul/dickerzing-a-python-django-web-app-on-ubuntu-21-04-54o7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To