Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: pip not available in runtime (#3306)

Xingyao Wangbdf6df12c34b
diff --git a/.github/workflows/ghcr.yml b/.github/workflows/ghcr.yml
index 770c181a7f..66270d3a0e 100644
--- a/.github/workflows/ghcr.yml
+++ b/.github/workflows/ghcr.yml
@@ -81,7 +81,7 @@ jobs:
strategy:
matrix:
image: ['od_runtime']
- base_image: ['ubuntu:22.04']
+ base_image: ['nikolaik/python-nodejs:python3.11-nodejs22']
platform: ['amd64', 'arm64']
steps:
- name: Checkout
@@ -115,7 +115,7 @@ jobs:
- name: Install Python dependencies using Poetry
run: make install-python-dependencies
- name: Create source distribution and Dockerfile
- run: poetry run python3 opendevin/runtime/utils/runtime_build.py --base_image ${{ matrix.base_image }} --build_folder containers/runtime
+ run: poetry run python3 opendevin/runtime/utils/runtime_build.py --base_image ${{ matrix.base_image }} --build_folder containers/runtime --force_rebuild
- name: Build and export image
id: build
run: |
diff --git a/config.template.toml b/config.template.toml
index 1307852d3d..644e1d6034 100644
--- a/config.template.toml
+++ b/config.template.toml
@@ -174,7 +174,7 @@ llm_config = 'gpt3'
#user_id = 1000
# Container image to use for the sandbox
-#container_image = "ghcr.io/opendevin/sandbox:main"
+#container_image = "nikolaik/python-nodejs:python3.11-nodejs22"
# Use host network
#use_host_network = false
diff --git a/evaluation/EDA/run_infer.py b/evaluation/EDA/run_infer.py
index 6d489c3053..29ce4241c6 100644
--- a/evaluation/EDA/run_infer.py
+++ b/evaluation/EDA/run_infer.py
@@ -62,7 +62,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=False,
use_host_network=False,
),
diff --git a/evaluation/agent_bench/run_infer.py b/evaluation/agent_bench/run_infer.py
index 23514634b6..77b7a1e002 100644
--- a/evaluation/agent_bench/run_infer.py
+++ b/evaluation/agent_bench/run_infer.py
@@ -44,7 +44,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=True,
use_host_network=False,
),
diff --git a/evaluation/bird/run_infer.py b/evaluation/bird/run_infer.py
index 70402c6a29..86db5eb9eb 100644
--- a/evaluation/bird/run_infer.py
+++ b/evaluation/bird/run_infer.py
@@ -75,7 +75,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=True,
use_host_network=False,
),
diff --git a/evaluation/browsing_delegation/run_infer.py b/evaluation/browsing_delegation/run_infer.py
index 983d753c8e..c4d5ecd4d6 100644
--- a/evaluation/browsing_delegation/run_infer.py
+++ b/evaluation/browsing_delegation/run_infer.py
@@ -40,7 +40,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=False,
use_host_network=False,
),
diff --git a/evaluation/gaia/run_infer.py b/evaluation/gaia/run_infer.py
index 7a791c4aa7..0ff4b1a434 100644
--- a/evaluation/gaia/run_infer.py
+++ b/evaluation/gaia/run_infer.py
@@ -51,7 +51,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=True,
use_host_network=False,
),
diff --git a/evaluation/gorilla/run_infer.py b/evaluation/gorilla/run_infer.py
index 8aad9d9da9..d84432c3b0 100644
--- a/evaluation/gorilla/run_infer.py
+++ b/evaluation/gorilla/run_infer.py
@@ -42,7 +42,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=True,
use_host_network=False,
),
diff --git a/evaluation/gpqa/run_infer.py b/evaluation/gpqa/run_infer.py
index 32cee5fb7e..8f2df78e5e 100644
--- a/evaluation/gpqa/run_infer.py
+++ b/evaluation/gpqa/run_infer.py
@@ -65,7 +65,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=True,
use_host_network=False,
),
diff --git a/evaluation/humanevalfix/run_infer.py b/evaluation/humanevalfix/run_infer.py
index 94d23f9313..8fc8950428 100644
--- a/evaluation/humanevalfix/run_infer.py
+++ b/evaluation/humanevalfix/run_infer.py
@@ -86,7 +86,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=True,
use_host_network=False,
),
diff --git a/evaluation/toolqa/run_infer.py b/evaluation/toolqa/run_infer.py
index 2eef9d2380..930f1199e6 100644
--- a/evaluation/toolqa/run_infer.py
+++ b/evaluation/toolqa/run_infer.py
@@ -45,7 +45,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=True,
use_host_network=False,
),
diff --git a/evaluation/webarena/run_infer.py b/evaluation/webarena/run_infer.py
index c0a3182f77..f661a147c9 100644
--- a/evaluation/webarena/run_infer.py
+++ b/evaluation/webarena/run_infer.py
@@ -54,7 +54,7 @@ def get_config(
runtime='eventstream',
max_iterations=metadata.max_iterations,
sandbox=SandboxConfig(
- container_image='ubuntu:22.04',
+ container_image='python:3.11-bookworm',
enable_auto_lint=True,
use_host_network=False,
browsergym_eval_env=env_id,
diff --git a/opendevin/core/config.py b/opendevin/core/config.py
index 46217d3784..9f918b0248 100644
--- a/opendevin/core/config.py
+++ b/opendevin/core/config.py
@@ -166,9 +166,7 @@ class SandboxConfig(metaclass=Singleton):
"""
api_hostname: str = 'localhost'
- container_image: str = (
- 'ubuntu:22.04' # default to ubuntu:22.04 for eventstream runtime
- )
+ container_image: str = 'nikolaik/python-nodejs:python3.11-nodejs22' # default to nikolaik/python-nodejs:python3.11-nodejs22 for eventstream runtime
user_id: int = os.getuid() if hasattr(os, 'getuid') else 1000
timeout: int = 120
enable_auto_lint: bool = (
diff --git a/opendevin/runtime/utils/image_agnostic.py b/opendevin/runtime/utils/image_agnostic.py
deleted file mode 100644
index d44c9c4ebb..0000000000
--- a/opendevin/runtime/utils/image_agnostic.py
+++ /dev/null
@@ -1,113 +0,0 @@
-"""This module contains functions for building and managing the agnostic sandbox image.
-
-This WILL BE DEPRECATED when EventStreamRuntime is fully implemented and adopted.
-"""
-
-import tempfile
-
-import docker
-
-from opendevin.core.logger import opendevin_logger as logger
-
-
-def generate_dockerfile(base_image: str) -> str:
- """Generate the Dockerfile content for the agnostic sandbox image based on user-provided base image.
-
- NOTE: This is only tested on debian yet.
- """
- # FIXME: Remove the requirement of ssh in future version
- dockerfile_content = (
- f'FROM {base_image}\n'
- 'RUN apt update && apt install -y openssh-server wget sudo\n'
- 'RUN mkdir -p -m0755 /var/run/sshd\n'
- 'RUN mkdir -p /opendevin && mkdir -p /opendevin/logs && chmod 777 /opendevin/logs\n'
- 'RUN echo "" > /opendevin/bash.bashrc\n'
- 'RUN if [ ! -d /opendevin/miniforge3 ]; then \\\n'
- ' wget --progress=bar:force -O Miniforge3.sh "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh" && \\\n'
- ' bash Miniforge3.sh -b -p /opendevin/miniforge3 && \\\n'
- ' rm Miniforge3.sh && \\\n'
- ' chmod -R g+w /opendevin/miniforge3 && \\\n'
- ' bash -c ". /opendevin/miniforge3/etc/profile.d/conda.sh && conda config --set changeps1 False && conda config --append channels conda-forge"; \\\n'
- ' fi\n'
- 'RUN /opendevin/miniforge3/bin/pip install --upgrade pip\n'
- 'RUN /opendevin/miniforge3/bin/pip install jupyterlab notebook jupyter_kernel_gateway flake8\n'
- 'RUN /opendevin/miniforge3/bin/pip install python-docx PyPDF2 python-pptx pylatexenc openai\n'
- ).strip()
- return dockerfile_content
-
-
-def _build_sandbox_image(
- base_image: str, target_image_name: str, docker_client: docker.DockerClient
-):
- try:
- with tempfile.TemporaryDirectory() as temp_dir:
- dockerfile_content = generate_dockerfile(base_image)
-
- logger.info(f'Building agnostic sandbox image: {target_image_name}')
- logger.info(
- (
- f'===== Dockerfile content =====\n'
- f'{dockerfile_content}\n'
- f'==============================='
- )
- )
- with open(f'{temp_dir}/Dockerfile', 'w') as file:
- file.write(dockerfile_content)
-
- api_client = docker_client.api
- build_logs = api_client.build(
- path=temp_dir, tag=target_image_name, rm=True, decode=True
- )
-
- for log in build_logs:
- if 'stream' in log:
- print(log['stream'].strip())
- elif 'error' in log:
- logger.error(log['error'].strip())
- else:
- logger.info(str(log))
-
- logger.info(f'Image {target_image_name} built successfully')
- except docker.errors.BuildError as e:
- logger.error(f'Sandbox image build failed: {e}')
- raise e
- except Exception as e:
- logger.error(f'An error occurred during sandbox image build: {e}')
- raise e
-
-
-def _get_new_image_name(base_image: str) -> str:
- prefix = 'od_sandbox'
- if ':' not in base_image:
- base_image = base_image + ':latest'
-
- [repo, tag] = base_image.split(':')
- repo = repo.replace('/', '___')
- return f'{prefix}:{repo}__{tag}'
-
-
-def get_od_sandbox_image(base_image: str, docker_client: docker.DockerClient) -> str:
- """Return the sandbox image name based on user-provided base image.
-
- The returned sandbox image is assumed to contains all the required dependencies for OpenDevin.
- If the sandbox image is not found, it will be built.
- """
- # OpenDevin's offcial sandbox already contains the required dependencies for OpenDevin.
- if 'ghcr.io/opendevin/sandbox' in base_image:
- return base_image
-
- new_image_name = _get_new_image_name(base_image)
-
- # Detect if the sandbox image is built
- images = docker_client.images.list()
- for image in images:
- if new_image_name in image.tags:
- logger.info('Found existing od_sandbox image, reuse:' + new_image_name)
- return new_image_name
-
- # If the sandbox image is not found, build it
- logger.info(
- f'od_sandbox image is not found for {base_image}, will build: {new_image_name}'
- )
- _build_sandbox_image(base_image, new_image_name, docker_client)
- return new_image_name
diff --git a/opendevin/runtime/utils/runtime_build.py b/opendevin/runtime/utils/runtime_build.py
index b04e99d15e..3f51a7f7ca 100644
--- a/opendevin/runtime/utils/runtime_build.py
+++ b/opendevin/runtime/utils/runtime_build.py
@@ -239,6 +239,7 @@ def build_runtime_image(
extra_deps: str | None = None,
docker_build_folder: str | None = None,
dry_run: bool = False,
+ force_rebuild: bool = False,
) -> str:
"""Build the runtime image for the OpenDevin runtime.
@@ -277,7 +278,10 @@ def build_runtime_image(
# 2. If the exact hash is not found, we will FIRST try to re-build it
# by leveraging the non-hash `generic_runtime_image_name` to save some time
# from re-building the dependencies (e.g., poetry install, apt install)
- elif _check_image_exists(generic_runtime_image_name, docker_client):
+ elif (
+ _check_image_exists(generic_runtime_image_name, docker_client)
+ and not force_rebuild
+ ):
logger.info(
f'Cannot find matched hash for image [{hash_runtime_image_name}]\n'
f'Will try to re-build it from latest [{generic_runtime_image_name}] image to potentially save '
@@ -319,6 +323,10 @@ def build_runtime_image(
# 3. If the image is not found AND we cannot re-use the non-hash latest relavant image,
# we will build it completely from scratch
else:
+ if force_rebuild:
+ logger.info(
+ f'Force re-build: Will try to re-build image [{generic_runtime_image_name}] from scratch.\n'
+ )
cur_docker_build_folder = docker_build_folder or tempfile.mkdtemp()
_new_from_scratch_hash = prep_docker_build_folder(
cur_docker_build_folder,
@@ -352,8 +360,11 @@ def build_runtime_image(
if __name__ == '__main__':
parser = argparse.ArgumentParser()
- parser.add_argument('--base_image', type=str, default='ubuntu:22.04')
+ parser.add_argument(
+ '--base_image', type=str, default='nikolaik/python-nodejs:python3.11-nodejs22'
+ )
parser.add_argument('--build_folder', type=str, default=None)
+ parser.add_argument('--force_rebuild', action='store_true', default=False)
args = parser.parse_args()
if args.build_folder is not None:
@@ -373,6 +384,7 @@ if __name__ == '__main__':
docker_client=docker.from_env(),
docker_build_folder=temp_dir,
dry_run=True,
+ force_rebuild=args.force_rebuild,
)
_runtime_image_repo, runtime_image_hash_tag = runtime_image_hash_name.split(
':'
diff --git a/opendevin/runtime/utils/runtime_templates/Dockerfile.j2 b/opendevin/runtime/utils/runtime_templates/Dockerfile.j2
index 6121e0e356..b7d04d2465 100644
--- a/opendevin/runtime/utils/runtime_templates/Dockerfile.j2
+++ b/opendevin/runtime/utils/runtime_templates/Dockerfile.j2
@@ -18,13 +18,6 @@ RUN apt-get update && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
-# Install Python if not already installed
-RUN if [ ! -e /usr/bin/python ]; then \
- apt-get update && \
- apt-get install -y python3 && \
- ln -s /usr/bin/python3 /usr/bin/python; \
-fi
-
# Create necessary directories
RUN mkdir -p /opendevin && \
mkdir -p /opendevin/logs && \
diff --git a/tests/integration/regenerate.sh b/tests/integration/regenerate.sh
index 016be2ca89..56253c5b96 100755
--- a/tests/integration/regenerate.sh
+++ b/tests/integration/regenerate.sh
@@ -59,7 +59,7 @@ mkdir -p $WORKSPACE_BASE
TEST_RUNTIME="${TEST_RUNTIME:-eventstream}" # can be server or eventstream
# TODO: set this as default after ServerRuntime is deprecated
if [ "$TEST_RUNTIME" == "eventstream" ] && [ -z "$SANDBOX_CONTAINER_IMAGE" ]; then
- SANDBOX_CONTAINER_IMAGE="ubuntu:22.04"
+ SANDBOX_CONTAINER_IMAGE="nikolaik/python-nodejs:python3.11-nodejs22"
fi
MAX_ITERATIONS=15
diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py
index acff8be7cc..301c157907 100644
--- a/tests/unit/test_config.py
+++ b/tests/unit/test_config.py
@@ -358,7 +358,7 @@ def test_defaults_dict_after_updates(default_config):
assert defaults_after_updates['sandbox']['timeout']['default'] == 120
assert (
defaults_after_updates['sandbox']['container_image']['default']
- == 'ubuntu:22.04'
+ == 'nikolaik/python-nodejs:python3.11-nodejs22'
)
assert defaults_after_updates == initial_defaults
diff --git a/tests/unit/test_image_agnostic_util.py b/tests/unit/test_image_agnostic_util.py
deleted file mode 100644
index d5a94bfe24..0000000000
--- a/tests/unit/test_image_agnostic_util.py
+++ /dev/null
@@ -1,51 +0,0 @@
-from unittest.mock import MagicMock, patch
-
-from opendevin.runtime.utils.image_agnostic import (
- _get_new_image_name,
- generate_dockerfile,
- get_od_sandbox_image,
-)
-
-
-def test_generate_dockerfile():
- base_image = 'debian:11'
- dockerfile_content = generate_dockerfile(base_image)
- assert base_image in dockerfile_content
- assert (
- 'RUN apt update && apt install -y openssh-server wget sudo'
- in dockerfile_content
- )
-
-
-def test_get_new_image_name_legacy():
- # test non-eventstream runtime (sandbox-based)
- base_image = 'debian:11'
- new_image_name = _get_new_image_name(base_image)
- assert new_image_name == 'od_sandbox:debian__11'
-
- base_image = 'ubuntu:22.04'
- new_image_name = _get_new_image_name(base_image)
- assert new_image_name == 'od_sandbox:ubuntu__22.04'
-
- base_image = 'ubuntu'
- new_image_name = _get_new_image_name(base_image)
- assert new_image_name == 'od_sandbox:ubuntu__latest'
-
-
-@patch('opendevin.runtime.utils.image_agnostic._build_sandbox_image')
-@patch('opendevin.runtime.utils.image_agnostic.docker.DockerClient')
-def test_get_od_sandbox_image(mock_docker_client, mock_build_sandbox_image):
- base_image = 'debian:11'
- mock_docker_client.images.list.return_value = [
- MagicMock(tags=['od_sandbox:debian__11'])
- ]
-
- image_name = get_od_sandbox_image(base_image, mock_docker_client)
- assert image_name == 'od_sandbox:debian__11'
-
- mock_docker_client.images.list.return_value = []
- image_name = get_od_sandbox_image(base_image, mock_docker_client)
- assert image_name == 'od_sandbox:debian__11'
- mock_build_sandbox_image.assert_called_once_with(
- base_image, 'od_sandbox:debian__11', mock_docker_client
- )
diff --git a/tests/unit/test_runtime.py b/tests/unit/test_runtime.py
index 9614f5adbf..190998436c 100644
--- a/tests/unit/test_runtime.py
+++ b/tests/unit/test_runtime.py
@@ -83,7 +83,9 @@ def enable_auto_lint(request):
return request.param
[email protected](scope='module', params=['ubuntu:22.04', 'debian:11'])
+ scope='module', params=['nikolaik/python-nodejs:python3.11-nodejs22', 'debian:11']
+)
def container_image(request):
time.sleep(1)
return request.param
@@ -127,7 +129,7 @@ async def _load_runtime(
if 'od_runtime' not in cur_container_image and cur_container_image not in {
'xingyaoww/od-eval-miniwob:v1.0'
}: # a special exception list
- cur_container_image = 'ubuntu:22.04'
+ cur_container_image = 'nikolaik/python-nodejs:python3.11-nodejs22'
logger.warning(
f'`{config.sandbox.container_image}` is not an od_runtime image. Will use `{cur_container_image}` as the container image for testing.'
)
@@ -1033,6 +1035,13 @@ async def test_bash_python_version(temp_dir, box_class):
assert obs.exit_code == 0
# Should not error out
+ action = CmdRunAction(command='pip --version')
+ logger.info(action, extra={'msg_type': 'ACTION'})
+ obs = await runtime.run_action(action)
+ logger.info(obs, extra={'msg_type': 'OBSERVATION'})
+ assert obs.exit_code == 0
+ # Should not error out
+
await runtime.close()
await asyncio.sleep(1)
diff --git a/tests/unit/test_runtime_build.py b/tests/unit/test_runtime_build.py
index 027b257b73..438355401e 100644
--- a/tests/unit/test_runtime_build.py
+++ b/tests/unit/test_runtime_build.py
@@ -62,7 +62,7 @@ def test_put_source_code_to_dir(temp_dir):
def test_docker_build_folder(temp_dir):
prep_docker_build_folder(
temp_dir,
- base_image='ubuntu:22.04',
+ base_image='nikolaik/python-nodejs:python3.11-nodejs22',
skip_init=False,
)
@@ -81,14 +81,14 @@ def test_docker_build_folder(temp_dir):
def test_hash_folder_same(temp_dir):
dir_hash_1 = prep_docker_build_folder(
temp_dir,
- base_image='ubuntu:22.04',
+ base_image='nikolaik/python-nodejs:python3.11-nodejs22',
skip_init=False,
)
with tempfile.TemporaryDirectory() as temp_dir_2:
dir_hash_2 = prep_docker_build_folder(
temp_dir_2,
- base_image='ubuntu:22.04',
+ base_image='nikolaik/python-nodejs:python3.11-nodejs22',
skip_init=False,
)
assert dir_hash_1 == dir_hash_2
@@ -97,14 +97,14 @@ def test_hash_folder_same(temp_dir):
def test_hash_folder_diff_init(temp_dir):
dir_hash_1 = prep_docker_build_folder(
temp_dir,
- base_image='ubuntu:22.04',
+ base_image='nikolaik/python-nodejs:python3.11-nodejs22',
skip_init=False,
)
with tempfile.TemporaryDirectory() as temp_dir_2:
dir_hash_2 = prep_docker_build_folder(
temp_dir_2,
- base_image='ubuntu:22.04',
+ base_image='nikolaik/python-nodejs:python3.11-nodejs22',
skip_init=True,
)
assert dir_hash_1 != dir_hash_2
@@ -113,7 +113,7 @@ def test_hash_folder_diff_init(temp_dir):
def test_hash_folder_diff_image(temp_dir):
dir_hash_1 = prep_docker_build_folder(
temp_dir,
- base_image='ubuntu:22.04',
+ base_image='nikolaik/python-nodejs:python3.11-nodejs22',
skip_init=False,
)
@@ -178,11 +178,12 @@ def test_get_runtime_image_repo_and_tag_eventstream():
and img_tag == f'{OD_VERSION}_image_debian_tag_11'
)
- base_image = 'ubuntu:22.04'
+ base_image = 'nikolaik/python-nodejs:python3.11-nodejs22'
img_repo, img_tag = get_runtime_image_repo_and_tag(base_image)
assert (
img_repo == f'{RUNTIME_IMAGE_REPO}'
- and img_tag == f'{OD_VERSION}_image_ubuntu_tag_22.04'
+ and img_tag
+ == f'{OD_VERSION}_image_nikolaik___python-nodejs_tag_python3.11-nodejs22'
)
base_image = 'ubuntu'