如何配置tox-docker在容器内而非本地安装依赖?
Solution
To resolve the local installation failure of cuda-python on your M2 Pro MacBook while running tests via tox-docker, modify your tox.ini with the following changes:
Modified tox.ini Sections
[testenv:test-docker-env] docker = py-3.11 skip_install = true # Skip local installation of project and dependencies deps = -r{toxinidir}/requirements/requirements.txt # Now safe to include—installs only in container -r{toxinidir}/requirements/requirements-test.txt commands = python -m pytest --cov=src \ --cov-report term-missing \ --cov-report xml:coverage-reports/coverage-BUILD_{env:BUILD_NUMBER:0}.xml \ --junitxml=xunit-reports/xunit-result-BUILD_{env:BUILD_NUMBER:0}.xml \ -vv \ tests/ # Map local report directories to container to preserve test results docker_volumes = {toxinidir}/coverage-reports:/app/coverage-reports {toxinidir}/xunit-reports:/app/xunit-reports [docker:py-3.11] # Use a CUDA-enabled image with Python 3.11 (supports ARM64 for M2) image = nvidia/cuda:12.2.0-python3.11-runtime-ubuntu22.04 working_dir = /app # Align with tox's project copy path # Ensure CUDA runtime libraries are accessible docker_run_env = LD_LIBRARY_PATH=/usr/local/nvidia/lib64:/usr/local/cuda/lib64 # Grant container access to host GPU (required for CUDA execution; omit if tests don't need GPU) docker_run_args = --gpus all
Key Changes Explained
skip_install = true: This stops tox from trying to install your project or dependencies on your local MacBook. All setup and test execution happens entirely inside the Docker container.- Re-enabled
requirements.txt: Since local installation is skipped, including this file no longer causes failures—cuda-pythonis installed only in the container where CUDA libraries are present. - CUDA-enabled Docker Image: The base
python:3.11image lacks CUDA runtime libraries, which are mandatory forcuda-python. Using an official NVIDIA CUDA image with Python pre-installed ensures all necessary dependencies are available. - Volume Mapping: The
docker_volumesdirective ensures test reports (coverage, xunit) generated inside the container are saved back to your local machine. - GPU Access: The
--gpus allargument lets the container access your host's GPU (if needed for test execution). If your tests don't require actual GPU usage, you can omit this line—cuda-pythonwill still install successfully as long as the CUDA runtime is present in the image.
Prerequisites
- Ensure you have Docker Desktop installed on your M2 MacBook with ARM64 support enabled.
- Install the
tox-dockerplugin if you haven't already:pip install tox-docker - For GPU access, set up the NVIDIA Container Toolkit (relevant if you're using an external NVIDIA GPU with your MacBook, or if your tests require GPU execution).
内容的提问来源于stack exchange,提问作者MaxU - stand with Ukraine
相关产品推荐
相关产品推荐

