使用dev镜像执行docker exec pytest时提示找不到可执行文件的问题
解决Docker容器中pytest找不到的问题(rapidsai-dev镜像场景)
问题场景
使用rapidsai/rapidsai-dev:22.04-cuda11.5-devel-ubuntu20.04-py3.9镜像构建容器后,在CI中执行以下命令时出现错误:
CONTAINER=$(docker run -d gpu-test) docker exec $CONTAINER pytest
错误信息:
OCI runtime exec failed: exec failed: unable to start container process: exec: "pytest": executable file not found in $PATH: unknown
但改用Dockerfile中注释的rapidsai/rapidsai:22.04-cuda11.5-runtime-ubuntu20.04-py3.9镜像构建容器则无此问题,当前Dockerfile内容如下:
# Pulls the basic Image from NVIDIA repository FROM rapidsai/rapidsai-dev:22.04-cuda11.5-devel-ubuntu20.04-py3.9 # FROM rapidsai/rapidsai:22.04-cuda11.5-runtime-ubuntu20.04-py3.9 # Updates OS libraries RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ --fix-missing git python3-setuptools python3-pip build-essential libcurl4-gnutls-dev \ zlib1g-dev rsync vim nano cmake tabix # Install libraries needed in the examples RUN /opt/conda/envs/rapids/bin/pip install \ scanpy==1.9.1 wget pytabix dash-daq \ dash-html-components dash-bootstrap-components dash-core-components \ utils pytest RUN /opt/conda/envs/rapids/bin/pip install \ torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116 WORKDIR /workspace ENV HOME /workspace
原因分析
- 你通过
/opt/conda/envs/rapids/bin/pip安装的pytest,实际位于/opt/conda/envs/rapids/bin/pytest路径下 rapidsai-dev镜像的默认环境变量PATH未包含/opt/conda/envs/rapids/bin目录,导致直接执行pytest时系统找不到该命令- 而
rapidsai镜像默认激活了rapidsconda环境,其PATH已包含上述目录,因此能正常找到pytest
解决方案
方案1:直接调用pytest的完整路径
修改CI中的执行命令,指定pytest的绝对路径:
docker exec $CONTAINER /opt/conda/envs/rapids/bin/pytest
方案2:在Dockerfile中添加conda环境路径到PATH
在Dockerfile末尾添加ENV指令,将conda环境的bin目录加入全局PATH:
WORKDIR /workspace ENV HOME /workspace # 新增以下行 ENV PATH="/opt/conda/envs/rapids/bin:$PATH"
重新构建镜像后,直接执行docker exec $CONTAINER pytest即可正常运行。
方案3:执行命令时先激活conda环境
通过bash执行命令,先激活rapids环境再运行pytest:
docker exec $CONTAINER bash -c "source /opt/conda/bin/activate rapids && pytest"
内容的提问来源于stack exchange,提问作者fabio.geraci
相关产品推荐
相关产品推荐

