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 04:15] – [Troubleshooting MariaDB import] 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 ==== | + | |
| - | First, create a `Dockerfile` for your Python application. Use a base image like Debian or Alpine Ubuntu. | + | |
| - | + | ||
| - | * Create a `Dockerfile`: | + | |
| - | < | + | |
| - | FROM python: | + | |
| - | + | ||
| - | WORKDIR /app | + | |
| - | + | ||
| - | COPY requirements.txt requirements.txt | + | |
| - | RUN pip install -r requirements.txt | + | |
| - | + | ||
| - | COPY . . | + | |
| - | + | ||
| - | CMD [" | + | |
| - | </ | + | |
| - | + | ||
| - | * Build the Docker image: | + | |
| - | < | + | |
| - | docker build -t my-python-app . | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 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`: | + | |
| - | < | + | |
| - | version: ' | + | |
| - | + | ||
| - | services: | + | |
| - | db: | + | |
| - | image: mariadb | + | |
| - | restart: always | + | |
| - | environment: | + | |
| - | MYSQL_ROOT_PASSWORD: | + | |
| - | MYSQL_DATABASE: | + | |
| - | MYSQL_USER: user | + | |
| - | MYSQL_PASSWORD: | + | |
| - | + | ||
| - | app: | + | |
| - | build: . | + | |
| - | ports: | + | |
| - | - " | + | |
| - | depends_on: | + | |
| - | - db | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 3. Configure Docker Compose ==== | + | |
| - | Use Docker Compose to manage both containers. Create a `docker-compose.yml` file. | + | |
| - | + | ||
| - | * Example `docker-compose.yml`: | + | |
| - | < | + | |
| - | version: ' | + | |
| - | + | ||
| - | services: | + | |
| - | app: | + | |
| - | build: . | + | |
| - | ports: | + | |
| - | - " | + | |
| - | depends_on: | + | |
| - | - db | + | |
| - | + | ||
| - | db: | + | |
| - | image: mariadb | + | |
| - | restart: always | + | |
| - | environment: | + | |
| - | MYSQL_ROOT_PASSWORD: | + | |
| - | MYSQL_DATABASE: | + | |
| - | MYSQL_USER: user | + | |
| - | MYSQL_PASSWORD: | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 4. Deploy Containers ==== | + | |
| - | Deploy your containers using Docker Compose. | + | |
| - | + | ||
| - | * Run the containers: | + | |
| - | < | + | |
| - | docker-compose up -d | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 5. Test and Verify ==== | + | |
| - | Ensure that both containers are running and communicating correctly. | + | |
| - | + | ||
| - | * Check container status: | + | |
| - | < | + | |
| - | docker ps | + | |
| - | </ | + | |
| - | + | ||
| - | * Verify the application is accessible: | + | |
| - | Open your browser and navigate to `http:// | + | |
| - | + | ||
| - | ===== Conclusion ===== | + | |
| - | + | ||
| - | Using clean Docker images for your Python applications and databases provides a more maintainable, | + | |
| - | + | ||
| - | + | ||
| - | ===== 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: | ||
| - | < | + | < |
| 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 175: | Line 85: | ||
| **Explanation**: | **Explanation**: | ||
| * `FROM python: | * `FROM python: | ||
| + | * **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 / | ||
| + | </ | ||
| + | * **`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. | * `WORKDIR /app`: Sets the working directory inside the container. | ||
| * `COPY requirements.txt requirements.txt`: | * `COPY requirements.txt requirements.txt`: | ||
| Line 217: | Line 150: | ||
| ==== 5. App code with environment variables ==== | ==== 5. App code with environment variables ==== | ||
| - | |||
| <code python> | <code python> | ||
| Line 371: | Line 303: | ||
| </ | </ | ||
| - | ### Explanation | + | === Explanation |
| * **Update and install dependencies**: | * **Update and install dependencies**: | ||
| * **Clean up**: The `apt-get clean && rm -rf / | * **Clean up**: The `apt-get clean && rm -rf / | ||
| Line 439: | Line 372: | ||
| * LXC documentation: | * LXC documentation: | ||
| + | https:// | ||
porting_an_lxc_to_docker.1723349736.txt.gz · Last modified: 2024/10/17 21:42 (external edit)
