使用 Docker 激活 conda 时出现 CommandNotFoundError

CommandNotFoundError when activate conda with Docker

我是 Docker 的新手,尝试通过激活 conda 环境来构建 Docker 映像。但是我不能。

这是我的 Dockerfile:

FROM continuumio/miniconda3

COPY . /app

WORKDIR /app

RUN conda env create -f environment.yml

SHELL ["conda", "run", "-n", "FM", "/bin/bash", "-c"]

RUN conda activate FM

ENTRYPOINT ["conda", "run", "-n", "FM", "python", "src/run.py"]

我的 environment.yml 文件:

name: FM
channels:
  - defaults
dependencies:
  - _libgcc_mutex=0.1=main
  - ca-certificates=2020.6.24=0
  - certifi=2020.6.20=py37_0
  - ld_impl_linux-64=2.33.1=h53a641e_7
  - libedit=3.1.20191231=h7b6447c_0
  - libffi=3.2.1=hd88cf55_4
  - libgcc-ng=9.1.0=hdf63c60_0
  - libstdcxx-ng=9.1.0=hdf63c60_0
  - ncurses=6.2=he6710b0_1
  - openssl=1.1.1g=h7b6447c_0
  - pip=20.1.1=py37_1
  - python=3.7.6=h0371630_2
  ...
  - pip:
    - click==7.1.2
    - cycler==0.10.0
    - decorator==4.4.2
    - flask==1.1.2
    - imageio==2.9.0
    - itsdangerous==1.1.0
    ...
prefix: /home/huytran/miniconda3/envs/FM

当我构建图像 docker build -t condatest . 时,出现此错误:

...
Step 5/8 : SHELL ["conda", "run", "-n", "FM", "/bin/bash", "-c"]
 ---> Running in 962623c513cc
Removing intermediate container 962623c513cc
 ---> c5a91349830f
Step 6/8 : RUN conda activate FM
 ---> Running in 1f3d1cd0c50b

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'.

你们知道我哪里错了吗?

RUN conda activate FM 没有任何作用,因为它只在特定的 RUN 中激活。它对以后的 RUNENTRYPOINT/CMD 命令没有影响。

这也是不必要的,因为您在入口点使用 conda run 并使用 SHELL 启用环境 ala https://pythonspeed.com/articles/activate-conda-dockerfile/.

省略它就可以了。