Skip to content

πŸ™Œ OpenHands: AI-Driven Development

perf: speed up Docker builds β€” amd64-only PRs, eliminate cross-layer chmod/chown bloat (#13590)

Ray Myers3e38f13d127d
diff --git a/.github/workflows/ghcr-build.yml b/.github/workflows/ghcr-build.yml
index 8b15aba6d1..bdc7404cf9 100644
--- a/.github/workflows/ghcr-build.yml
+++ b/.github/workflows/ghcr-build.yml
@@ -33,28 +33,33 @@ jobs:
runs-on: blacksmith
outputs:
base_image: ${{ steps.define-base-images.outputs.base_image }}
+ platforms: ${{ steps.define-base-images.outputs.platforms }}
steps:
- name: Define base images
shell: bash
id: define-base-images
run: |
if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
- json=$(jq -n -c '[
- { image: "nikolaik/python-nodejs:python3.12-nodejs22-slim", tag: "nikolaik" }
+ platforms="linux/amd64"
+ json=$(jq -n -c --arg platforms "$platforms" '[
+ { image: "nikolaik/python-nodejs:python3.12-nodejs22-slim", tag: "nikolaik", platforms: $platforms }
]')
else
- json=$(jq -n -c '[
- { image: "nikolaik/python-nodejs:python3.12-nodejs22-slim", tag: "nikolaik" },
- { image: "ubuntu:24.04", tag: "ubuntu" }
+ platforms="linux/amd64,linux/arm64"
+ json=$(jq -n -c --arg platforms "$platforms" '[
+ { image: "nikolaik/python-nodejs:python3.12-nodejs22-slim", tag: "nikolaik", platforms: $platforms },
+ { image: "ubuntu:24.04", tag: "ubuntu", platforms: $platforms }
]')
fi
echo "base_image=$json" >> "$GITHUB_OUTPUT"
+ echo "platforms=$platforms" >> "$GITHUB_OUTPUT"
# Builds the OpenHands Docker images
ghcr_build_app:
name: Build App Image
runs-on: blacksmith-4vcpu-ubuntu-2204
if: "!(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/ext-v'))"
+ needs: define-matrix
permissions:
contents: read
packages: write
@@ -82,7 +87,7 @@ jobs:
- name: Build and push app image
if: "!github.event.pull_request.head.repo.fork"
run: |
- ./containers/build.sh -i openhands -o ${{ env.REPO_OWNER }} --push
+ ./containers/build.sh -i openhands -o ${{ env.REPO_OWNER }} --push -p ${{ needs.define-matrix.outputs.platforms }}
# Builds the runtime Docker images
ghcr_build_runtime:
@@ -136,7 +141,7 @@ jobs:
shell: bash
run: |
- ./containers/build.sh -i runtime -o ${{ env.REPO_OWNER }} -t ${{ matrix.base_image.tag }} --dry
+ ./containers/build.sh -i runtime -o ${{ env.REPO_OWNER }} -t ${{ matrix.base_image.tag }} --dry -p ${{ matrix.base_image.platforms }}
DOCKER_BUILD_JSON=$(jq -c . < docker-build-dry.json)
echo "DOCKER_TAGS=$(echo "$DOCKER_BUILD_JSON" | jq -r '.tags | join(",")')" >> $GITHUB_ENV
diff --git a/containers/build.sh b/containers/build.sh
index 72a1d2c117..eaf923c20e 100755
--- a/containers/build.sh
+++ b/containers/build.sh
@@ -8,15 +8,17 @@ push=0
load=0
tag_suffix=""
dry_run=0
+platform_override=""
# Function to display usage information
usage() {
- echo "Usage: $0 -i <image_name> [-o <org_name>] [--push] [--load] [-t <tag_suffix>] [--dry]"
+ echo "Usage: $0 -i <image_name> [-o <org_name>] [--push] [--load] [-t <tag_suffix>] [-p <platform>] [--dry]"
echo " -i: Image name (required)"
echo " -o: Organization name"
echo " --push: Push the image"
echo " --load: Load the image"
echo " -t: Tag suffix"
+ echo " -p: Platform(s) to build for (e.g. linux/amd64 or linux/amd64,linux/arm64)"
echo " --dry: Don't build, only create build-args.json"
exit 1
}
@@ -29,6 +31,7 @@ while [[ $# -gt 0 ]]; do
--push) push=1; shift ;;
--load) load=1; shift ;;
-t) tag_suffix="$2"; shift 2 ;;
+ -p) platform_override="$2"; shift 2 ;;
--dry) dry_run=1; shift ;;
*) usage ;;
esac
@@ -134,8 +137,10 @@ fi
echo "Args: $args"
-# Modify the platform selection based on --load flag
-if [[ $load -eq 1 ]]; then
+# Determine the platform(s) to build for
+if [[ -n "$platform_override" ]]; then
+ platform="$platform_override"
+elif [[ $load -eq 1 ]]; then
# When loading, build only for the current platform
platform=$(docker version -f '{{.Server.Os}}/{{.Server.Arch}}')
else
diff --git a/openhands/runtime/utils/runtime_templates/Dockerfile.j2 b/openhands/runtime/utils/runtime_templates/Dockerfile.j2
index 69eb841a9f..13b444ecaa 100644
--- a/openhands/runtime/utils/runtime_templates/Dockerfile.j2
+++ b/openhands/runtime/utils/runtime_templates/Dockerfile.j2
@@ -218,13 +218,11 @@ RUN \
libxdamage1 libxrandr2 libgbm1 libxss1 && \
# Install libasound2 - try new package name first (Ubuntu 24.04+), fallback to old name
(apt-get install -y --no-install-recommends libasound2t64 || apt-get install -y --no-install-recommends libasound2) && \
- apt-get clean && rm -rf /var/lib/apt/lists/* && \
# Install Playwright browsers in shared location accessible to all users
export PLAYWRIGHT_BROWSERS_PATH=/opt/playwright-browsers && \
mkdir -p /opt/playwright-browsers && \
+ umask 022 && \
/openhands/micromamba/bin/micromamba run -n openhands poetry run playwright install --with-deps chromium && \
- # Set proper permissions for shared access
- chmod -R 755 /opt/playwright-browsers && \
# Create cache directories and symlinks for both users
mkdir -p /home/openhands/.cache && \
mkdir -p /root/.cache && \
@@ -236,11 +234,9 @@ RUN \
{% endif %}
# Set environment variables (requires root)
/openhands/micromamba/bin/micromamba run -n openhands poetry run python -c "import sys; print('OH_INTERPRETER_PATH=' + sys.executable)" >> /etc/environment && \
- # Set permissions for shared read-only access
- # Note: chown -R operations removed - files are now created with correct ownership
- # by running micromamba/poetry as openhands user (see install_dependencies_user)
- chmod -R 755 /openhands/poetry && \
- chmod -R 755 /openhands/micromamba && \
+ # Permissions: poetry and micromamba are already world-readable thanks to
+ # umask 022 set during install_dependencies_user and micromamba install.
+ # No cross-layer chmod -R needed, which avoids expensive overlay2 copy-ups.
mkdir -p /openhands/workspace && chmod -R g+rws,o+rw /openhands/workspace && \
chown -R openhands:openhands /openhands/workspace && \
# Ensure PATH includes system binaries early in startup
@@ -265,6 +261,9 @@ WORKDIR /openhands/code
USER openhands
RUN \
+ # Set umask so all files are created world-readable (dirs 755, files 644).
+ # This avoids expensive cross-layer chmod -R 755 in install_dependencies_root.
+ umask 022 && \
/openhands/micromamba/bin/micromamba config set changeps1 False && \
/openhands/micromamba/bin/micromamba run -n openhands poetry config virtualenvs.path /openhands/poetry && \
/openhands/micromamba/bin/micromamba run -n openhands poetry env use python3.12 && \
@@ -306,7 +305,8 @@ RUN mkdir -p /openhands/micromamba/bin && \
# Create the openhands virtual environment and install poetry and python
# Run as openhands user to avoid expensive chown -R operations later
USER openhands
-RUN /openhands/micromamba/bin/micromamba create -n openhands -y && \
+RUN umask 022 && \
+ /openhands/micromamba/bin/micromamba create -n openhands -y && \
/openhands/micromamba/bin/micromamba install -n openhands -c conda-forge poetry python=3.12 -y
USER root
@@ -345,9 +345,12 @@ RUN \
mkdir -p /home/openhands && \
mkdir -p /openhands && \
mkdir -p $(dirname ${OPENVSCODE_SERVER_ROOT}) && \
- # Ensure ownership is correct for all OpenHands paths
- chown -R openhands:openhands /home/openhands || true && \
- chown -R openhands:openhands /openhands || true
+ # Fix ownership only on files not already owned by openhands.
+ # A blanket chown -R would trigger an overlay2 copy-up of every inode
+ # from prior layers even when ownership is already correct.
+ find /home/openhands /openhands \
+ ! \( -user openhands -group openhands \) \
+ -exec chown openhands:openhands {} + 2>/dev/null || true
{{ setup_vscode_server() }}