Dockerfile.j2 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. {% if skip_init %}
  2. FROM {{ base_image }}
  3. {% else %}
  4. # ================================================================
  5. # START: Build Runtime Image from Scratch
  6. # ================================================================
  7. FROM {{ base_image }}
  8. {% if 'ubuntu' in base_image and (base_image.endswith(':latest') or base_image.endswith(':24.04')) %}
  9. {% set LIBGL_MESA = 'libgl1' %}
  10. {% else %}
  11. {% set LIBGL_MESA = 'libgl1-mesa-glx' %}
  12. {% endif %}
  13. # Install necessary packages and clean up in one layer
  14. RUN apt-get update && \
  15. apt-get install -y wget sudo apt-utils {{ LIBGL_MESA }} libasound2-plugins git && \
  16. apt-get clean && \
  17. rm -rf /var/lib/apt/lists/*
  18. # Remove UID 1000 if it's called pn--this fixes the nikolaik image for ubuntu users
  19. RUN if getent passwd 1000 | grep -q pn; then userdel pn; fi
  20. # Create necessary directories
  21. RUN mkdir -p /openhands && \
  22. mkdir -p /openhands/logs && \
  23. mkdir -p /openhands/poetry
  24. # Directory containing subdirectories for virtual environment.
  25. ENV POETRY_VIRTUALENVS_PATH=/openhands/poetry
  26. RUN if [ ! -d /openhands/miniforge3 ]; then \
  27. wget --progress=bar:force -O Miniforge3.sh "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh" && \
  28. bash Miniforge3.sh -b -p /openhands/miniforge3 && \
  29. rm Miniforge3.sh && \
  30. chmod -R g+w /openhands/miniforge3 && \
  31. bash -c ". /openhands/miniforge3/etc/profile.d/conda.sh && conda config --set changeps1 False && conda config --append channels conda-forge"; \
  32. fi
  33. # Install Python and Poetry
  34. RUN /openhands/miniforge3/bin/mamba install conda-forge::poetry python=3.11 -y
  35. # ================================================================
  36. # END: Build Runtime Image from Scratch
  37. # ================================================================
  38. {% endif %}
  39. # ================================================================
  40. # START: Copy Project and Install/Update Dependencies
  41. # ================================================================
  42. RUN if [ -d /openhands/code ]; then rm -rf /openhands/code; fi
  43. COPY ./code /openhands/code
  44. # Below RUN command sets up the Python environment using Poetry,
  45. # installs project dependencies, and configures the container
  46. # for OpenHands development.
  47. # It creates and activates a virtual environment, installs necessary
  48. # tools like Playwright, sets up environment variables, and configures
  49. # the bash environment to ensure the correct Python interpreter and
  50. # virtual environment are used by default.
  51. WORKDIR /openhands/code
  52. RUN \
  53. # Configure Poetry and create virtual environment
  54. /openhands/miniforge3/bin/mamba run -n base poetry config virtualenvs.path /openhands/poetry && \
  55. /openhands/miniforge3/bin/mamba run -n base poetry env use python3.11 && \
  56. # Install project dependencies
  57. /openhands/miniforge3/bin/mamba run -n base poetry install --only main,runtime --no-interaction --no-root && \
  58. # Update and install additional tools
  59. apt-get update && \
  60. /openhands/miniforge3/bin/mamba run -n base poetry run pip install playwright && \
  61. /openhands/miniforge3/bin/mamba run -n base poetry run playwright install --with-deps chromium && \
  62. # Set environment variables
  63. echo "OH_INTERPRETER_PATH=$(/openhands/miniforge3/bin/mamba run -n base poetry run python -c "import sys; print(sys.executable)")" >> /etc/environment && \
  64. # Install extra dependencies if specified
  65. {{ extra_deps }} {% if extra_deps %} && {% endif %} \
  66. # Clear caches
  67. /openhands/miniforge3/bin/mamba run -n base poetry cache clear --all . && \
  68. # Set permissions
  69. {% if not skip_init %}chmod -R g+rws /openhands/poetry && {% endif %} \
  70. mkdir -p /openhands/workspace && chmod -R g+rws,o+rw /openhands/workspace && \
  71. # Clean up
  72. apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
  73. /openhands/miniforge3/bin/mamba clean --all
  74. # ================================================================
  75. # END: Copy Project and Install/Update Dependencies
  76. # ================================================================