# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license

# Builds GitHub actions CI runner image for deployment to DockerHub https://hub.docker.com/r/ultralytics/ultralytics
# Image is CUDA-optimized for YOLO single/multi-GPU training and inference tests

# Start FROM Ultralytics GPU image
FROM ultralytics/ultralytics:latest
COPY --from=node:24-bookworm-slim /usr/local/bin/node /usr/local/bin/node
COPY --from=node:24-bookworm-slim /usr/local/lib/node_modules/npm/ /usr/local/lib/node_modules/npm/

# Set additional environment variables for runner (no "v" on the version)
ARG RUNNER_VERSION=2.338.0
ENV RUNNER_ALLOW_RUNASROOT=1 \
    DEBIAN_FRONTEND=noninteractive

# Set the working directory
WORKDIR /actions-runner

# Download and unpack the runner from https://github.com/actions/runner and install dependencies
RUN FILENAME=actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz && \
    ln -s ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \
    ln -s ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx && \
    curl -fLso "$FILENAME" "https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/${FILENAME}" && \
    tar xzf "$FILENAME" && \
    rm "$FILENAME" && \
    uv pip install --system --no-cache pytest-cov tensorrt-cu13 && \
    ./bin/installdependencies.sh && \
    # libgl1 and libglib2.0-0 for GUI opencv-python pulled in by gpu-latest jobs, e.g. vllm-omni in ultralytics/annotate \
    apt-get install -y --no-install-recommends g++ gh gpg gpg-agent jq libicu-dev sudo libgl1 libglib2.0-0 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# JSON ENTRYPOINT command to configure and start runner with default TOKEN and NAME
ENTRYPOINT ["sh", "-c", "./config.sh --url https://github.com/ultralytics --token ${GITHUB_RUNNER_TOKEN:-TOKEN} --name ${GITHUB_RUNNER_NAME:-NAME} --labels gpu-latest --replace && exec ./run.sh"]

# Usage --------------------------------------------------------------------------------------------------------------

# Production builds: https://github.com/ultralytics/ultralytics/blob/main/.github/workflows/docker.yml
# Example (build): t=ultralytics/ultralytics:latest-runner && docker build -f docker/Dockerfile-runner -t $t .
# Example (push): docker push $t
# Example (pull): t=ultralytics/ultralytics:latest-runner && docker pull $t
# Example (run): docker run -d --restart unless-stopped -e GITHUB_RUNNER_TOKEN=TOKEN -e GITHUB_RUNNER_NAME=NAME --ipc=host --device nvidia.com/gpu=all $t
# CDI requires Docker >= 28.2.0 and nvidia-container-toolkit >= 1.18.
# Legacy --gpus all can lose GPU access after host systemd daemon reloads (nvidia-container-toolkit#48).
