Table of contents
- Introduction
- Let's get started....
- Step 1: Create an AWS EC2 Instance π
- Step 2: Clone the Django Todo App from GitHub π
- Step 3: Create a Dockerfile for the Django Todo App π
- Step 4: Build the Docker Image and Run the Container ποΈ
- Step 5: Verify Application Functionality π©βπ»
- Step 6: Push the Image to a Repository βοΈ
- Step 7: Create a docker-compose file for the Django Todo App π
- Step 8:Run the docker-compose file ποΈ
Introduction
DevOps engineers play a pivotal role in enhancing the efficiency and reliability of software development and deployment processes. Docker, a containerization platform, has emerged as an indispensable tool in the DevOps toolkit. In this blog post, we will guide you through a Docker project tailored for DevOps engineers. This project involves creating a Dockerfile for a Django-based Todo application, building the Docker image, running the container, verifying the application's functionality, and pushing the image to a repository.
Let's get started....
Step 1: Create an AWS EC2 Instance π
Navigate to the AWS Management Console.
Launch an EC2 instance, choosing an Amazon Machine Image (AMI) based on your preference (e.g., Ubuntu).
Configure the instance, set up security groups to allow inbound traffic on ports 22 (SSH) and 8000 (Django application).
Launch the instance and download the private key.
Connect to the instance using SSH:
ssh -i /path/to/private-key.pem ec2-user@your-instance-ip
Step 2: Clone the Django Todo App from GitHub π
Once connected to your EC2 instance, clone your Django Todo application from GitHub:
# Clone the GitHub repository
git clone https://github.com/Kritika257/Django-Todo.git
# Navigate to the project directory
cd Django-Todo
Step 3: Create a Dockerfile for the Django Todo App π
FROM python:3.10-slim
WORKDIR /app
RUN pip install django==3.2
COPY . .
RUN python manage.py migrate
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
This Dockerfile sets up a Python environment, installs the application dependencies, and specifies the command to run the Django Todo application.
Step 4: Build the Docker Image and Run the Container ποΈ
Navigate to the directory containing your Dockerfile and application code in the terminal. Run the following commands:
# Build the Docker image
docker build -t django-app .
# Run the Docker container
docker run -d -p 8000:8000 <image-id>
These commands build the Docker image with the <image-id>
and run a container based on that image, mapping port 8000 from the container to the host.
Step 5: Verify Application Functionality π©βπ»
Open your web browser and navigate to <ipaddress>:8000
, where <ipaddress> is the appropriate IP of your remote server. You should see your application running.
This step verifies that the application is working correctly within the Docker container.
Step 6: Push the Image to a Repository βοΈ
To share your Docker image or deploy it to other environments, push it to a container registry. For example, let's use Docker Hub as the repository:
# Log in to Docker Hub (replace USERNAME with your Docker Hub username)
docker login -u USERNAME
# Tag the image with your Docker Hub username and repository name
docker tag django-app USERNAME/django-app
# Push the image to Docker Hub
docker push USERNAME/django-app
Now, your Docker image is available on Docker Hub and can be pulled by others for deployment.
So, now to make the deployment shorter, not that much lenthgy we are going to use docker compose.
Step 7: Create a docker-compose file for the Django Todo App π
version : "3"
services :
web :
image : kritikashaw/django-app
ports :
- "8000:8000"
Step 8:Run the docker-compose file ποΈ
We have to use two commands in this to run multiple containers.
# To up the container
docker-compose up
# To down the container
docker-compose down
Let's check it's up or not
Now, it's all done....
Cheers!!!
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)
Connect with me: LinkedIn