liaoch commited on
Commit
a7acbbd
Β·
1 Parent(s): fac51fc

latest changes

Browse files
Files changed (3) hide show
  1. Dockerfile +98 -0
  2. README.md +34 -25
  3. makefile +2 -0
Dockerfile ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image, choosing one that includes Node.js
2
+ # Check Docker Hub for suitable tags (e.g., python:3.9-slim-bullseye might need Node installed)
3
+ # Or use a Node image and install Python. Let's try a Node base and add Python.
4
+ FROM node:18-slim
5
+
6
+ # Install Python, pip, and venv
7
+ # 'apt-get update && apt-get install -y --no-install-recommends' is standard practice
8
+ # 'rm -rf /var/lib/apt/lists/*' cleans up afterward to keep image size down
9
+ # Install Python, pip, venv, and Puppeteer dependencies
10
+ # See: https://pptr.dev/troubleshooting#running-puppeteer-on-debian
11
+ RUN apt-get update && \
12
+ apt-get install -y --no-install-recommends \
13
+ python3 \
14
+ python3-pip \
15
+ python3-venv \
16
+ # Puppeteer dependencies:
17
+ ca-certificates \
18
+ fonts-liberation \
19
+ libasound2 \
20
+ libatk-bridge2.0-0 \
21
+ libatk1.0-0 \
22
+ libcairo2 \
23
+ libcups2 \
24
+ libdbus-1-3 \
25
+ libexpat1 \
26
+ libfontconfig1 \
27
+ libgbm1 \
28
+ libgcc1 \
29
+ libglib2.0-0 \
30
+ libgtk-3-0 \
31
+ libnspr4 \
32
+ libnss3 \
33
+ libpango-1.0-0 \
34
+ libpangocairo-1.0-0 \
35
+ libstdc++6 \
36
+ libx11-6 \
37
+ libx11-xcb1 \
38
+ libxcb1 \
39
+ libxcomposite1 \
40
+ libxcursor1 \
41
+ libxdamage1 \
42
+ libxext6 \
43
+ libxfixes3 \
44
+ libxi6 \
45
+ libxrandr2 \
46
+ libxrender1 \
47
+ libxss1 \
48
+ libxtst6 \
49
+ lsb-release \
50
+ wget \
51
+ xdg-utils \
52
+ && \
53
+ apt-get clean && \
54
+ rm -rf /var/lib/apt/lists/*
55
+
56
+ # Set the working directory in the container
57
+ WORKDIR /app
58
+
59
+ # Install mermaid-cli globally
60
+ # Use --unsafe-perm if needed for permissions during global install
61
+ RUN npm install -g @mermaid-js/mermaid-cli --unsafe-perm=true
62
+
63
+ # Copy the requirements file first to leverage Docker cache
64
+ COPY requirements.txt ./
65
+
66
+ # Create a virtual environment and install Python dependencies
67
+ # This isolates Python packages within the container, similar to local setup
68
+ RUN python3 -m venv /app/venv
69
+ # Activate venv for the RUN command and install packages
70
+ RUN . /app/venv/bin/activate && pip install --no-cache-dir -r requirements.txt
71
+
72
+ # Copy the rest of the application code into the container
73
+ COPY . .
74
+
75
+ # Make port 5001 available to the world outside this container
76
+ # This should match the port Flask runs on in app.py (or Gunicorn config)
77
+ EXPOSE 5001
78
+
79
+ # Define environment variables
80
+ # IMPORTANT: Set a strong FLASK_SECRET_KEY when running the container!
81
+ # Using a placeholder here for demonstration.
82
+ ENV FLASK_APP=app.py
83
+ ENV FLASK_RUN_HOST=0.0.0.0
84
+ ENV FLASK_RUN_PORT=5001
85
+ ENV FLASK_SECRET_KEY="replace_this_in_docker_run_with_a_real_secret"
86
+ # Add venv's bin to the PATH for subsequent commands (like CMD)
87
+ ENV PATH="/app/venv/bin:$PATH"
88
+
89
+ # Override base image entrypoint so CMD executes directly
90
+ ENTRYPOINT []
91
+
92
+ # Run the application using Gunicorn when the container launches
93
+ # Use the full path to gunicorn within the virtual environment
94
+ # Bind to 0.0.0.0 to accept connections from outside the container
95
+ # Use the port defined by FLASK_RUN_PORT
96
+ # The number of workers (e.g., --workers 3) can be adjusted based on server resources
97
+ # Use JSON form with the absolute path to gunicorn in the venv to avoid PATH issues
98
+ CMD ["/app/venv/bin/gunicorn", "--workers", "3", "--bind", "0.0.0.0:5001", "app:app"]
README.md CHANGED
@@ -35,46 +35,55 @@ This Flask application provides a web interface for rendering diagrams from [Mer
35
  β”œβ”€β”€ requirements.txt # Python dependencies (Flask)
36
  β”œβ”€β”€ templates/
37
  β”‚ └── index.html # HTML template for the web interface
38
- β”œβ”€β”€ venv/ # Python virtual environment (created locally/on server)
39
  └── README.md # This file
40
  ```
41
 
42
  ## Prerequisites
43
 
44
- ### Local Machine (for testing)
45
 
46
- * Python 3 & pip
47
- * Node.js & npm
48
- * `@mermaid-js/mermaid-cli` installed globally (`npm install -g @mermaid-js/mermaid-cli`)
49
 
50
- ### Azure Ubuntu VM (for deployment)
51
 
52
- * An Azure Virtual Machine running Ubuntu (e.g., Ubuntu 20.04 LTS or later).
53
- * SSH access to the VM.
54
- * Ability to configure Network Security Groups (NSGs) in Azure portal.
55
- * `sudo` privileges on the VM.
56
-
57
- ## Local Testing
58
-
59
- 1. **Clone/Download:** Get the application files (`app.py`, `mermaid_renderer.py`, `requirements.txt`, `templates/`).
60
- 2. **Install Prerequisites:** Ensure Python 3, pip, Node.js, npm, and `@mermaid-js/mermaid-cli` are installed locally.
61
- 3. **Create Virtual Environment:**
62
  ```bash
63
- cd /path/to/project/directory
64
- python3 -m venv venv
65
- source venv/bin/activate
66
  ```
67
- 4. **Install Dependencies:**
 
68
  ```bash
69
- pip install -r requirements.txt
 
70
  ```
71
- 5. **Run Development Server:**
72
  ```bash
73
- python app.py
 
 
 
 
 
 
 
 
74
  ```
75
- 6. **Access:** Open your browser to `http://127.0.0.1:5001` (or the port specified in `app.py`).
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- ## Deployment to Azure Ubuntu VM
78
 
79
  These steps guide you through deploying the application using Gunicorn and Nginx.
80
 
 
35
  β”œβ”€β”€ requirements.txt # Python dependencies (Flask)
36
  β”œβ”€β”€ templates/
37
  β”‚ └── index.html # HTML template for the web interface
38
+ β”œβ”€β”€ Dockerfile # Instructions to build the Docker image
39
  └── README.md # This file
40
  ```
41
 
42
  ## Prerequisites
43
 
44
+ * [Docker](https://docs.docker.com/get-docker/) installed on your local machine or deployment server.
45
 
46
+ ## Deployment with Docker
 
 
47
 
48
+ This is the recommended way to run the application, as it bundles all dependencies.
49
 
50
+ 1. **Clone/Download:** Get the application files (`app.py`, `mermaid_renderer.py`, `requirements.txt`, `templates/`, `Dockerfile`).
51
+ 2. **Generate Secret Key:** Create a strong secret key for Flask sessions. You can generate one using:
 
 
 
 
 
 
 
 
52
  ```bash
53
+ python -c 'import secrets; print(secrets.token_hex(16))'
 
 
54
  ```
55
+ Keep this key safe and use it in the next step.
56
+ 3. **Build the Docker Image:** Navigate to the project directory in your terminal and run:
57
  ```bash
58
+ # Replace 'mermaid-renderer-app' with your desired image name/tag
59
+ docker build -t mermaid-renderer-app .
60
  ```
61
+ 4. **Run the Docker Container:**
62
  ```bash
63
+ # Replace 'YOUR_GENERATED_SECRET_KEY' with the key from step 2.
64
+ # -d: Run in detached mode (background)
65
+ # -p 80:5001: Map port 80 on the host to port 5001 in the container
66
+ # --name mermaid-app: Assign a name to the container for easier management
67
+ # -e FLASK_SECRET_KEY=...: Set the environment variable inside the container
68
+ # mermaid-renderer-app: The name of the image to run
69
+ docker run -d -p 80:5001 --name mermaid-app \
70
+ -e FLASK_SECRET_KEY='YOUR_GENERATED_SECRET_KEY' \
71
+ mermaid-renderer-app
72
  ```
73
+ *Note:* If port 80 is already in use on your host, choose a different host port (e.g., `-p 8080:5001`).
74
+
75
+ 5. **Access the Application:** Open your web browser and navigate to `http://localhost` (or `http://your_server_ip` if deploying remotely). If you used a different host port, include it (e.g., `http://localhost:8080`).
76
+
77
+ ### Managing the Container
78
+
79
+ * **View logs:** `docker logs mermaid-app`
80
+ * **Stop:** `docker stop mermaid-app`
81
+ * **Start:** `docker start mermaid-app`
82
+ * **Remove (after stopping):** `docker rm mermaid-app`
83
+
84
+ ---
85
 
86
+ # Deployment to Azure Ubuntu VM
87
 
88
  These steps guide you through deploying the application using Gunicorn and Nginx.
89
 
makefile ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ all:
2
+ python3 app.py