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

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

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

  1. FROM continuumio/miniconda3
  2. ARG ENV_NAME
  3. SHELL ["/bin/bash","-l", "-c"]
  4. WORKDIR /root/
  5. # Install Miniconda
  6. RUN /opt/conda/bin/conda init bash && \
  7. /opt/conda/bin/conda config --add channels jolespin && \
  8. /opt/conda/bin/conda config --add channels bioconda && \
  9. /opt/conda/bin/conda config --add channels conda-forge && \
  10. /opt/conda/bin/conda update conda -y && \
  11. /opt/conda/bin/conda clean -afy
  12. # =================================
  13. # Add conda bin to path
  14. ENV PATH /opt/conda/bin:$PATH
  15. # Create environment
  16. RUN conda create -n ${ENV_NAME} -c bioconda seqkit -y
  17. # Activate environment
  18. RUN conda activate ${ENV_NAME}
  19. # ENTRYPOINT ["/bin/bash", "-c", "source activate ${ENV_NAME} && exec /bin/bash"]

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

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

问题:

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

  1. (base) jespinozlt2-osx:docker jespinoz$ docker run -it test bash
  2. (base) root@19dcf0f9570f:~# echo $CONDA_PREFIX
  3. /opt/conda
  4. (base) root@19dcf0f9570f:~# conda activate test_env
  5. (test_env) root@19dcf0f9570f:~# echo $CONDA_PREFIX
  6. /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:

  1. FROM continuumio/miniconda3
  2. ARG ENV_NAME
  3. SHELL ["/bin/bash","-l", "-c"]
  4. WORKDIR /root/
  5. # Install Miniconda
  6. RUN /opt/conda/bin/conda init bash && \
  7. /opt/conda/bin/conda config --add channels jolespin && \
  8. /opt/conda/bin/conda config --add channels bioconda && \
  9. /opt/conda/bin/conda config --add channels conda-forge && \
  10. /opt/conda/bin/conda update conda -y && \
  11. /opt/conda/bin/conda clean -afy
  12. # =================================
  13. # Add conda bin to path
  14. ENV PATH /opt/conda/bin:$PATH
  15. # Create environment
  16. RUN conda create -n ${ENV_NAME} -c bioconda seqkit -y
  17. # Activate environment
  18. RUN conda activate ${ENV_NAME}
  19. # ENTRYPOINT ["/bin/bash", "-c", "source activate ${ENV_NAME} && exec /bin/bash"]

Here is my command to build the docker image:

  1. 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?

  1. (base) jespinozlt2-osx:docker jespinoz$ docker run -it test bash
  2. (base) root@19dcf0f9570f:~# echo $CONDA_PREFIX
  3. /opt/conda
  4. (base) root@19dcf0f9570f:~# conda activate test_env
  5. (test_env) root@19dcf0f9570f:~# echo $CONDA_PREFIX
  6. /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

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

  1. 我解决了:
  2. # =================================
  3. # Miniconda3
  4. # =================================
  5. continuumio/miniconda3
  6. ARG ENV_NAME
  7. SHELL ["/bin/bash", "-l", "-c"]
  8. 工作目录 /root/
  9. # 安装 Miniconda
  10. RUN /opt/conda/bin/conda init bash && \
  11. /opt/conda/bin/conda config --add channels jolespin && \
  12. /opt/conda/bin/conda config --add channels bioconda && \
  13. /opt/conda/bin/conda config --add channels conda-forge && \
  14. /opt/conda/bin/conda update conda -y && \
  15. /opt/conda/bin/conda install -c conda-forge mamba -y && \
  16. /opt/conda/bin/conda clean -afy
  17. # =================================
  18. # 将 conda bin 添加到路径
  19. ENV PATH /opt/conda/bin:$PATH
  20. # 创建环境
  21. RUN mamba create -n ${ENV_NAME} -c bioconda seqkit -y
  22. # 设置环境
  23. RUN echo "conda activate ${ENV_NAME}" >> ~/.bashrc
  24. # 设置入口点为 bash
  25. ENTRYPOINT ["bash", "-l", "-c"]

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

  1. docker run -it test -c "bash"

这是运行命令的方式:

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

I figured it out:

  1. # =================================
  2. # Miniconda3
  3. # =================================
  4. FROM continuumio/miniconda3
  5. ARG ENV_NAME
  6. SHELL ["/bin/bash","-l", "-c"]
  7. WORKDIR /root/
  8. # Install Miniconda
  9. RUN /opt/conda/bin/conda init bash && \
  10. /opt/conda/bin/conda config --add channels jolespin && \
  11. /opt/conda/bin/conda config --add channels bioconda && \
  12. /opt/conda/bin/conda config --add channels conda-forge && \
  13. /opt/conda/bin/conda update conda -y && \
  14. /opt/conda/bin/conda install -c conda-forge mamba -y && \
  15. /opt/conda/bin/conda clean -afy
  16. # =================================
  17. # Add conda bin to path
  18. ENV PATH /opt/conda/bin:$PATH
  19. # Create environment
  20. RUN mamba create -n ${ENV_NAME} -c bioconda seqkit -y
  21. # Set up environment
  22. RUN echo "conda activate ${ENV_NAME}" >> ~/.bashrc
  23. # Set entrypoint to bash
  24. ENTRYPOINT ["bash", "-l", "-c"]

Here's how you activate it interactively:

  1. docker run -it test -c "bash"

Here's how you run commands:

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

答案2

得分: 0

  1. Dockerfile的末尾添加以下内容:
  2. CMD ['conda', 'activate', '${ENV_NAME}']
  3. 你可以在这里查看*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

  1. 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:

确定