Dockerfile.j2 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. FROM {{ base_image }}
  2. # Shared environment variables (regardless of init or not)
  3. ENV POETRY_VIRTUALENVS_PATH=/openhands/poetry
  4. ENV MAMBA_ROOT_PREFIX=/openhands/micromamba
  5. {% if build_from_scratch %}
  6. # ================================================================
  7. # START: Build Runtime Image from Scratch
  8. # ================================================================
  9. # This is used in cases where the base image is something more generic like nikolaik/python-nodejs
  10. # rather than the current OpenHands release
  11. {% if 'ubuntu' in base_image and (base_image.endswith(':latest') or base_image.endswith(':24.04')) %}
  12. {% set LIBGL_MESA = 'libgl1' %}
  13. {% else %}
  14. {% set LIBGL_MESA = 'libgl1-mesa-glx' %}
  15. {% endif %}
  16. # Install necessary packages and clean up in one layer
  17. RUN apt-get update && \
  18. apt-get install -y wget curl sudo apt-utils {{ LIBGL_MESA }} libasound2-plugins git && \
  19. apt-get clean && \
  20. rm -rf /var/lib/apt/lists/*
  21. # Remove UID 1000 if it's called pn--this fixes the nikolaik image for ubuntu users
  22. RUN if getent passwd 1000 | grep -q pn; then userdel pn; fi
  23. # Create necessary directories
  24. RUN mkdir -p /openhands && \
  25. mkdir -p /openhands/logs && \
  26. mkdir -p /openhands/poetry
  27. # Install micromamba
  28. RUN mkdir -p /openhands/micromamba/bin && \
  29. /bin/bash -c "PREFIX_LOCATION=/openhands/micromamba BIN_FOLDER=/openhands/micromamba/bin INIT_YES=no CONDA_FORGE_YES=yes $(curl -L https://micro.mamba.pm/install.sh)" && \
  30. /openhands/micromamba/bin/micromamba config remove channels defaults && \
  31. /openhands/micromamba/bin/micromamba config list
  32. # Create the openhands virtual environment and install poetry and python
  33. RUN /openhands/micromamba/bin/micromamba create -n openhands -y && \
  34. /openhands/micromamba/bin/micromamba install -n openhands -c conda-forge poetry python=3.12 -y
  35. # Create a clean openhands directory including only the pyproject.toml, poetry.lock and openhands/__init__.py
  36. RUN \
  37. if [ -d /openhands/code ]; then rm -rf /openhands/code; fi && \
  38. mkdir -p /openhands/code/openhands && \
  39. touch /openhands/code/openhands/__init__.py
  40. COPY ./code/pyproject.toml ./code/poetry.lock /openhands/code/
  41. # Install all dependencies
  42. WORKDIR /openhands/code
  43. RUN \
  44. /openhands/micromamba/bin/micromamba config set changeps1 False && \
  45. # Configure Poetry and create virtual environment
  46. /openhands/micromamba/bin/micromamba run -n openhands poetry config virtualenvs.path /openhands/poetry && \
  47. /openhands/micromamba/bin/micromamba run -n openhands poetry env use python3.12 && \
  48. # Install project dependencies
  49. /openhands/micromamba/bin/micromamba run -n openhands poetry install --only main,runtime --no-interaction --no-root && \
  50. # Update and install additional tools
  51. apt-get update && \
  52. /openhands/micromamba/bin/micromamba run -n openhands poetry run pip install playwright && \
  53. /openhands/micromamba/bin/micromamba run -n openhands poetry run playwright install --with-deps chromium && \
  54. # Set environment variables
  55. echo "OH_INTERPRETER_PATH=$(/openhands/micromamba/bin/micromamba run -n openhands poetry run python -c "import sys; print(sys.executable)")" >> /etc/environment && \
  56. # Clear caches
  57. /openhands/micromamba/bin/micromamba run -n openhands poetry cache clear --all . && \
  58. # Set permissions
  59. chmod -R g+rws /openhands/poetry && \
  60. mkdir -p /openhands/workspace && chmod -R g+rws,o+rw /openhands/workspace && \
  61. # Clean up
  62. apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
  63. /openhands/micromamba/bin/micromamba clean --all
  64. # ================================================================
  65. # END: Build Runtime Image from Scratch
  66. # ================================================================
  67. {% endif %}
  68. # ================================================================
  69. # Copy Project source files
  70. # ================================================================
  71. RUN if [ -d /openhands/code/openhands ]; then rm -rf /openhands/code/openhands; fi
  72. COPY ./code/pyproject.toml ./code/poetry.lock /openhands/code/
  73. COPY ./code/openhands /openhands/code/openhands
  74. # Install extra dependencies if specified
  75. {% if extra_deps %}RUN {{ extra_deps }} {% endif %}