Introduction to Docker and dockerizing a Django Application
Tags: docker, django, devops, containers
Published: 2023-06-12 15:18:17

Hola, in this article, I will talk about Docker and its Architecture as well as dockerizing a Django Application.
<h2>What exactly is Docker?</h2>
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.

<h2>Docker Architecture</h2>

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


![Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jun3spmn1wli43wa8ro.png)

**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
<br>
- 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

---

<h2>Difference between Images and Containers</h2>
<br>
**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.
<br> **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](https://www.geeksforgeeks.org/difference-between-docker-image-and-container/) this page. 

---

<h2>Key Commands</h2>
<br>
`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)

---
<h2>Building a Django Application</h2>

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/267lrloe421btpmg32b2.png)
you’ve just created a Django site...Now create Django App.

`$ python manage.py startapp dockerpractice`

It will contain the following Files

```__init__.py 
admin.py 
apps.py 
models.py 
tests.py 
views.py
```
Create a templates folder which has dockertut.html file in app.
```
<h1>Django Docker App</h1>
```
In views.py in dockerpractice app, add the html file.

```
from django.shortcuts import render

def dockertut(request):
    return render(request, 'dockertut.html', {})
```
Add urls.py file and add the following code.

```
from django.urls import path
from dockertut import views

urlpatterns = [
    path('', views.dockertut, name='dockertut'),
]
```
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')),   
]
```
Go to `http://127.0.0.1:8000/`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlrbd5xjmbbsfmt02uxo.png)

Run `pip freeze > requirements.txt`

<h4>create a dockerfile.</h4>

```
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"]
```
>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.)
<br>
Make Docker-compose.yml file.

```
version: "3.9"

services:
  django:
    image: django-docker:0.0.1
    build: .
    ports:
      - "8000:8000"
```
In .dockerignore file add

```
.git
venv
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gr11ct1yhflwc77npdih.png)