Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker optimised #1676

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

docker optimised #1676

wants to merge 1 commit into from

Conversation

sfahad1414
Copy link
Collaborator

@sfahad1414 sfahad1414 commented Dec 20, 2024

Summary by CodeRabbit

  • New Features

    • Updated Python version to 3.10.16 for enhanced performance.
    • Introduced environment variable PYENV_ROOT for better Python version management.
  • Improvements

    • Streamlined installation process for Python and related packages.
    • Updated installation commands to ensure correct Python environment usage.

Copy link

coderabbitai bot commented Dec 20, 2024

Walkthrough

The pull request focuses on updating the Dockerfile for improved Python version management and installation process. The changes primarily involve updating the Python version from 3.10.13 to 3.10.16, introducing pyenv for Python version control, and modifying the installation commands. The new approach streamlines the Python environment setup by using pyenv, updating environment variables, and ensuring a more consistent method of installing and using Python within the Docker container.

Changes

File Change Summary
docker/Dockerfile - Updated Python version from 3.10.13 to 3.10.16
- Added PYENV_ROOT environment variable
- Updated PATH to include pyenv
- Replaced manual Python installation with pyenv
- Modified Python package installation commands
- Updated file copying paths

Possibly related PRs

Poem

🐰 A Dockerfile's Pythonic Dance

Pyenv whispers, versions advance,
From 3.10.13 to 16's sweet glance,
Docker builds with newfound grace,
A rabbit's code, a nimble trace!

🐾 Hop, hop, deploy! 🚀


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (1)
docker/Dockerfile (1)

40-40: Consider multi-stage build and volume mounting for models

Training models during image build increases both build time and image size. This can impact CI/CD pipeline performance and container deployment.

Consider:

  1. Using multi-stage builds to separate the training environment from the runtime environment
  2. Moving model training to a separate process
  3. Using volumes to mount pre-trained models

Example multi-stage approach:

# Training stage
FROM amazonlinux:latest as builder
# ... (previous setup steps) ...
RUN python -m rasa train --data ${TEMPLATE_DIR_DEFAULT}/data \
    --config ${TEMPLATE_DIR_DEFAULT}/config.yml \
    --domain ${TEMPLATE_DIR_DEFAULT}/domain.yml \
    --out ${TEMPLATE_DIR_DEFAULT}/models

# Runtime stage
FROM amazonlinux:latest
# ... (copy only necessary files and models from builder) ...
VOLUME ["${TEMPLATE_DIR_DEFAULT}/models"]
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c529677 and 261ade3.

📒 Files selected for processing (1)
  • docker/Dockerfile (2 hunks)

Comment on lines +10 to +11
RUN yum install -y wget make gcc zlib-devel tar xz gzip openssl openssl-devel bzip2-devel sqlite-devel libffi-devel openblas-devel ca-certificates git

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize package installation and reduce image size

The current setup uses multiple RUN instructions and doesn't clean up package manager cache, leading to larger image size.

-RUN yum update -y
-RUN yum install -y wget make gcc zlib-devel tar xz gzip openssl openssl-devel bzip2-devel sqlite-devel libffi-devel openblas-devel ca-certificates git
+RUN yum update -y && \
+    yum install -y \
+        wget \
+        make \
+        gcc \
+        zlib-devel \
+        tar \
+        xz \
+        gzip \
+        openssl \
+        openssl-devel \
+        bzip2-devel \
+        sqlite-devel \
+        libffi-devel \
+        openblas-devel \
+        ca-certificates \
+        git && \
+    yum clean all && \
+    rm -rf /var/cache/yum
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
RUN yum install -y wget make gcc zlib-devel tar xz gzip openssl openssl-devel bzip2-devel sqlite-devel libffi-devel openblas-devel ca-certificates git
RUN yum update -y && \
yum install -y \
wget \
make \
gcc \
zlib-devel \
tar \
xz \
gzip \
openssl \
openssl-devel \
bzip2-devel \
sqlite-devel \
libffi-devel \
openblas-devel \
ca-certificates \
git && \
yum clean all && \
rm -rf /var/cache/yum

Comment on lines +3 to +5
ENV KAIRON_HOME=/app PYTHON_VERSION=3.10.16 TEMPLATE_DIR_DEFAULT=./template/use-cases/Hi-Hello-GPT RASA_TELEMETRY_ENABLED=false
ENV PYENV_ROOT=/root/.pyenv
ENV PATH=$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize Docker layers by combining ENV instructions

Multiple ENV instructions create unnecessary layers. Consider combining them for better optimization.

-ENV KAIRON_HOME=/app PYTHON_VERSION=3.10.16 TEMPLATE_DIR_DEFAULT=./template/use-cases/Hi-Hello-GPT RASA_TELEMETRY_ENABLED=false
-ENV PYENV_ROOT=/root/.pyenv
-ENV PATH=$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
+ENV KAIRON_HOME=/app \
+    PYTHON_VERSION=3.10.16 \
+    TEMPLATE_DIR_DEFAULT=./template/use-cases/Hi-Hello-GPT \
+    RASA_TELEMETRY_ENABLED=false \
+    PYENV_ROOT=/root/.pyenv \
+    PATH=$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ENV KAIRON_HOME=/app PYTHON_VERSION=3.10.16 TEMPLATE_DIR_DEFAULT=./template/use-cases/Hi-Hello-GPT RASA_TELEMETRY_ENABLED=false
ENV PYENV_ROOT=/root/.pyenv
ENV PATH=$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
ENV KAIRON_HOME=/app \
PYTHON_VERSION=3.10.16 \
TEMPLATE_DIR_DEFAULT=./template/use-cases/Hi-Hello-GPT \
RASA_TELEMETRY_ENABLED=false \
PYENV_ROOT=/root/.pyenv \
PATH=$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH

Comment on lines +12 to +20
RUN curl https://pyenv.run | bash
RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc && \
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc && \
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

RUN source ~/.zshrc && \
pyenv install ${PYTHON_VERSION} && \
pyenv global ${PYTHON_VERSION} && \
pyenv rehash && \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Security and optimization concerns in pyenv installation

Several issues need attention:

  1. Executing scripts directly from curl poses security risks
  2. Using .zshrc in amazonlinux (which typically uses bash)
  3. Multiple RUN instructions creating unnecessary layers

Consider this safer and more optimized approach:

-RUN curl https://pyenv.run | bash
-RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc && \
-    echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc && \
-    echo 'eval "$(pyenv init -)"' >> ~/.zshrc
-
-RUN source ~/.zshrc && \
-    pyenv install ${PYTHON_VERSION} && \
-    pyenv global ${PYTHON_VERSION} &&  \
-    pyenv rehash &&  \
+RUN set -ex && \
+    curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer -o pyenv-installer && \
+    chmod +x pyenv-installer && \
+    ./pyenv-installer && \
+    rm pyenv-installer && \
+    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc && \
+    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc && \
+    echo 'eval "$(pyenv init -)"' >> ~/.bashrc && \
+    . ~/.bashrc && \
+    pyenv install ${PYTHON_VERSION} && \
+    pyenv global ${PYTHON_VERSION} && \
+    pyenv rehash && \
     python -m pip install --upgrade pip && \
     mkdir data_generator training_data testing_data models /home/cache .rasa
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
RUN curl https://pyenv.run | bash
RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc && \
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc && \
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
RUN source ~/.zshrc && \
pyenv install ${PYTHON_VERSION} && \
pyenv global ${PYTHON_VERSION} && \
pyenv rehash && \
RUN set -ex && \
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer -o pyenv-installer && \
chmod +x pyenv-installer && \
./pyenv-installer && \
rm pyenv-installer && \
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc && \
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc && \
echo 'eval "$(pyenv init -)"' >> ~/.bashrc && \
. ~/.bashrc && \
pyenv install ${PYTHON_VERSION} && \
pyenv global ${PYTHON_VERSION} && \
pyenv rehash && \
python -m pip install --upgrade pip && \
mkdir data_generator training_data testing_data models /home/cache .rasa

Comment on lines +42 to +43
RUN cp kairon/shared/rule_policy.py /root/.pyenv/versions/${PYTHON_VERSION}/lib/python3.10/site-packages/rasa/core/policies/rule_policy.py
RUN cp kairon/shared/schemas/domain.yml /root/.pyenv/versions/${PYTHON_VERSION}/lib/python3.10/site-packages/rasa/shared/utils/schemas/domain.yml
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid direct modification of installed packages

Copying files directly into the Python packages directory is risky and can break during version updates. This approach bypasses package management and can lead to maintenance issues.

Consider these alternatives:

  1. Create proper patches and apply them during package installation
  2. Fork and maintain your own version of the packages
  3. Use Python's site-packages directory for custom overrides:
-RUN cp kairon/shared/rule_policy.py /root/.pyenv/versions/${PYTHON_VERSION}/lib/python3.10/site-packages/rasa/core/policies/rule_policy.py
-RUN cp kairon/shared/schemas/domain.yml /root/.pyenv/versions/${PYTHON_VERSION}/lib/python3.10/site-packages/rasa/shared/utils/schemas/domain.yml
+# Create a custom package directory
+RUN mkdir -p /app/custom_overrides/rasa/{core/policies,shared/utils/schemas}
+
+# Copy your custom files
+COPY kairon/shared/rule_policy.py /app/custom_overrides/rasa/core/policies/
+COPY kairon/shared/schemas/domain.yml /app/custom_overrides/rasa/shared/utils/schemas/
+
+# Add the custom directory to Python path
+ENV PYTHONPATH=/app/custom_overrides:$PYTHONPATH

Committable suggestion skipped: line range outside the PR's diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant