porting_an_lxc_to_docker
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| porting_an_lxc_to_docker [2024/08/11 03:54] – [1. Setting Up Dependencies] oso | porting_an_lxc_to_docker [2024/10/17 21:42] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 37: | Line 37: | ||
| The process of containerizing the Flask application involved several key steps, including setting up the necessary dependencies, | The process of containerizing the Flask application involved several key steps, including setting up the necessary dependencies, | ||
| - | ====== | + | ====== Using Clean Docker Images for Python Apps and MariaDB ====== |
| When transitioning from LXC containers to Docker, it's often more efficient and cleaner to use base Docker images for your applications rather than attempting to convert existing LXC containers. This approach offers several advantages: | When transitioning from LXC containers to Docker, it's often more efficient and cleaner to use base Docker images for your applications rather than attempting to convert existing LXC containers. This approach offers several advantages: | ||
| Line 47: | Line 47: | ||
| ===== General Outline for Container Creation ===== | ===== General Outline for Container Creation ===== | ||
| - | Here’s a general outline of the steps to create Docker containers for a Python application | + | Here’s a general outline of the steps to create Docker containers for a Python application |
| - | ==== 1. Create Dockerfile for Python App ==== | + | ==== 1. Setting Up Dependencies |
| - | First, create | + | Before containerizing the application, it was crucial to define the required dependencies in a `requirements.txt` file. The Flask application used the following dependencies: |
| - | * Create a `Dockerfile`: | ||
| < | < | ||
| + | Flask==2.1.2 | ||
| + | mariadb==1.1.4 | ||
| + | </ | ||
| + | |||
| + | These versions ensured compatibility and stability, avoiding issues related to breaking changes in more recent versions. | ||
| + | |||
| + | ==== 2. Creating the Dockerfile ==== | ||
| + | The next step was to create a `Dockerfile` to define the environment for the Flask application. The Dockerfile included the following instructions: | ||
| + | |||
| + | <code dockerfile> | ||
| FROM python: | FROM python: | ||
| + | |||
| + | # Install MariaDB Connector/C library and other dependencies | ||
| + | RUN apt-get update && apt-get install -y \ | ||
| + | libmariadb3 \ | ||
| + | libmariadb-dev \ | ||
| + | gcc \ | ||
| + | && apt-get clean \ | ||
| + | && rm -rf / | ||
| WORKDIR /app | WORKDIR /app | ||
| Line 63: | Line 80: | ||
| COPY . . | COPY . . | ||
| - | CMD [" | + | CMD [" |
| </ | </ | ||
| - | | + | **Explanation**: |
| - | < | + | * `FROM python: |
| - | docker build -t my-python-app . | + | * **Explanation of MariaDB Connector/C and Dependencies** |
| + | < | ||
| + | # Install MariaDB Connector/C library and other dependencies | ||
| + | RUN apt-get update && apt-get install | ||
| + | libmariadb3 \ | ||
| + | libmariadb-dev \ | ||
| + | gcc \ | ||
| + | && apt-get clean \ | ||
| + | && rm -rf / | ||
| </ | </ | ||
| + | * **`RUN apt-get update && apt-get install -y \`**: | ||
| + | - This command updates the package list on the Debian-based slim image and installs the specified packages. | ||
| + | * **`libmariadb3`**: | ||
| + | - **What it is**: This is the shared library for MariaDB Connector/ | ||
| + | - **Why it’s needed**: If your Flask app interacts with a MariaDB database, this library allows it to establish that connection. | ||
| + | * **`libmariadb-dev`**: | ||
| + | - **What it is**: This is the development package for MariaDB Connector/ | ||
| + | - **Why it’s needed**: If you have Python packages in `requirements.txt` that need to compile C extensions (for example, `mysqlclient`), | ||
| + | * **`gcc`**: | ||
| + | - **What it is**: The GNU Compiler Collection, a compiler system that supports various programming languages. | ||
| + | - **Why it’s needed**: This is required to compile any C extensions or Python packages that need compilation, | ||
| + | * **`&& | ||
| + | - **What it does**: This cleans up the package lists and removes any cached data from `apt-get`, which reduces the size of the Docker image by removing unnecessary files. | ||
| + | * `WORKDIR /app`: Sets the working directory inside the container. | ||
| + | * `COPY requirements.txt requirements.txt`: | ||
| + | * `RUN pip install -r requirements.txt`: | ||
| + | * `COPY . .`: Copies all the application files into the container. | ||
| - | ==== 2. Create Dockerfile for MariaDB (optional) | + | ==== 3. Configuring Docker-Compose |
| - | Next, create | + | To orchestrate the Flask application in a multi-container environment, a `docker-compose.yml` file was created with the following content: |
| - | * Use the official MariaDB image in your `docker-compose.yml`: | ||
| < | < | ||
| version: ' | version: ' | ||
| services: | services: | ||
| - | db: | ||
| - | image: mariadb | ||
| - | restart: always | ||
| - | environment: | ||
| - | MYSQL_ROOT_PASSWORD: | ||
| - | MYSQL_DATABASE: | ||
| - | MYSQL_USER: user | ||
| - | MYSQL_PASSWORD: | ||
| - | |||
| app: | app: | ||
| - | build: . | + | |
| - | ports: | + | ports: |
| - | | + | - " |
| - | depends_on: | + | |
| - | | + | - FLASK_ENV=development |
| + | - API_TOKEN=bXktbG9uZy***LongAssBase64String***cmluZw== | ||
| + | - DB_HOST=api.facundoitest.space | ||
| + | - DB_PASSWORD=***myPassword*** | ||
| </ | </ | ||
| - | ==== 3. Configure Docker Compose | + | **Explanation**: |
| - | Use Docker Compose to manage both containers. Create a `docker-compose.yml` | + | * `services`: Defines the application service. |
| + | * `build: .`: Tells Docker Compose to build the image using the Dockerfile in the current directory. | ||
| + | * `ports: " | ||
| + | |||
| + | ==== 4. Building and Running the Container | ||
| + | After setting up the Dockerfile and `docker-compose.yml`, the following commands were used to build and run the Flask application in a Docker container: | ||
| - | * Example `docker-compose.yml`: | ||
| < | < | ||
| - | version: ' | + | docker-compose build |
| + | docker-compose up -d | ||
| + | </ | ||
| - | services: | + | **Explanation**: |
| - | | + | |
| - | build: . | + | * `docker-compose up -d`: Starts the container in detached mode. |
| - | ports: | + | |
| - | | + | |
| - | depends_on: | + | |
| - | - db | + | |
| - | db: | + | ==== 5. App code with environment variables ==== |
| - | image: mariadb | + | |
| - | restart: always | + | |
| - | environment: | + | |
| - | MYSQL_ROOT_PASSWORD: | + | |
| - | MYSQL_DATABASE: | + | |
| - | MYSQL_USER: user | + | |
| - | MYSQL_PASSWORD: | + | |
| - | </code> | + | |
| - | ==== 4. Deploy Containers ==== | + | <code python> |
| - | Deploy your containers using Docker Compose. | + | from flask import Flask, request, jsonify, abort, g |
| + | from datetime import datetime | ||
| + | import mariadb | ||
| + | import re | ||
| + | import os | ||
| - | * Run the containers: | + | # Get passwords and tokens from environment variables |
| - | < | + | API_TOKEN = os.environ.get(' |
| - | docker-compose up -d | + | DB_PASSWORD = os.environ.get(' |
| - | </ | + | DB_HOST = os.environ.get(' |
| - | ==== 5. Test and Verify ==== | + | app = Flask(__name__) |
| - | Ensure that both containers are running and communicating correctly. | + | |
| - | * Check container status: | + | def get_db(): |
| - | < | + | if ' |
| - | docker ps | + | g.db = mariadb.connect( |
| - | </ | + | host=DB_HOST, |
| + | port=3306, | ||
| + | user=" | ||
| + | password=DB_PASSWORD, | ||
| + | database=" | ||
| + | ) | ||
| + | | ||
| - | * Verify the application is accessible: | + | @app.before_request |
| - | | + | def before_request(): |
| + | | ||
| - | ===== Conclusion ===== | + | @app.teardown_request |
| + | def teardown_request(exception=None): | ||
| + | db = g.pop(' | ||
| + | if db is not None: | ||
| + | db.close() | ||
| - | Using clean Docker images for your Python applications and databases provides a more maintainable, scalable, and universally compatible solution compared to converting LXC containers. This approach aligns with modern best practices in containerization and microservices architecture. | + | @app.route('/ |
| + | def upload_file(): | ||
| + | db = get_db() | ||
| + | cursor = db.cursor() | ||
| + | # Check for Authorization header | ||
| + | if ' | ||
| + | abort(401) | ||
| - | ===== V2 ===== | + | # Check if the token is correct |
| + | token = request.headers[' | ||
| + | if token != API_TOKEN: | ||
| + | abort(403) | ||
| - | ==== 1. Setting Up Dependencies | + | data = request.get_json() |
| - | Before containerizing | + | if data is None: |
| + | return jsonify({' | ||
| + | |||
| + | # ---------- The application itself --------- | ||
| + | # Extract hostname from data | ||
| + | hostname | ||
| + | |||
| + | # Create a new table for this hostname if it doesn' | ||
| + | cursor.execute(f" | ||
| + | |||
| + | # Delete all rows from the table | ||
| + | cursor.execute(f" | ||
| + | |||
| + | for restorePoint in data[' | ||
| + | creationtime | ||
| + | |||
| + | # Check if creationtime contains Date | ||
| + | if " | ||
| + | # Extract the integer from the creationtime string | ||
| + | match = re.search(r' | ||
| + | if match: | ||
| + | creationtime | ||
| + | |||
| + | # Check the type and replace it if necessary | ||
| + | type = str(restorePoint[' | ||
| + | if str(restorePoint[' | ||
| + | type = ' | ||
| + | elif str(restorePoint[' | ||
| + | type = ' | ||
| + | elif str(restorePoint[' | ||
| + | type = ' | ||
| + | |||
| + | # Check the type and replace | ||
| + | if str(restorePoint[' | ||
| + | if str(restorePoint[' | ||
| + | result = ' | ||
| + | elif str(restorePoint[' | ||
| + | result = ' | ||
| + | elif str(restorePoint[' | ||
| + | result = ' | ||
| + | else: | ||
| + | result = restorePoint[' | ||
| + | |||
| + | query = f" | ||
| + | values = ( | ||
| + | creationtime, | ||
| + | restorePoint[' | ||
| + | type, | ||
| + | result | ||
| + | ) | ||
| + | cursor.execute(query, | ||
| + | |||
| + | db.commit() | ||
| + | |||
| + | return jsonify({' | ||
| + | |||
| + | if __name__ == ' | ||
| + | app.run(host=' | ||
| + | </ | ||
| + | |||
| + | ==== Troubleshooting MariaDB import ==== | ||
| < | < | ||
| - | Flask==2.1.2 | + | #5 11.31 |
| - | mariadb==1.1.4 | + | #5 11.31 |
| + | #5 11.31 This error typically indicates that MariaDB Connector/ | ||
| + | #5 11.31 must be preinstalled, | ||
| + | #5 11.31 If MariaDB Connector/C is not installed, see installation instructions | ||
| + | #5 11.31 If MariaDB Connector/C is installed, either set the environment variable | ||
| + | #5 11.31 | ||
| + | #5 11.31 ' | ||
| + | #5 11.31 | ||
| + | #5 11.31 [end of output] | ||
| </ | </ | ||
| - | These versions ensured compatibility and stability, avoiding issues related | + | The error you're seeing indicates that the `mariadb` Python package requires the MariaDB Connector/C library |
| - | ==== 2. Creating the Dockerfile | + | Here's how you can modify your Dockerfile to install |
| - | The next step was to create a `Dockerfile` to define | + | |
| - | < | + | === Updated Dockerfile === |
| + | |||
| + | < | ||
| FROM python: | FROM python: | ||
| + | |||
| + | # Install MariaDB Connector/C library and other dependencies | ||
| + | RUN apt-get update && apt-get install -y \ | ||
| + | libmariadb3 \ | ||
| + | libmariadb-dev \ | ||
| + | gcc \ | ||
| + | && apt-get clean \ | ||
| + | && rm -rf / | ||
| WORKDIR /app | WORKDIR /app | ||
| Line 170: | Line 300: | ||
| COPY . . | COPY . . | ||
| - | CMD ["gunicorn", "--bind", | + | CMD ["python", "main.py"] |
| </ | </ | ||
| - | **Explanation**: | + | === Explanation |
| - | * `FROM python: | + | |
| - | * `WORKDIR /app`: Sets the working directory inside the container. | + | |
| - | * `COPY requirements.txt requirements.txt`: | + | |
| - | * `RUN pip install -r requirements.txt`: | + | |
| - | * `COPY . .`: Copies all the application files into the container. | + | |
| - | * `CMD [" | + | |
| - | ==== 3. Configuring Docker-Compose ==== | + | * **Update and install dependencies**: |
| - | To orchestrate | + | * **Clean up**: The `apt-get clean && rm -rf / |
| - | < | + | Now, try building your Docker image again with the updated Dockerfile: |
| - | version: ' | + | < |
| + | docker-compose up --build | ||
| + | </ | ||
| - | services: | + | This should resolve the issue with the missing `mariadb_config` and allow the `mariadb` Python package to install correctly. |
| - | app: | + | |
| - | build: | + | |
| - | ports: | + | |
| - | - " | + | |
| - | environment: | + | |
| - | API_TOKEN: ${API_TOKEN} | + | |
| - | networks: | + | |
| - | - app-network | + | |
| - | networks: | + | ==== Troubleshooting Werkzeug ==== |
| - | app-network: | + | < |
| + | app-flask-app-1 | ||
| + | app-flask-app-1 exited with code 1 | ||
| </ | </ | ||
| - | **Explanation**: | + | The error you're encountering is due to an incompatibility between Flask and Werkzeug versions. Flask 2.1.2 may not be compatible with the latest version of Werkzeug. |
| - | * `services`: Defines the application service. | + | |
| - | * `build: .`: Tells Docker Compose | + | |
| - | * `ports: " | + | |
| - | * `environment: | + | |
| - | * `networks`: Creates and attaches | + | |
| - | ==== 4. Building and Running the Container | + | To fix this, you can pin the Werkzeug version to one that is compatible with Flask 2.1.2. Based on the error, pinning Werkzeug to version 2.0.3 should resolve the issue. |
| - | After setting up the Dockerfile and `docker-compose.yml`, | + | |
| + | === Updated requirements.txt === | ||
| < | < | ||
| - | docker-compose build | + | Flask==2.1.2 |
| - | docker-compose up -d | + | mariadb==1.1.4 |
| + | Werkzeug==2.0.3 | ||
| </ | </ | ||
| - | **Explanation**: | + | === Dockerfile |
| - | * `docker-compose build`: Builds the Docker image based on the instructions in the Dockerfile. | + | |
| - | * `docker-compose up -d`: Starts the container in detached mode. | + | |
| - | ==== 5. Troubleshooting and Final Adjustments ==== | + | <code Dockerfile> |
| - | During the initial deployment, the following issues were encountered: | + | FROM python:3.9-slim |
| - | * **Werkzeug Compatibility**: | + | |
| - | * **Missing Imports**: The application threw an error due to a missing `os` module import, which was quickly added to resolve the issue. | + | |
| - | By following these steps, the Flask application was successfully containerized | + | # Install MariaDB Connector/C library |
| + | RUN apt-get update && apt-get install -y \ | ||
| + | libmariadb3 \ | ||
| + | libmariadb-dev \ | ||
| + | gcc \ | ||
| + | && apt-get clean \ | ||
| + | && rm -rf / | ||
| + | WORKDIR /app | ||
| + | COPY requirements.txt requirements.txt | ||
| + | RUN pip install -r requirements.txt | ||
| + | COPY . . | ||
| + | |||
| + | CMD [" | ||
| + | </ | ||
| + | |||
| + | === Steps === | ||
| + | |||
| + | - **Update your `requirements.txt`** as shown above. | ||
| + | - **Rebuild the Docker image** with the updated `requirements.txt`: | ||
| + | |||
| + | docker-compose up --build | ||
| + | |||
| + | |||
| + | This should resolve the incompatibility issue between Flask and Werkzeug and allow your Flask app to start without errors. | ||
| ===== Conclusion ===== | ===== Conclusion ===== | ||
| Porting from LXC to Docker is not a trivial task due to the differences in how these containers are designed and managed. It's essential to review the services, configurations, | Porting from LXC to Docker is not a trivial task due to the differences in how these containers are designed and managed. It's essential to review the services, configurations, | ||
| Line 236: | Line 372: | ||
| * LXC documentation: | * LXC documentation: | ||
| + | https:// | ||
porting_an_lxc_to_docker.1723348484.txt.gz · Last modified: 2024/10/17 21:42 (external edit)
