Web Application Deployment with Docker: A 
  Django-Todo-CICD Project

Web Application Deployment with Docker: A Django-Todo-CICD Project

What is Dockerfile

A Dockerfile is a text file that contains a set of instructions for building a Docker image. The Dockerfile describes how to create a custom Docker image that includes all the necessary dependencies and configurations for running an application. The Dockerfile contains commands that are executed in sequence to build the image. Each command in the Dockerfile creates a new layer in the image.

Dockerfile is used as the source code to build Docker images, and it is highly configurable, allowing developers to customize the environment for their application. Dockerfile is typically used with the docker build command, which takes the Dockerfile as input and produces a Docker image as output. Once the image is built, it can be used to start containers that run the application.

Dockerfiles are commonly used to define and automate the build process for complex applications that require specific dependencies, configuration, and environment variables. They are a powerful tool for building consistent and reproducible Docker images that can be easily shared and deployed across different environments.

Let's talk about what we insert in Dockerfile

  1. FROM: Specifies the base image that the new image will be built on top of. For example, you might use an official Node.js image as the base for an application that runs on Node.js.

  2. RUN: Executes a command in the image. This command is run during the image build process. For example, you might use the RUN command to install necessary packages or dependencies for your application.

  3. COPY: Copies files from the host machine to the image. For example, you might use the COPY command to copy the files for your application into the image.

  4. ENV: Sets an environment variable in the image. For example, you might use the ENV command to set a variable that holds the version of your application.

  5. EXPOSE: Specifies the ports that should be exposed on the container. For example, you might use the EXPOSE command to specify that port 8000 should be exposed on the container.

  6. CMD: Specifies the command that should be run when a container is created from the image. For example, you might use the CMD command to specify that your application should be started when the container is created.

Here’s an example of a simple Dockerfile that sets up a Django-Todo-CICD environment:

FROM python:3.9
RUN pip install django==3.2
COPY . .  
RUN python manage.py migrate
EXPOSE 8000
CMD ["python","manage.py","runserver","0.0.0.0:8000" ]
  • We start with the base image of Python 3.9.

  • Install the Django 3.2

  • Install the dependencies using pip.

  • Copy the entire source code of the django-todo-cicd application to the working directory.

  • Expose port 8000, which is the default port used by Django.

  • Define the command to run the application using sudo docker run -d -p 8000:8000 django-todo-app.

Task: Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

Clone the repository in your system using the git clone command, view the content of the repository & create a Dockerfile if not present or modify the present Dockerfile.

https://github.com/mizandevops/django-todo-cicd

Step 2:- Firstly need to create a separate directory to create a project so it will very easy to track all the changes we need to clone the repo by using the below command

git clone <Repo name>

Step 3:- Now we need to create Dockerfile to write the set of instructions to run the code

NOTE:- Here we have already a docker file that case no need to create a again

FROM python:3.9
RUN pip install django==3.2
COPY . .  
RUN python manage.py migrate
EXPOSE 8000
CMD ["python","manage.py","runserver","0.0.0.0:8000" ]

Step 4:- Now using the Docker file we can create an Docker image

docker build . -t <image_name>
# Ex:- docker build . -t django-todo-cicd

Step 5:- Now using the above docker image we can create a n number of docker containers

docker run -d -p 8000:8000 django-todo-cicd:latest

Step 6:- Now we need to search in the web browser along with the ipv4 address and port number

Note: Go to your ec2 instance security group and check whether the port 8000 is opened or not. If not, then add it to the inbound rules of the security group.

Step 7:- Now we need to push that image to the Docker Hub repo so everyone can use that same image. using the below url we can log in to Docker Hub

hub.docker.com

Step 8:- Now we need to log in to the docker hub from the Linux machine using docker hub conditionals

docker login

Step 9:- Now we need to tag that image before pushing it to the docker hub

docker tag django-todo-cicd mizanfirdausi/django-todo-cicd

Step 10:- Now we can push that image to the docker hub

docker push mizanfirdausi/django-todo-cicd

Step 11:- Now we can see the docker image in the docker hub