GitHub - atulkamble/dockerized-python-azure-vm: A step-by-step project to Dockerize a Python Flask application and deploy it on an Azure Virtual Machine, featuring Docker, Docker Compose, and automation scripts.
A step-by-step project to Dockerize a Python Flask application and deploy it on an Azure Virtual Machine, featuring Docker, Docker Compose, and automation scripts.
Here's a step-by-step guide to Dockerize a Python application on an Azure Virtual Machine (VM):
Prerequisites
-
Azure VM: Create an Azure VM (Ubuntu 20.04/22.04 recommended) with public IP.
-
Docker Installed: Install Docker on the VM.
sudo apt update sudo apt install -y docker.io sudo usermod -aG docker $USERLog out and log back in for group changes to take effect.
-
Docker Compose Installed:
sudo apt install -y docker-compose
Step 1: Write the Python Code
Create a simple Python application. Example: a Flask app.
app.py:
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, Azure Dockerized Python App!" if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
Step 2: Create a Requirements File
List Python dependencies in a requirements.txt file.
requirements.txt:
Step 3: Create a Dockerfile
Define the steps to containerize the Python application.
Dockerfile:
# Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container COPY . /app # Install the dependencies RUN pip install --no-cache-dir -r requirements.txt # Make port 5000 available to the outside world EXPOSE 5000 # Run the application CMD ["python", "app.py"]
Step 4: Build and Run Docker Container
-
Transfer Files to VM: Use SCP or Filezilla to transfer
app.py,requirements.txt, andDockerfileto your VM. -
SSH into VM:
ssh <your_username>@<vm_public_ip>
-
Build the Docker Image:
docker build -t flask-app . -
Run the Docker Container:
docker run -d -p 5000:5000 flask-app
-
Test the Application: Open your browser and go to
http://<vm_public_ip>:5000. You should see "Hello, Azure Dockerized Python App!".
Step 5: Optional - Use Docker Compose
If your project grows (e.g., with a database), use Docker Compose.
docker-compose.yml:
version: '3.8' services: web: build: . ports: - "5000:5000"
Run the application with:
Step 6: Automate Deployment
For automated deployment, create a Bash script or use CI/CD tools like GitHub Actions or Azure DevOps.
Let me know if you'd like further assistance with deploying this project!