Skip to content

Docker: Setup Python venv in Base image for utilities #2748

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

Merged
merged 1 commit into from
Apr 4, 2025
Merged

Conversation

VietND96
Copy link
Member

@VietND96 VietND96 commented Apr 4, 2025

User description

Thanks for contributing to the Docker-Selenium project!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines, applied for this repository.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Fixes #2721

Motivation and Context

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Bug fix, Enhancement


Description

  • Added Python virtual environment setup in base image for utilities.

  • Updated dependencies and supervisor installation process.

  • Adjusted entry point scripts to activate virtual environment if present.

  • Removed redundant Python-related installations in specific Dockerfiles.


Changes walkthrough 📝

Relevant files
Enhancement
entry_point.sh
Activate Python virtual environment in entry point             

Base/entry_point.sh

  • Added logic to activate Python virtual environment if detected.
  • Ensured supervisor starts within the virtual environment context.
  • +7/-1     
    entry_point.sh
    Activate Python virtual environment in video entry point 

    Video/entry_point.sh

  • Added logic to activate Python virtual environment if detected.
  • Ensured supervisor starts within the virtual environment context.
  • +7/-1     
    Dockerfile
    Add Python virtual environment and update dependencies     

    Base/Dockerfile

  • Added Python installation and virtual environment setup.
  • Updated supervisor installation to use virtual environment.
  • Removed unused Netty dependencies.
  • Updated OpenTelemetry and gRPC versions.
  • +25/-7   
    Dockerfile
    Use Python virtual environment for websockify installation

    NodeBase/Dockerfile

  • Updated websockify installation to use Python virtual environment.
  • Ensured compatibility with virtual environment setup.
  • +1/-1     
    Dockerfile
    Simplify Python dependencies in video Dockerfile                 

    Video/Dockerfile

  • Removed redundant Python pip installation.
  • Simplified dependencies to align with virtual environment setup.
  • +0/-2     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
    Copy link

    qodo-merge-pro bot commented Apr 4, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    2721 - PR Code Verified

    Compliant requirements:

    • Fix Python error in Chrome node when updating from 4.24.0-20240907 to 4.29.0-20250303
    • Address ModuleNotFoundError for '_distutils_hack' and 'pkg_resources'
    • Ensure supervisor starts correctly without Python errors

    Requires further human verification:

    • Fix the issue in the Docker image for Selenium Grid - needs verification that the changes actually resolve the issue in a real deployment

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Missing command separator

    There appears to be a missing '&&' between the Python venv creation and the echo command that adds the activation to bashrc, which could cause the build to fail.

    RUN python3 -m venv $VENV_PATH \
        echo "source $VENV_PATH/bin/activate" >> /etc/bash.bashrc
    Redundant flag

    The --break-system-packages flag is used when installing supervisor, but this is unnecessary since the installation is happening within a virtual environment.

    && $VENV_PATH/bin/python3 -m pip install --break-system-packages . \
    && rm -rf /tmp/supervisor.zip /tmp/supervisor-main

    Copy link

    qodo-merge-pro bot commented Apr 4, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Missing command separator

    The command to create the virtual environment and the echo command to update
    bash.bashrc are missing a line continuation character () or && between them,
    causing the echo command to run in the host shell rather than in the Docker
    build context.

    Base/Dockerfile [79-80]

    -RUN python3 -m venv $VENV_PATH \
    +RUN python3 -m venv $VENV_PATH && \
         echo "source $VENV_PATH/bin/activate" >> /etc/bash.bashrc
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    __

    Why: The missing command separator (&&) between the venv creation and echo command is a critical issue that would cause the echo command to execute on the host system rather than in the Docker build context, breaking the virtual environment setup.

    High
    General
    Use specific version

    Installing supervisor directly from the main branch is risky as it could
    introduce unexpected changes. Use a specific release tag or version instead of
    the main branch to ensure stability and reproducibility of builds.

    Base/Dockerfile [82-87]

     RUN $VENV_PATH/bin/python3 -m pip install --upgrade pip setuptools virtualenv psutil \
    -    && wget -q https://github.com/Supervisor/supervisor/archive/refs/heads/main.zip -O /tmp/supervisor.zip \
    +    && wget -q https://github.com/Supervisor/supervisor/archive/refs/tags/4.2.5.zip -O /tmp/supervisor.zip \
         && unzip /tmp/supervisor.zip -d /tmp \
    -    && cd /tmp/supervisor-main \
    +    && cd /tmp/supervisor-4.2.5 \
         && $VENV_PATH/bin/python3 -m pip install --break-system-packages . \
    -    && rm -rf /tmp/supervisor.zip /tmp/supervisor-main
    +    && rm -rf /tmp/supervisor.zip /tmp/supervisor-4.2.5
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: Installing supervisor from the main branch introduces potential instability as the code could change unexpectedly. Using a specific version tag ensures build reproducibility and system stability.

    Medium
    Validate directory exists

    The script should check if the virtual environment directory exists before
    attempting to activate it, as the VIRTUAL_ENV variable might be set but the
    directory could be missing, which would cause the script to fail.

    Base/entry_point.sh [15-19]

    -if [ -n "${VIRTUAL_ENV}" ]; then
    +if [ -n "${VIRTUAL_ENV}" ] && [ -d "${VIRTUAL_ENV}" ]; then
       echo "Virtual environment detected at ${VIRTUAL_ENV}, activating..."
       source ${VIRTUAL_ENV}/bin/activate
       python3 --version
     fi
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: Adding a check for the virtual environment directory existence prevents script failures when the VIRTUAL_ENV variable is set but the directory doesn't exist, improving script robustness and error handling.

    Medium
    • More

    Copy link

    qodo-merge-pro bot commented Apr 4, 2025

    CI Feedback 🧐

    (Feedback updated until commit 853608b)

    A test triggered by this PR failed. Here is an AI-generated analysis of the failure:

    Action: Rerun workflow when failure

    Failed stage: Authenticate GitHub CLI for PR [❌]

    Failure summary:

    The action failed because the GitHub CLI authentication failed with the error: "missing required
    scope 'read:org'". The token provided in the GH_CLI_TOKEN_PR environment variable does not have the
    necessary permissions to perform the required operations. Specifically, it's missing the 'read:org'
    scope which is needed for the GitHub CLI to function properly in this workflow.

    Relevant error logs:
    1:  ##[group]Operating System
    2:  Ubuntu
    ...
    
    22:  Issues: write
    23:  Metadata: read
    24:  Models: read
    25:  Packages: write
    26:  Pages: write
    27:  PullRequests: write
    28:  RepositoryProjects: write
    29:  SecurityEvents: write
    30:  Statuses: write
    31:  ##[endgroup]
    32:  Secret source: Actions
    33:  Prepare workflow directory
    34:  Prepare all required actions
    35:  Getting action download info
    36:  Download action repository 'actions/checkout@main' (SHA:85e6279cec87321a52edac9c87bce653a07cf6c2)
    37:  Complete job name: Rerun workflow when failure
    38:  ##[group]Run actions/checkout@main
    ...
    
    42:  ssh-strict: true
    43:  ssh-user: git
    44:  persist-credentials: true
    45:  clean: true
    46:  sparse-checkout-cone-mode: true
    47:  fetch-depth: 1
    48:  fetch-tags: false
    49:  show-progress: true
    50:  lfs: false
    51:  submodules: false
    52:  set-safe-directory: true
    53:  env:
    54:  GH_CLI_TOKEN: ***
    55:  GH_CLI_TOKEN_PR: ***
    56:  RUN_ID: 14256057748
    57:  RERUN_FAILED_ONLY: true
    58:  RUN_ATTEMPT: 1
    ...
    
    113:  Or undo this operation with:
    114:  git switch -
    115:  Turn off this advice by setting config variable advice.detachedHead to false
    116:  HEAD is now at f4f11aa Merge 853608bcb5793116026452363a5049da859d1513 into 1cca6ba8ef0d5ad1026a3c894a57164dfc275b38
    117:  ##[endgroup]
    118:  [command]/usr/bin/git log -1 --format=%H
    119:  f4f11aabf36eb405747a9c6062c3d4fcdab7aff9
    120:  ##[group]Run sudo apt update
    121:  �[36;1msudo apt update�[0m
    122:  �[36;1msudo apt install gh�[0m
    123:  shell: /usr/bin/bash -e {0}
    124:  env:
    125:  GH_CLI_TOKEN: ***
    126:  GH_CLI_TOKEN_PR: ***
    127:  RUN_ID: 14256057748
    128:  RERUN_FAILED_ONLY: true
    129:  RUN_ATTEMPT: 1
    ...
    
    168:  Reading state information...
    169:  118 packages can be upgraded. Run 'apt list --upgradable' to see them.
    170:  WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
    171:  Reading package lists...
    172:  Building dependency tree...
    173:  Reading state information...
    174:  gh is already the newest version (2.69.0).
    175:  0 upgraded, 0 newly installed, 0 to remove and 118 not upgraded.
    176:  ##[group]Run echo "$GH_CLI_TOKEN_PR" | gh auth login --with-token
    177:  �[36;1mecho "$GH_CLI_TOKEN_PR" | gh auth login --with-token�[0m
    178:  shell: /usr/bin/bash -e {0}
    179:  env:
    180:  GH_CLI_TOKEN: ***
    181:  GH_CLI_TOKEN_PR: ***
    182:  RUN_ID: 14256057748
    183:  RERUN_FAILED_ONLY: true
    184:  RUN_ATTEMPT: 1
    185:  ##[endgroup]
    186:  error validating token: missing required scope 'read:org'
    187:  ##[error]Process completed with exit code 1.
    188:  Post job cleanup.
    

    @VietND96 VietND96 merged commit 5c0402b into trunk Apr 4, 2025
    25 of 27 checks passed
    @VietND96 VietND96 deleted the python-venv branch April 4, 2025 02:57
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    [🐛 Bug]: Chrome Node - Python error
    1 participant