如何在 Dockerfile 中使用 miniconda 安装软件包?

问题描述 投票:0回答:3

我有一个简单的 Dockerfile:

FROM ubuntu:18.04

RUN apt-get update

RUN apt-get install -y wget && rm -rf /var/lib/apt/lists/*

RUN wget \
    https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && mkdir /root/.conda \
    && bash Miniconda3-latest-Linux-x86_64.sh -b \
    && rm -f Miniconda3-latest-Linux-x86_64.sh \
    && echo PATH="/root/miniconda3/bin":$PATH >> .bashrc \
    && exec bash \
    && conda --version

RUN conda --version

而且它无法建造。在最后一步我得到了

/bin/sh: 1: conda: not found
....
第一次出现
conda --version
并没有引发错误,这让我想知道这是一个
PATH
问题吗?
我想在这个 Dockerfile 中有另一个
RUN
条目,我将在其中安装带有
conda install ...

的软件包 最后,我想要有
CMD ["bash", "test.py"]
条目,以便在执行
docker run
这个图像时,它会自动运行一个简单的 python 脚本,该脚本导入使用 conda 安装的所有库。也许还有一个
CMD ["bash", "test.sh"]
脚本可以测试 conda 和 python 解释器是否确实安装。

这是一个简化的示例,会有很多软件,所以我不想更改基础镜像。

python docker anaconda dockerfile miniconda
3个回答
90
投票

这将使用 ARG 和 ENV 工作:

FROM ubuntu:18.04
ENV PATH="/root/miniconda3/bin:${PATH}"
ARG PATH="/root/miniconda3/bin:${PATH}"
RUN apt-get update

RUN apt-get install -y wget && rm -rf /var/lib/apt/lists/*

RUN wget \
    https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && mkdir /root/.conda \
    && bash Miniconda3-latest-Linux-x86_64.sh -b \
    && rm -f Miniconda3-latest-Linux-x86_64.sh 
RUN conda --version

0
投票

如果你想在没有root权限的情况下安装miniconda:

RUN wget https://repo.continuum.io/miniconda/Miniconda3-4.5.4-Linux-x86_64.sh -O /tmp/miniconda.sh
RUN /bin/bash /tmp/miniconda.sh -b -p /opt/conda && \
    rm /tmp/miniconda.sh && \
    echo "export PATH=/opt/conda/bin:$PATH" > /etc/profile.d/conda.sh
ENV PATH /opt/conda/bin:$PATH

注意:将 Miniconda3-x.x.x 版本替换为您需要的版本


-4
投票

@soren——你必须运行

$CONDA_BIN/conda init
。重新启动一个新的外壳。那么
conda activate
应该可以工作。 conda init 更新您的登录配置文件以在登录时设置 conda(或采购,例如 .bashrc)

© www.soinside.com 2019 - 2024. All rights reserved.