Skip to content

Commit

Permalink
remove build part of the code and minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
renjith-digicat committed Oct 7, 2024
1 parent 98c3619 commit 43b4ebe
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 196 deletions.
11 changes: 4 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ FROM python:3.12-slim

# Install dependencies and Poetry
RUN apt-get update && \
apt-get install -y --fix-missing build-essential docker.io && \
apt-get install -y --fix-missing build-essential && \
pip install --no-cache-dir poetry && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Upgrade pip to the latest version
RUN pip install --upgrade pip

# Set the working directory in the container
WORKDIR /app

Expand All @@ -24,13 +21,13 @@ ENV POETRY_VIRTUALENVS_CREATE=false
COPY pyproject.toml poetry.lock ./

# Install only non-development dependencies
RUN poetry install --no-dev --no-root
RUN poetry install --only main --no-root

# Copy the rest of the application code into the container
COPY src ./src

# Add source directory to python path
ENV PYTHONPATH="${PYTHONPATH}:/app/src"

# Run the application
CMD ["poetry", "run", "python", "src/main.py"]
# Set default command
CMD ["bash"]
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package-mode = false
[tool.poetry.dependencies]
python = "^3.12"
mlflow = "^2.16.2"
docker = "^7.1.0"
pydantic-settings = "^2.5.2"


Expand Down
130 changes: 0 additions & 130 deletions src/build_mlflow_docker_image.py

This file was deleted.

1 change: 0 additions & 1 deletion src/deploy_kserve.py

This file was deleted.

71 changes: 71 additions & 0 deletions src/generate_mlflow_docker_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Generate Dockerfile to create deployable mlflow model base image."""

import argparse
import os

from mlflow.pyfunc.backend import PyFuncBackend

from src.utils import envs, set_mlflow_tracking_uri


def generate_docker_file(model_uri: str, out_dir: str = "./mlflow-dockerfile"):
"""Generate mlflow model Dockerfile for deployment."""
print("Generating Dockerfile using python api...")
# Create an instance of the PyFuncBackend class
backend = PyFuncBackend(config={}, env_manager="conda")
backend.generate_dockerfile(
model_uri=model_uri, output_dir=out_dir, enable_mlserver=True
)

print(f"MLFlow model Dockerfile generated in `./{out_dir}/`")


def update_dockerfile(dockerfile_dir: str = "./mlflow-dockerfile/"):
"""Add dependency installation instructions to the dockerfile."""
# Command to install dependencies
command_to_install_deps = (
"RUN apt-get -y update && apt-get install "
"-y --no-install-recommends gcc libc-dev\n"
)

# Insert the dependency installation line after the FROM line
dockerfile_path = os.path.join(dockerfile_dir, "Dockerfile")
with open(dockerfile_path, "r") as file:
lines = file.readlines()
for index, line in enumerate(lines):
if line.startswith("FROM"):
lines.insert(index + 1, command_to_install_deps)
break

# Update Dockerfile
with open(dockerfile_path, "w") as file:
file.writelines(lines)

print(f"Updated {dockerfile_path} with necessary dependencies.")


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_uri",
type=str,
required=True,
help="MLFlow model uri",
)

parser.add_argument(
"--out_dir", type=str, required=False, default="./mlflow-dockerfile"
)

args = parser.parse_args()

set_mlflow_tracking_uri(envs.mlflow_tracking_uri)

model_uri = args.model_uri

# Generate a docker file with artefacts and dependencies gathered
# from the mlflow remote registry
generate_docker_file(model_uri, out_dir=args.out_dir)

# Add missing dependencies to the generated dockerfile
update_dockerfile(dockerfile_dir=args.out_dir)
20 changes: 4 additions & 16 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"""Main entry point."""

from src.build_mlflow_docker_image import (build_from_dockerfile,
generate_docker_file,
modify_dockerfile)
from src.generate_mlflow_docker_image import (generate_docker_file,
update_dockerfile)
from src.get_mlflow_model_details import get_model_run_uri
from src.push_image_to_cr import push_image_to_docker_registry
from src.utils import envs, set_mlflow_tracking_uri

if __name__ == "__main__":

# Set the output directory for docker files and artefacts
out_dir = "./mlflow-dockerfile"
out_dir = envs.mlflow_docker_out_dir

# Set the mlflow tracking uri before getting the model details
set_mlflow_tracking_uri(envs.mlflow_tracking_uri)
Expand All @@ -26,14 +24,4 @@
generate_docker_file(model_uri, out_dir=out_dir)

# Add missing dependencies to the generated dockerfile
modify_dockerfile(dockerfile_dir=out_dir)

# Build docker image to be used as the base image for deployment
docker_image = build_from_dockerfile(
image_name=envs.mlflow_built_image_name,
docker_registry=envs.docker_registry,
image_tag=envs.mlflow_built_image_tag,
)

# Push the built image to local container registry
push_image_to_docker_registry(docker_image)
update_dockerfile(dockerfile_dir=out_dir)
40 changes: 0 additions & 40 deletions src/push_image_to_cr.py

This file was deleted.

1 change: 1 addition & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Envs(BaseSettings):
docker_registry: str = "localhost:5000"
mlflow_built_image_name: str = "mlflow_model"
mlflow_built_image_tag: str = "latest"
mlflow_docker_out_dir: str = "./mlflow-dockerfile"

class Config:
env_prefix = ""
Expand Down

0 comments on commit 43b4ebe

Please sign in to comment.