Dockerfile.j2 4.2 KB

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