This content originally appeared on Level Up Coding - Medium and was authored by KokaTic
A Practical Guide Using a Real Django Python Web Application from Docker Hub
Introduction
Deploying applications efficiently and securely is crucial for modern software development. Leveraging cloud platforms can significantly streamline this process, providing both robustness and scalability. Azure App Service offers a powerful and versatile platform to deploy Docker containers, enabling developers to focus on building their applications without worrying about the underlying infrastructure.
In this comprehensive guide, we’ll walk through the process of deploying a Django Python web application using Docker on Azure App Service. We’ll cover the initial setup of your Azure account, creating and configuring your Docker environment, and finally, deploying and managing your Django application in the cloud. By the end of this guide, you’ll have a solid understanding of how to utilize Azure App Service for your containerized applications, ensuring they run efficiently, securely, and at scale.
Why Choose Azure App Service?
Azure App Service offers a streamlined platform for deploying web applications and APIs with several key benefits:
- Simplicity: Easy deployment options like Git, Docker, and integration with CI/CD pipelines.
- Scalability: Automatically scale up or out based on demand, ensuring performance during peak times.
- Security: Built-in security features, SSL support, and integration with Azure Active Directory for identity management.
- Global Availability: Multiple regions worldwide for deploying applications closer to users.
- Integration: Seamless integration with other Azure services like databases, storage, and monitoring tools.
- Cost Efficiency: Pay-as-you-go pricing model and managed infrastructure reduce operational costs.
Azure App Service is the go-to choice for developers seeking a reliable, scalable, and secure platform to deploy their applications efficiently.
Prerequisites
- An Azure account
- A Docker account
- Azure CLI installed on your local machine version 2.60+
- Docker installed locally version 4.24.1+ (needs to be running)
Deploying steps
Step 1: Prepare Your Django Application and Docker file
Create a basic Django application using the following commands:
# Create and navigate to a new directory
mkdir mydjangoapp
cd mydjangoapp
# Create a virtual environment
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
# Install Django
pip install django
# Start a new Django project
django-admin startproject myproject .
Inside the created folder create a:
- a Dockerfile with the following content:
# Use the official Python image from the Docker Hub
FROM python:3.8-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /mydjangoapp
# Install dependencies
COPY requirements.txt /mydjangoapp/
RUN pip install -r requirements.txt
# Copy project
COPY . /mydjangoapp/
# Run the application
CMD ["python", "manage.py", "runserver", "0.0.0.0:80"]
- a requirements.txt file to specify your dependencies:
Django>=3.0,<4.0
Step 2: Create, Build, and tag the image
1- Build the image
Let’s buid the image
docker build -t kt_django .
2- Push the image into DockerHub (assuming your Dockerfile is in the current directory):
docker tag kt_django:latest kokaticops/kt_django:latest
Step 3: Deploying in Azure
Create the necessary Azure resources using Azure CLI. You should login before starting the following steps via az login.
1- Login to Azure:
Open a terminal and login to your Azure account:
az login --use-device-code
Use your username & password to get logged in.
2- Create a Resource Group:
Create a resource group where your app service will be hosted:
az group create --name kokaticrg --location eastus
3- Login to Docker:
You need to log in to your Docker account in order to push your image to Docker Hub:
docker login
4- Create Azure plan:
To ensure your applications have the resources, scalability, security, and support, let’s create an Azure plan:
az appservice plan create --name kokaticps --resource-group kokaticrg --sku B1 --is-linux
5- Create azure app service:
az webapp create --resource-group kokaticrg --plan kokaticps --name myKtDjangoWebApp --deployment-container-image-name kokaticops/kt_django:latest
You can the get the URL in the returned JSON: “defaultHostName”
{
"availabilityState": "Normal",
"clientAffinityEnabled": true,
"clientCertEnabled": false,
"clientCertExclusionPaths": null,
"clientCertMode": "Required",
"cloningInfo": null,
"containerSize": 0,
"customDomainVerificationId": "<ID>",
"dailyMemoryTimeQuota": 0,
"daprConfig": null,
"defaultHostName": "myktdjangowebapp.azurewebsites.net", => URL to check
"enabled": true,
"enabledHostNames": [
6- Checking our deployement
myktdjangowebapp.scm.azurewebsites.net
That’s the kind of error we sometimes hope for. It indicates that the Docker Compose deployment was successful!
7- (Optional) Bonus
Don’t forget to clean ressources:
az group delete --name kokaticrg --yes --no-wait
Conclusion
Deploying a Docker container to Azure App Service allows you to leverage Azure’s platform for hosting and scaling your applications. Ensure you have Dockerized your application correctly, pushed it to a container registry, and followed these steps to deploy it to Azure App Service. Adjust configurations and settings based on your application’s requirements and Azure environment.
In our next article, we’ll explain how to deploy the same Docker image using Azure Kubernetes Service (AKS), providing even more flexibility and scalability for your containerized applications. Stay tuned!
Thank you for staying with us until the end. Please don’t hesitate to contact me if you encounter any issues with this practical guide. Your feedback and questions are valuable to us!
Follow me
Deploy Flexibly Your Docker Image with Azure App Service was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by KokaTic
KokaTic | Sciencx (2024-06-27T13:06:46+00:00) Deploy Flexibly Your Docker Image with Azure App Service. Retrieved from https://www.scien.cx/2024/06/27/deploy-flexibly-your-docker-image-with-azure-app-service/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.