dockerfile 中激活conda并安装package

遇错的dockerfile写法:

bash 复制代码
WORKDIR /tmp
RUN wget "https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh" -O ~/miniconda.sh \
    && bash ~/miniconda.sh -b -p $HOME/miniconda \
    && ~/miniconda/bin/conda init $(echo /bin/bash | awk -F '/' '{print $NF}')

RUN echo yes | $HOME/miniconda/bin/conda create -n textgen python=3.10.9 \
    && $HOME/miniconda/bin/conda activate textgen \
    && pip install git+https://github.com/huggingface/transformers.git@9eae4aa57650c1dbe1becd4e0979f6ad1e572ac0 \
    && pip install SentencePiece

遇到的错误:

bash 复制代码
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

解决办法:

bash 复制代码
WORKDIR /tmp
RUN wget "https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh" -O ~/miniconda.sh \
    && bash ~/miniconda.sh -b -p $HOME/miniconda \
    && ~/miniconda/bin/conda init bash
ENV PATH=/root/miniconda/bin:$PATH
RUN echo yes | conda create -n textgen python=3.10.9
SHELL ["conda", "run", "-n", "textgen", "/bin/bash", "-c"]
RUN python --version && pip install git+https://github.com/huggingface/transformers.git@9eae4aa57650c1dbe1becd4e0979f6ad1e572ac0 \
    && pip install SentencePiece
SHELL ["/bin/bash", "-c"]

参考:Activating a Conda environment in your Dockerfile (pythonspeed.com)

相关推荐
文洪涛1 天前
VS Code Python “第一次运行失败 / 先执行 python 再激活 Conda” 问题定位与解决
开发语言·python·conda
schinber2 天前
Python包管理工具全景指南:从pip到现代化工具实战
python·conda·pip·uv
colourmind2 天前
记录一次vscode debug conda python 使用报错问题排查
vscode·python·conda
Studying 开龙wu3 天前
Linux 系统中配置国内源下载时使用pip install 和conda install哪个快?
linux·conda·pip
KevinShi_BJ3 天前
Conda env & python 的包管理器
conda
Echo_NGC22373 天前
【传统JSCC+Deep JSCC】联合信源信道编码完全指南
人工智能·python·深度学习·神经网络·conda·无人机·jscc
多想和从前一样3 天前
2025【最新】基于conda环境从头安装MMSegmentation
conda·pip·mmsegmentation
NicoNicoleNi4 天前
Anaconda安装和环境配置实践记录
python·conda
sulikey5 天前
Conda下载依赖/包报错:Connection broken: IncompleteRead(...)
conda·connectbroken·incompleteread·网络中断
kkoral5 天前
基于MS-Swift 为 Qwen3-0.6B-Base 模型搭建可直接调用的 API 服务
python·conda·fastapi·swift