Dockerfile.j2 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. FROM {{ base_image }}
  2. # Shared environment variables
  3. ENV POETRY_VIRTUALENVS_PATH=/openhands/poetry \
  4. MAMBA_ROOT_PREFIX=/openhands/micromamba \
  5. LANG=C.UTF-8 \
  6. LC_ALL=C.UTF-8 \
  7. EDITOR=code \
  8. VISUAL=code \
  9. GIT_EDITOR="code --wait" \
  10. OPENVSCODE_SERVER_ROOT=/openhands/.openvscode-server
  11. {% macro setup_base_system() %}
  12. # Install base system dependencies
  13. RUN apt-get update && \
  14. apt-get install -y --no-install-recommends \
  15. wget curl sudo apt-utils git \
  16. {% if 'ubuntu' in base_image and (base_image.endswith(':latest') or base_image.endswith(':24.04')) %}
  17. libgl1 \
  18. {% else %}
  19. libgl1-mesa-glx \
  20. {% endif %}
  21. libasound2-plugins libatomic1 curl && \
  22. apt-get clean && \
  23. rm -rf /var/lib/apt/lists/*
  24. # Remove UID 1000 if it's called pn--this fixes the nikolaik image for ubuntu users
  25. RUN if getent passwd 1000 | grep -q pn; then userdel pn; fi
  26. # Create necessary directories
  27. RUN mkdir -p /openhands && \
  28. mkdir -p /openhands/logs && \
  29. mkdir -p /openhands/poetry
  30. {% endmacro %}
  31. {% macro setup_vscode_server() %}
  32. # Reference:
  33. # 1. https://github.com/gitpod-io/openvscode-server
  34. # 2. https://github.com/gitpod-io/openvscode-releases
  35. # Setup VSCode Server
  36. ARG RELEASE_TAG="openvscode-server-v1.94.2"
  37. ARG RELEASE_ORG="gitpod-io"
  38. # ARG USERNAME=openvscode-server
  39. # ARG USER_UID=1000
  40. # ARG USER_GID=1000
  41. RUN if [ -z "${RELEASE_TAG}" ]; then \
  42. echo "The RELEASE_TAG build arg must be set." >&2 && \
  43. exit 1; \
  44. fi && \
  45. arch=$(uname -m) && \
  46. if [ "${arch}" = "x86_64" ]; then \
  47. arch="x64"; \
  48. elif [ "${arch}" = "aarch64" ]; then \
  49. arch="arm64"; \
  50. elif [ "${arch}" = "armv7l" ]; then \
  51. arch="armhf"; \
  52. fi && \
  53. wget https://github.com/${RELEASE_ORG}/openvscode-server/releases/download/${RELEASE_TAG}/${RELEASE_TAG}-linux-${arch}.tar.gz && \
  54. tar -xzf ${RELEASE_TAG}-linux-${arch}.tar.gz && \
  55. mv -f ${RELEASE_TAG}-linux-${arch} ${OPENVSCODE_SERVER_ROOT} && \
  56. cp ${OPENVSCODE_SERVER_ROOT}/bin/remote-cli/openvscode-server ${OPENVSCODE_SERVER_ROOT}/bin/remote-cli/code && \
  57. rm -f ${RELEASE_TAG}-linux-${arch}.tar.gz
  58. {% endmacro %}
  59. {% macro install_dependencies() %}
  60. # Install all dependencies
  61. WORKDIR /openhands/code
  62. RUN \
  63. /openhands/micromamba/bin/micromamba config set changeps1 False && \
  64. # Configure Poetry and create virtual environment
  65. /openhands/micromamba/bin/micromamba run -n openhands poetry config virtualenvs.path /openhands/poetry && \
  66. /openhands/micromamba/bin/micromamba run -n openhands poetry env use python3.12 && \
  67. # Install project dependencies
  68. /openhands/micromamba/bin/micromamba run -n openhands poetry install --only main,runtime --no-interaction --no-root && \
  69. # Update and install additional tools
  70. apt-get update && \
  71. /openhands/micromamba/bin/micromamba run -n openhands poetry run pip install playwright && \
  72. /openhands/micromamba/bin/micromamba run -n openhands poetry run playwright install --with-deps chromium && \
  73. # Set environment variables
  74. echo "OH_INTERPRETER_PATH=$(/openhands/micromamba/bin/micromamba run -n openhands poetry run python -c "import sys; print(sys.executable)")" >> /etc/environment && \
  75. # Clear caches
  76. /openhands/micromamba/bin/micromamba run -n openhands poetry cache clear --all . && \
  77. # Set permissions
  78. chmod -R g+rws /openhands/poetry && \
  79. mkdir -p /openhands/workspace && chmod -R g+rws,o+rw /openhands/workspace && \
  80. # Clean up
  81. apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
  82. /openhands/micromamba/bin/micromamba clean --all
  83. {% endmacro %}
  84. {% if build_from_scratch %}
  85. # ================================================================
  86. # START: Build Runtime Image from Scratch
  87. # ================================================================
  88. # This is used in cases where the base image is something more generic like nikolaik/python-nodejs
  89. # rather than the current OpenHands release
  90. {{ setup_base_system() }}
  91. {{ setup_vscode_server() }}
  92. # Install micromamba
  93. RUN mkdir -p /openhands/micromamba/bin && \
  94. /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)" && \
  95. /openhands/micromamba/bin/micromamba config remove channels defaults && \
  96. /openhands/micromamba/bin/micromamba config list
  97. # Create the openhands virtual environment and install poetry and python
  98. RUN /openhands/micromamba/bin/micromamba create -n openhands -y && \
  99. /openhands/micromamba/bin/micromamba install -n openhands -c conda-forge poetry python=3.12 -y
  100. # Create a clean openhands directory including only the pyproject.toml, poetry.lock and openhands/__init__.py
  101. RUN \
  102. if [ -d /openhands/code ]; then rm -rf /openhands/code; fi && \
  103. mkdir -p /openhands/code/openhands && \
  104. touch /openhands/code/openhands/__init__.py
  105. COPY ./code/pyproject.toml ./code/poetry.lock /openhands/code/
  106. {{ install_dependencies() }}
  107. # ================================================================
  108. # END: Build Runtime Image from Scratch
  109. # ================================================================
  110. {% endif %}
  111. # ================================================================
  112. # Copy Project source files
  113. # ================================================================
  114. RUN if [ -d /openhands/code/openhands ]; then rm -rf /openhands/code/openhands; fi
  115. COPY ./code/pyproject.toml ./code/poetry.lock /openhands/code/
  116. COPY ./code/openhands /openhands/code/openhands
  117. RUN chmod a+rwx /openhands/code/openhands/__init__.py
  118. # ================================================================
  119. # END: Build from versioned image
  120. # ================================================================
  121. {% if build_from_versioned %}
  122. {{ install_dependencies() }}
  123. {% endif %}
  124. # Install extra dependencies if specified
  125. {% if extra_deps %}RUN {{ extra_deps }} {% endif %}