如何使Docker容器自动激活conda环境?

huangapple go评论71阅读模式
英文:

How to make Docker container automatically activate a conda environment?

问题

I'm working on dockerizing a conda environment so I've created a toy example. My main goal is to end up with the following:

  • 一个 Docker 镜像
  • 一个 Docker 容器,可以自动加载 conda 环境
  • 一个 Docker 容器,可以接受 conda 环境中可执行程序的参数(例如,docker run [some arguments] "seqkit -h"
  • 如果未提供参数,则一个 Docker 容器将以交互方式运行,但将自动进入 conda 环境。

为了简化,我已经创建了一个 Docker 容器,通过 conda 安装了 seqkit

这是我的 Dockerfile

FROM continuumio/miniconda3

ARG ENV_NAME

SHELL ["/bin/bash","-l", "-c"]

WORKDIR /root/

# Install Miniconda
RUN /opt/conda/bin/conda init bash && \
    /opt/conda/bin/conda config --add channels jolespin && \
    /opt/conda/bin/conda config --add channels bioconda && \
    /opt/conda/bin/conda config --add channels conda-forge && \
    /opt/conda/bin/conda update conda -y && \
    /opt/conda/bin/conda clean -afy

# =================================

# Add conda bin to path
ENV PATH /opt/conda/bin:$PATH

# Create environment
RUN conda create -n ${ENV_NAME} -c bioconda seqkit -y

# Activate environment
RUN conda activate ${ENV_NAME}

# ENTRYPOINT ["/bin/bash", "-c", "source activate ${ENV_NAME} && exec /bin/bash"]

这是我构建 Docker 镜像的命令:

docker build --build-arg ENV_NAME=test_env -t test -f Dockerfile-test .

问题:

1. 当我运行容器时,它不会自动激活 test_env 环境。如何使得当我运行我的 Docker 镜像以构建一个容器时,无论给出的命令如何,它都会自动激活 test_env

(base) jespinozlt2-osx:docker jespinoz$ docker run -it test bash
(base) root@19dcf0f9570f:~# echo $CONDA_PREFIX
/opt/conda
(base) root@19dcf0f9570f:~# conda activate test_env
(test_env) root@19dcf0f9570f:~# echo $CONDA_PREFIX
/opt/conda/envs/test_env

2. 一旦环境被激活,我如何给它发送命令?我想要运行的测试命令只是 seqkit -h

英文:

I'm working on dockerizing a conda environment so I've created a toy example. My main goal is to end up with the following:

  • A docker image
  • A docker container that automatically loads the conda environment
  • A docker container that can accept arguments for executables in the conda environment (e.g., docker run [some arguments] "seqkit -h"
  • A docker container that will be interactive if no arguments are given but will automatically be in the conda environment.

For simplicity, I've created a docker container that installs seqkit through conda.

Here is my Dockerfile:

FROM continuumio/miniconda3

ARG ENV_NAME

SHELL ["/bin/bash","-l", "-c"]

WORKDIR /root/

# Install Miniconda
RUN /opt/conda/bin/conda init bash && \
    /opt/conda/bin/conda config --add channels jolespin && \
    /opt/conda/bin/conda config --add channels bioconda && \
    /opt/conda/bin/conda config --add channels conda-forge && \
    /opt/conda/bin/conda update conda -y && \
    /opt/conda/bin/conda clean -afy

# =================================

# Add conda bin to path
ENV PATH /opt/conda/bin:$PATH

# Create environment
RUN conda create -n ${ENV_NAME} -c bioconda seqkit -y

# Activate environment
RUN conda activate ${ENV_NAME}

# ENTRYPOINT ["/bin/bash", "-c", "source activate ${ENV_NAME} && exec /bin/bash"]

Here is my command to build the docker image:

docker build --build-arg ENV_NAME=test_env -t test -f Dockerfile-test .

Issues:

1. When I run the container, it does not activate the test_env environment automatically. How can I make it so when I run my docker image to build a container it automatically activates test_env regardless of the commands given?

(base) jespinozlt2-osx:docker jespinoz$ docker run -it test bash
(base) root@19dcf0f9570f:~# echo $CONDA_PREFIX
/opt/conda
(base) root@19dcf0f9570f:~# conda activate test_env
(test_env) root@19dcf0f9570f:~# echo $CONDA_PREFIX
/opt/conda/envs/test_env

2. How am I able to give it commands once the environment is activated? My test command I want to run is just seqkit -h

答案1

得分: 0

我已经翻译好了代码部分:

我解决了:

# =================================
# Miniconda3
# =================================

从 continuumio/miniconda3

ARG ENV_NAME

SHELL ["/bin/bash", "-l", "-c"]

工作目录 /root/

# 安装 Miniconda
RUN /opt/conda/bin/conda init bash && \
    /opt/conda/bin/conda config --add channels jolespin && \
    /opt/conda/bin/conda config --add channels bioconda && \
    /opt/conda/bin/conda config --add channels conda-forge && \
    /opt/conda/bin/conda update conda -y && \
    /opt/conda/bin/conda install -c conda-forge mamba -y && \
    /opt/conda/bin/conda clean -afy

# =================================

# 将 conda bin 添加到路径
ENV PATH /opt/conda/bin:$PATH

# 创建环境
RUN mamba create -n ${ENV_NAME} -c bioconda seqkit -y

# 设置环境
RUN echo "conda activate ${ENV_NAME}" >> ~/.bashrc

# 设置入口点为 bash
ENTRYPOINT ["bash", "-l", "-c"]

这是交互式激活它的方式:

docker run -it test -c "bash"

这是运行命令的方式:

docker run -it test -c "seqkit -h"
英文:

I figured it out:

# =================================
# Miniconda3
# =================================

FROM continuumio/miniconda3

ARG ENV_NAME

SHELL ["/bin/bash","-l", "-c"]

WORKDIR /root/

# Install Miniconda
RUN /opt/conda/bin/conda init bash && \
    /opt/conda/bin/conda config --add channels jolespin && \
    /opt/conda/bin/conda config --add channels bioconda && \
    /opt/conda/bin/conda config --add channels conda-forge && \
    /opt/conda/bin/conda update conda -y && \
    /opt/conda/bin/conda install -c conda-forge mamba -y && \
    /opt/conda/bin/conda clean -afy

# =================================

# Add conda bin to path
ENV PATH /opt/conda/bin:$PATH

# Create environment
RUN mamba create -n ${ENV_NAME} -c bioconda seqkit -y

# Set up environment
RUN echo "conda activate ${ENV_NAME}" >> ~/.bashrc

# Set entrypoint to bash
ENTRYPOINT ["bash", "-l", "-c"]

Here's how you activate it interactively:

docker run -it test  -c "bash"

Here's how you run commands:

docker run -it test  -c "seqkit -h"

答案2

得分: 0

在Dockerfile的末尾添加以下内容:

CMD ['conda', 'activate', '${ENV_NAME}']

你可以在这里查看*RUN*和*CMD*命令的区别:[链接](https://stackoverflow.com/questions/37461868/difference-between-run-and-cmd-in-a-dockerfile#:~:text=RUN%20is%20an%20image%20build,you%20launch%20the%20built%20image.)
英文:

You could add

CMD ['conda', 'activate', '${ENV_NAME}']

at the end of the Dockerfile.

You can look at the difference between RUN and CMD commands here

huangapple
  • 本文由 发表于 2023年5月10日 13:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定