Day 17 : Docker Project for DevOps Engineers.

Day 17 : Docker Project for DevOps Engineers.

Docker Project For DevOps Engineers

ยท

5 min read

๐Ÿ“„What is 'Dockerfile' ?

Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run.

To create these containers, developers use something called a Dockerfile. A Dockerfile is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.

Task

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

  • Build the image using the Dockerfile and run the container

  • Verify that the application is working as expected by accessing it in a web browser

  • Push the image to a public or private repository (e.g. Docker Hub )

Django streamlines web development, but managing environments can become tricky. Docker offers a solution by packaging your application with its dependencies into a portable container. This blog post will guide you through creating a simple Django web application, containerizing it with Docker, and deploying it to a t2.micro EC2 instance on AWS.

Prerequisites:

AWS Account:

Sign up for a free tier account at AWS website.

Basic Linux Knowledge:

This guide assumes familiarity with the Linux terminal.

1. Setting Up Your AWS EC2 Instance:

Launch an EC2 Instance:

  • Log in to the AWS Management Console and navigate to the EC2 service.

  • Choose an Amazon Machine Image (AMI) like Ubuntu. Select the free tier eligible t2.micro instance type.

  • Configure security groups to allow inbound SSH access on port 22 from your source IP and HTTP traffic on port 80 or 8000 (optional, depending on your application).

  • Launch the instance.

Connect to Your Instance:

  • Once the instance is running, retrieve the public DNS name from the EC2 console.

  • Use an SSH client like PuTTY to connect to your instance using the public DNS name or Public IP as the hostname and your key pair credentials.

Update and Secure Your Instance:

  • Connect to your instance and update the package lists:
sudo apt update
  • Install Docker for Ubuntu:
sudo apt install docker
  • Verify Docker installation:
docker run hello-world

2. Creating a Django Project (on your local machine):

  • Create a Django project: Use django-admin startproject my-django-app to create a new Django project directory named my-django-app.

  • Develop the web application: Create your Django application within the project and develop your views and templates.

3. Building the Dockerfile (on your local machine):

Create a file named Dockerfile in the root directory of your Django project (my-django-app). Paste the following content into the Dockerfile:

# Use a lightweight Python base image
FROM python:3.11-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy the application code to the working directory
COPY . .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Expose the port where Django listens (default 8000)
EXPOSE 8000

# Set environment variables (optional)
ENV DJANGO_SETTINGS_MODULE=my_django_app.settings

# Run migrations and start the development server
CMD [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]

Explanation of Dockerfile Commands:

  • FROM python:3.11-alpine: Specifies the base image for our container, using the official Python 3.11 image with Alpine Linux for a smaller size.

  • WORKDIR /app: Sets the working directory within the container to /app.

  • COPY . .: Copies all files from the current directory (.) into the working directory (/app) of the container, ensuring your Django project code gets included.

  • RUN pip install --no-cache-dir -r requirements.txt: Installs Python dependencies listed in your requirements.txt file. The --no-cache-dir option avoids downloading packages if they already exist in the cache layer, improving build efficiency.

  • EXPOSE 8000: Informs Docker that the application listens on port 8000 inside the container.

  • ENV DJANGO_SETTINGS_MODULE=my-django-app.settings (Optional): Sets the environment variable DJANGO_SETTINGS_MODULE to point to your Django project's settings file. This is necessary for Django to function correctly within the container.

  • CMD [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]: Specifies the command to run when the container starts, which in this case is starting the Django development server.

4. Transferring the Project and Building the Image (on your EC2 instance):

Transfer the Project:

  • Use a tool like FileZilla to securely transfer your Django project directory (my-django-app) from your local machine to the EC2 instance.

Build the Docker Image:

  • Navigate to the directory containing your transferred project on the EC2 instance (e.g., /home/ubuntu/my-django-app).

  • Build the Docker image using the following command:

docker build -t my-django-app .

5. Running the Container on the EC2 Instance:

  • Run the following command to start a container from the built image:
docker run -d -p 80:8000 my-django-app
  • -d: Runs the container in detached mode.

  • -p 80:8000: Maps port 80 on the EC2 instance (host) to port 8000 inside the container. This allows you to access the application through a web browser.

  • my-django-app: Replace this with the name you used while building the image.

6. Verifying the Application:

  • Open a web browser on another machine (or a different terminal window on your EC2 instance) and navigate to the public DNS name or IP address of your EC2 instance followed by port 80 (e.g., http://<public_ip>:80/).

  • If everything works correctly, you should see your Django application's default welcome page or the output based on your development.

And you are done.. this is how you use docker for deploying a Django Application.


โœ‰Endcard:

๐ŸŽ‰ Thank you for joining me on this insightful journey into the world of DevOps!

โค If you found this blog helpful and informative, don't forget to give it a like!

๐Ÿ”„ Share this valuable knowledge with your friends and colleagues, so they can also benefit from understanding the power of DevOps!

๐Ÿ‘‰ Stay updated with my latest posts and never miss out on exciting content! Click that Follow button to join and stay in the loop!

Follow me on LinkedIn -->abdallah-qamar๐Ÿ‘”

Stay tuned for Day 18...๐Ÿ‘‹

ย