DEV Community

Shreya Nalawade
Shreya Nalawade

Posted on

Introduction to Docker and dockerizing a Django Application

Hola, in this article, I will talk about Docker and its Architecture as well as dockerizing a Django Application.

What exactly is Docker?

Docker is an open platform for developing, shipping, and running applications in a containerized environment also known as docker containers. Docker is lightweight and fast. Docker containers are portable way to package and run applications that enable developers to package their applications with all the required dependencies and configurations in a single package that can be easily moved between any environment. Instead of running an entire separate operating system (which is a massive overhead), Docker runs containers, which use the same host operating system, and only virtualize at a software level.

Docker Architecture

Docker has a Client-Server design, with three primary components: Docker Client, Docker Host, and Docker Registry.

Architecture

Docker Daemon - The Docker daemon runs on the host operating system. It is in charge of running containers to manage Docker services. The Docker daemon communicates with other daemons. It provides Docker objects such as images, containers, networking, and storage.

  • Docker Client

Docker client uses commands and REST APIs to communicate with the Docker Daemon (Server). Docker Client uses CLI to run the following commands -

 1. docker build - The docker build command builds Docker 
  images from a Dockerfile.

 2. docker pull - Download an image from a registry/Docker Hub

 3. docker run  - Create and run a new container from an image
Enter fullscreen mode Exit fullscreen mode
  • Docker Host

Docker Host is used to offer an environment in which applications can be executed and run. The docker daemon, images, containers, networks, and storage are all included.

  • Docker registry

Docker Registry manages and stores the Docker images. Docker Hub is a hosted repository service registry provided by Docker for finding and sharing container images


Difference between Images and Containers



Images are one of the two core building blocks. Images are blueprints / templates for containers. They are read-only and contain the application as well as the necessary application environment (operating system, runtimes, tools,...). Images do not run themselves, instead, they can be executed as containers.

Containers are running instances of Images. When you create a container (via docker run), a thin read-write layer is added on top of the Image. Multiple Containers can therefore be started based on one and the same Image. So it's Containers which are in the end executed - both in development and production. For more info visit this page.


Key Commands



docker build –t NAME:TAG : Build an image based on a Dockerfile
docker run --name NAME --rm –d IMAGE : Run a container based on a remote or local Image
docker push REPOSITORY/NAME:TAG : Share (push) an Image to a Registry(default: DockerHub)
docker pull REPOSITORY/NAME:TAG : Fetch (pull) an Image from a Registry(default: DockerHub)


Building a Django Application

Run the code below on your terminal to create Django Project.
$ django-admin startproject docker
Then Run :

$ cd docker
$ python manage.py runserver

Go to http://127.0.0.1:8000/

Image description
you’ve just created a Django site...Now create Django App.

$ python manage.py startapp dockerpractice

It will contain the following Files

admin.py 
apps.py 
models.py 
tests.py 
views.py
Enter fullscreen mode Exit fullscreen mode

Create a templates folder which has dockertut.html file in app.

<h1>Django Docker App</h1>
Enter fullscreen mode Exit fullscreen mode

In views.py in dockerpractice app, add the html file.

from django.shortcuts import render

def dockertut(request):
    return render(request, 'dockertut.html', {})
Enter fullscreen mode Exit fullscreen mode

Add urls.py file and add the following code.

from django.urls import path
from dockertut import views

urlpatterns = [
    path('', views.dockertut, name='dockertut'),
]
Enter fullscreen mode Exit fullscreen mode

Include your app in the django project

from django.contrib import admin
from django.urls import path, include     

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('dockerpractice.urls')),   
]
Enter fullscreen mode Exit fullscreen mode

Go to http://127.0.0.1:8000/

Image description

Run pip freeze > requirements.txt

create a dockerfile.

FROM python:3

ENV PYTHONUNBUFFERED=1

WORKDIR /code

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["python","manage.py","runserver","0.0.0.0:8000"]
Enter fullscreen mode Exit fullscreen mode

Commands

FROM(base image)
COPY(copy files from local to the container)
ENV (environment variable)
RUN (any arbitrary shell command)
EXPOSE (open port from container to virtual network)
CMD (command to run when the container starts)
WORKDIR (Create a dir where all the files will be copied and used.)


Make Docker-compose.yml file.

version: "3.9"

services:
  django:
    image: django-docker:0.0.1
    build: .
    ports:
      - "8000:8000"
Enter fullscreen mode Exit fullscreen mode

In .dockerignore file add

.git
venv
Enter fullscreen mode Exit fullscreen mode

In your terminal enter docker compose up --build to build and run the image. Congrats! You have successfully dockerize Django App.

Now go to the localhost http://127.0.0.1:8000/

Image description

Top comments (3)

Collapse
 
praise002 profile image
Praise Idowu

Thanks

Collapse
 
shreya111111 profile image
Shreya Nalawade

I am glad u liked it

Collapse
 
sreekeshiyer profile image
Sreekesh Iyer

Nicely written😄