porting_an_lxc_to_docker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
porting_an_lxc_to_docker [2024/08/11 04:10] – [5. App code with environment variables] osoporting_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, creating a Dockerfile, and configuring the `docker-compose.yml` file. The process of containerizing the Flask application involved several key steps, including setting up the necessary dependencies, creating a Dockerfile, and configuring the `docker-compose.yml` file.
  
-====== v1 - Using Clean Docker Images for Python Apps and MariaDB ======+====== 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 and a MariaDB database. +Here’s a general outline of the steps to create Docker containers for a Python application writing to a MariaDB database.
- +
-==== 1. Create Dockerfile for Python App ==== +
-First, create a `Dockerfile` for your Python application. Use a base image like Debian or Alpine Ubuntu. +
- +
-  * Create a `Dockerfile`: +
-<code> +
-FROM python:3.9-slim +
- +
-WORKDIR /app +
- +
-COPY requirements.txt requirements.txt +
-RUN pip install -r requirements.txt +
- +
-COPY . . +
- +
-CMD ["python", "app.py"+
-</code> +
- +
-  * Build the Docker image: +
-<code> +
-docker build -t my-python-app . +
-</code> +
- +
-==== 2. Create Dockerfile for MariaDB (optional) ==== +
-Next, create a `Dockerfile` for MariaDB or use the official MariaDB image. +
- +
-  * Use the official MariaDB image in your `docker-compose.yml`: +
-<code> +
-version: '3.1' +
- +
-services: +
-  db: +
- image: mariadb +
- restart: always +
- environment: +
-   MYSQL_ROOT_PASSWORD: example +
-   MYSQL_DATABASE: mydb +
-   MYSQL_USER: user +
-   MYSQL_PASSWORD: password +
- +
-  app: +
- build: . +
- ports: +
-   - "5000:5000" +
- depends_on: +
-   - db +
-</code> +
- +
-==== 3. Configure Docker Compose ==== +
-Use Docker Compose to manage both containers. Create a `docker-compose.yml` file. +
- +
-  * Example `docker-compose.yml`: +
-<code> +
-version: '3.1' +
- +
-services: +
-  app: +
- build: . +
- ports: +
-   - "5000:5000" +
- depends_on: +
-   - db +
- +
-  db: +
- image: mariadb +
- restart: always +
- environment: +
-   MYSQL_ROOT_PASSWORD: example +
-   MYSQL_DATABASE: mydb +
-   MYSQL_USER: user +
-   MYSQL_PASSWORD: password +
-</code> +
- +
-==== 4. Deploy Containers ==== +
-Deploy your containers using Docker Compose. +
- +
-  * Run the containers: +
-<code> +
-docker-compose up -d +
-</code> +
- +
-==== 5. Test and Verify ==== +
-Ensure that both containers are running and communicating correctly. +
- +
-  * Check container status: +
-<code> +
-docker ps +
-</code> +
- +
-  * Verify the application is accessible: +
-    Open your browser and navigate to `http://localhost:5000`. +
- +
-===== Conclusion ===== +
- +
-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. +
- +
- +
-===== V2 =====+
  
 ==== 1. Setting Up Dependencies ==== ==== 1. Setting Up Dependencies ====
Line 160: Line 62:
 The next step was to create a `Dockerfile` to define the environment for the Flask application. The Dockerfile included the following instructions: The next step was to create a `Dockerfile` to define the environment for the Flask application. The Dockerfile included the following instructions:
  
-<code>+<code dockerfile>
 FROM python:3.9-slim FROM python:3.9-slim
 +
 +# 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 /var/lib/apt/lists/*
  
 WORKDIR /app WORKDIR /app
Line 175: Line 85:
 **Explanation**: **Explanation**:
   * `FROM python:3.9-slim`: This uses a lightweight Python 3.9 image as the base.   * `FROM python:3.9-slim`: This uses a lightweight Python 3.9 image as the base.
 +  * **Explanation of MariaDB Connector/C and Dependencies**
 +<code dockerfile>
 +# 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 /var/lib/apt/lists/*
 +</code>
 +  * **`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/C, which is a lightweight client library to connect your Flask application to a MariaDB database.
 +    - **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/C. It includes the headers and static libraries needed for compiling programs that use MariaDB.
 +    - **Why it’s needed**: If you have Python packages in `requirements.txt` that need to compile C extensions (for example, `mysqlclient`), they require these development files to build correctly.
 +  * **`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, particularly those that depend on the MariaDB libraries mentioned above.
 +  * **`&& apt-get clean && rm -rf /var/lib/apt/lists/*`**: 
 +    - **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.   * `WORKDIR /app`: Sets the working directory inside the container.
   * `COPY requirements.txt requirements.txt`: Copies the `requirements.txt` file into the container.   * `COPY requirements.txt requirements.txt`: Copies the `requirements.txt` file into the container.
Line 217: Line 150:
  
 ==== 5. App code with environment variables ==== ==== 5. App code with environment variables ====
- 
  
 <code python> <code python>
Line 371: Line 303:
 </code> </code>
  
-### Explanation+=== Explanation === 
   * **Update and install dependencies**: The `RUN apt-get update && apt-get install -y` command installs the required MariaDB Connector/C library (`libmariadb3` and `libmariadb-dev`) and a C compiler (`gcc`).   * **Update and install dependencies**: The `RUN apt-get update && apt-get install -y` command installs the required MariaDB Connector/C library (`libmariadb3` and `libmariadb-dev`) and a C compiler (`gcc`).
   * **Clean up**: The `apt-get clean && rm -rf /var/lib/apt/lists/*` commands clean up the apt cache to reduce the image size.   * **Clean up**: The `apt-get clean && rm -rf /var/lib/apt/lists/*` commands clean up the apt cache to reduce the image size.
Line 381: Line 314:
  
 This should resolve the issue with the missing `mariadb_config` and allow the `mariadb` Python package to install correctly. This should resolve the issue with the missing `mariadb_config` and allow the `mariadb` Python package to install correctly.
 +
 +==== Troubleshooting Werkzeug ====
 +<code>
 +app-flask-app-1  | ImportError: cannot import name 'url_quote' from 'werkzeug.urls' (/usr/local/lib/python3.9/site-packages/werkzeug/urls.py)
 +app-flask-app-1 exited with code 1
 +</code>
 +
 +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.
 +
 +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.
 +
 +=== Updated requirements.txt ===
 +
 +<code>
 +Flask==2.1.2
 +mariadb==1.1.4
 +Werkzeug==2.0.3
 +</code>
 +
 +=== Dockerfile (unchanged from previous update) ===
 +
 +<code Dockerfile>
 +FROM python:3.9-slim
 +
 +# 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 /var/lib/apt/lists/*
 +
 +WORKDIR /app
 +
 +COPY requirements.txt requirements.txt
 +RUN pip install -r requirements.txt
 +
 +COPY . .
 +
 +CMD ["python", "main.py"]
 +</code>
 +
 +=== 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, and workflows involved before starting the migration to avoid unexpected issues and ensure that your applications run smoothly in Docker. 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, and workflows involved before starting the migration to avoid unexpected issues and ensure that your applications run smoothly in Docker.
Line 388: Line 372:
   * LXC documentation: https://linuxcontainers.org/lxc/introduction/   * LXC documentation: https://linuxcontainers.org/lxc/introduction/
  
 +https://docs.facundoitest.space/doku.php?id=crear_container_con_apps_en_python_para_aci
porting_an_lxc_to_docker.1723349441.txt.gz · Last modified: 2024/10/17 21:42 (external edit)