Dockerfile.j2 4.5 KB

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