Dockerfile:如何获取Anaconda [英] Dockerfile: How to source Anaconda

查看:184
本文介绍了Dockerfile:如何获取Anaconda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AWS Batch设置Anaconda环境

I am trying to set up an Anaconda environment with AWS Batch

这是我的Dockerfile中的摘录

Here is a snippet from my Dockerfile

#: Download Anaconda
COPY Anaconda3-2019.10-Linux-x86_64.sh /setup/
RUN bash Anaconda3-2019.10-Linux-x86_64.sh -b -p /home/ec2-user/anaconda3 
RUN echo 'export PATH=$PATH:/home/ec2-user/anaconda3/bin' >>~/.bashrc \
    && /bin/bash -c "source ~/.bashrc"
ENV PATH $PATH:/home/ec2-user/anaconda3/bin

# Create the environment:
COPY environment.yml .
RUN conda env create -f environment.yml

# Make RUN commands use the new environment:
SHELL ["conda", "run", "-n", "solver_env", "/bin/bash", "-c"]

当我访问容器并进行一些简单测试时,我得到一个错误消息

When I access my container, and do some simple tests I get an error that

conda: command not found

即使我已经找到了我的bashrc文件的路径.

Even though I have sourced the path to my bashrc file.

当尝试访问我的bashrc文件时,我也会得到

Also when trying to access my bashrc file I get

cat ~.bashrc: No such file or directory

cat: //.bashrc: No such file or directory

关于我可能在哪里出错/在哪里检查的任何想法?

Any ideas on where I may be going wrong/where to check?

我的Dockerfile中激活venv并启动脚本的最后一行是:

Last line in my Dockerfile to activate the venv and kick off the script is:

ENTRYPOINT ["conda", "run", "-n", "solver_env", "/bin/bash", "/usr/local/bin/fetch_and_run.sh"]

一旦构建了映像并将其部署到AWS ECR,我便启动了一个批处理作业,该作业实际上运行了此Shell脚本:

Once I have the image built and deployed to AWS ECR, I kick off a Batch job which essentially runs this shell script:

#!/bin/bash
date
echo "Args: $@"
env
echo "script_path: $1"
echo "script_name: $2"
echo "path_prefix: $3"
echo "jobID: $AWS_BATCH_JOB_ID"
echo "jobQueue: $AWS_BATCH_JQ_NAME"
echo "computeEnvironment: $AWS_BATCH_CE_NAME"
   
mkdir /tmp/scripts/
aws s3 cp $1 /tmp/scripts/$2
python3 /tmp/scripts/${@:2}

推荐答案

您似乎忘记了命令中的斜线:

You seem to have forgotten a slash in your command:

cat ~.bashrc: No such file or directory

应该是:

cat ~/.bashrc

使用Dockerfile中的 .bashrc 所做的所有事情都是没有意义的.不仅如此,正如@Itamar在其回答中指出的那样, .bashrc 仅在交互式非登录Shells ,但是每个 RUN 命令都使用其自己的Bash进程,甚至可以使用单独的进程手动进行源化,这实际上意味着您要启动Bash,源为.bashrc ,然后丢弃该Bash实例,并丢弃所有与之相关的更改.

Also everything you do with .bashrc in your Dockerfile is pointless. Not only, as @Itamar states in his answer is .bashrc only executed in interactive non-login shells, but every RUN command uses its own Bash process and you even use a separate process to source it manually, which essentially means you start Bash, source .bashrc and then discard that instance of Bash, discarding all changes with it.

我在 SuperUser 上回答了类似的问题,并且该解决方案也适用于此.实际上,它更简单,因为您想将Docker映像与Docker一起使用,而不是与Singularity一起使用,并且您希望以root用户身份运行.

I have answered a similar question on SuperUser and the solution applies here as well. It's actually simpler, since you want to use the Docker image with Docker and not with Singularity and you want to run is as root.

COPY Anaconda3-2019.10-Linux-x86_64.sh /setup/Anaconda3-2019.10-Linux-x86_64.sh
COPY environment.yml /setup/environment.yml
RUN bash /setup/Anaconda3-2019.10-Linux-x86_64.sh -b -p /home/ec2-user/anaconda3 && \
    rm /setup/Anaconda3-2019.10-Linux-x86_64.sh && \
    ln -s /home/ec2-user/anaconda3/bin/conda /usr/bin/conda && \
    conda env create -f /setup/environment.yml && \
    rm /setup/environment.yml

CMD ["conda", "run", "-n", "solver_env", "/bin/bash"]

如果要在创建环境后运行更多的conda命令,则应将它们附加到现有的 RUN 指令中,如下所示:

If you want to run some more conda commands after creating the environment you should append them to the existing RUN instruction, like so:

COPY Anaconda3-2019.10-Linux-x86_64.sh /setup/Anaconda3-2019.10-Linux-x86_64.sh
COPY environment.yml /setup/environment.yml
RUN bash /setup/Anaconda3-2019.10-Linux-x86_64.sh -b -p /home/ec2-user/anaconda3 && \
    rm /setup/Anaconda3-2019.10-Linux-x86_64.sh && \
    ln -s /home/ec2-user/anaconda3/bin/conda /usr/bin/conda && \
    conda env create -f /setup/environment.yml && \
    rm /setup/environment.yml && \
    conda run -n solver_env python -V

CMD ["conda", "run", "-n", "solver_env", "/bin/bash"]

这篇关于Dockerfile:如何获取Anaconda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆