我是docker新手,需要在docker环境下跑一段代码。
我在构建 Dockerfile 时遇到错误:
我正在通过 hyper-V 运行 Ubuntu 20.04,当我构建 Dockerfile 时,我收到以下消息:
Step 4/20 : RUN curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && chmod +x ~/miniconda.sh && ~/miniconda.sh -b -p /opt/conda && rm ~/miniconda.sh && /opt/conda/bin/conda install numpy pyyaml scipy ipython mkl && /opt/conda/bin/conda install -c soumith magma-cuda90 && /opt/conda/bin/conda clean -ya <br />
---> Running in 9758f4fe60a4 <br />
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
/bin/sh: 1: /opt/conda/bin/conda: not found
The command '/bin/sh -c curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && chmod +x ~/miniconda.sh && ~/miniconda.sh -b -p /opt/conda && rm ~/miniconda.sh && /opt/conda/bin/conda install numpy pyyaml scipy ipython mkl && /opt/conda/bin/conda install -c soumith magma-cuda90 && /opt/conda/bin/conda clean -ya' returned a non-zero code: 127
docker 文件:
# PyTorch Install
FROM nvidia/cuda:9.0-cudnn7-devel-ubuntu16.04
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
cmake \
git \
curl \
vim \
emacs \
parallel \
ca-certificates \
libjpeg-dev \
hdf5-tools \
libpng-dev &&\
rm -rf /var/lib/apt/lists/*
RUN mkdir ~/.parallel && touch ~/.parallel/will-cite
RUN sudo curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
chmod +x ~/miniconda.sh && \
~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh && \
/opt/conda/bin/conda install numpy pyyaml scipy ipython mkl && \
/opt/conda/bin/conda install -c soumith magma-cuda90 && \
/opt/conda/bin/conda clean -ya
ENV PATH /opt/conda/bin:$PATH
# This must be done before pip so that requirements.txt is available
WORKDIR /opt/pytorch
COPY . .
RUN conda install pytorch torchvision -c pytorch
#
# Now install the julia dependencies.
#
WORKDIR /opt/julia
RUN pip install pandas matplotlib utils argh biopython
RUN conda install networkx joblib
RUN apt-get update && apt-get install -y curl
RUN mkdir /julia
RUN curl -L https://julialang-s3.julialang.org/bin/linux/x64/0.6/julia-0.6.2-linux-x86_64.tar.gz | tar -C /julia --strip-components=1 -xzf -
ENV PATH "/julia/bin:$PATH"
RUN julia -e "Pkg.init()"
COPY setup.jl /julia/setup.jl
RUN julia /julia/setup.jl
WORKDIR /root/hyperbolics
ENV PYTHONPATH /root/hyperbolics
当我在命令提示符下直接在 RUN 下尝试命令时,它运行良好。
但是,当我构建 Dockerfile、julia、pip 等时,每个命令都是'not found'
。(当我注释掉 conda... 部分时)
我该如何解决这个问题?
最佳答案
/bin/sh: 1:/opt/conda/bin/conda: not found
错误是conda没有正确安装导致的。这是因为您下载的 miniconda.sh
文件(使用 curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux -x86_64.sh
) 是一个空文件。
发生这种情况是因为 curl by default doesn't follow redirects ,并且来自上面 url 的响应发送重定向而不是直接发送 miniconda.sh
文件。您可以通过检查 URL 的 header 来验证这一点(例如 curl -i https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
显示 HTTP 301 重定向状态代码).
您可以通过在您的 curl 命令中提供 -L 标志来告诉 curl 跟随重定向来解决这个问题,例如:
(我还必须删除 sudo
)
RUN curl -L -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
chmod +x ~/miniconda.sh && \
~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh && \
/opt/conda/bin/conda install numpy pyyaml scipy ipython mkl && \
/opt/conda/bin/conda install -c soumith magma-cuda90 && \
/opt/conda/bin/conda clean -ya
这里是一些调试信息/研究:
您可以通过查看上面代码段中 curl 的输出来验证空白 miniconda.sh
是否存在问题。看起来有点神秘,但你可以看到实际上没有下载任何内容
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
当我尝试构建添加了 -L 标志的 docker 镜像时,输出如下所示:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0
100 88.7M 100 88.7M 0 0 11.9M 0 0:00:07 0:00:07 --:--:-- 34.7M
您还可以通过删除下载/运行部分之前的所有内容来手动检查 miniconda.sh 的内容
(例如,保持这部分的所有内容)
RUN sudo curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
chmod +x ~/miniconda.sh && \
~/miniconda.sh -b -p /opt/conda
然后运行 sudo docker run -it containername/bin/bash
并检查 miniconda.sh 的内容(cat ~/miniconda.sh
,它揭示了文件为空)。
关于linux - 构建 docker : opt/conda/bin/conda not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63359198/