英文:
Is there an elegant way to have conda install a 3rd party golang package during a docker build?
问题
我正在为软件开发准备一个带有Ubuntu v18.04的Docker镜像。我正在包含miniconda来管理开发环境,其中全部是用golang编写的。我使用一个YAML文件来创建环境:
RUN conda env create --file goDev.yml
我还希望在启动Docker时激活conda环境。这有点棘手,因为必须先初始化conda,但JaegerP在这里提供了一个不错的解决方法,涉及更新.bashrc文件(谢谢)。
不幸的是,我还需要将一个第三方的YAML包安装到golang中。我必须激活环境才能安装该包,所以这又让我回到了JaegerP帮助我克服的最初问题:在初始化之前无法激活环境,而在Docker构建过程中无法进行初始化,因为必须重新启动shell。
换句话说,这个方法很好用:
RUN conda env create --file goDev.yml \
&& rm goDev.yml \
&& echo "source /opt/conda/etc/profile.d/conda.sh" \
&& echo "conda activate go_dev" >> ${HOME}/.bashrc
所期望的conda环境在Docker启动时被激活,但不幸的是,外部的YAML包没有被安装。这个方法行不通,因为在初始化之前无法激活conda环境,而初始化又需要重新启动shell:
RUN conda env create --file goDev.yml \
&& rm goDev.yml \
&& conda init bash \
&& conda activate go_dev \
&& go get gopkg.in/yaml.v2 \
&& echo "source /opt/conda/etc/profile.d/conda.sh" \
&& echo "conda activate go_dev" >> ${HOME}/.bashrc
如果这个文件不存在,我可以进一步更新.bashrc来安装YAML包:
/root/go/pkg/mod/cache/download/gopkg.in/yaml.v2
是否有更优雅的解决方案,可以在Docker构建过程中安装第三方的golang包,而不是每次运行镜像时都检查它?
英文:
I’m preparing a docker image w/ Ubuntu v18.04 for s/w development. I’m including miniconda to manage the development environment, which is all golang. I create the environment with a YAML file:
RUN conda env create --file goDev.yml
I’d also like the conda environment to be activated when the docker is started. That’s a little tricky to do b/c conda has to be initialized first, but JaegerP provides a nice workaround here that involves updating .bashrc (thanks).
Unfortunately, I also need to install a third party YAML package to golang. I have to activate the environment to install the package, so it brings me back to the original problem JaegerP helped me overcome: I can’t activate the environment until its initialized, and I cannot initialize during the docker build b/c I have to restart the shell.
In other words, this works nicely:
> RUN conda env create --file goDev.yml
> && rm goDev.yml
> && echo "source /opt/conda/etc/profile.d/conda.sh"
> && echo "conda activate go_dev" >> ${HOME}/.bashrc
The desired conda environment is activated when the docker is started, unfortunately the external YAML package is not installed. This does not work b/c the conda environment can't be activated until it's initialized and initialization requires the shell to be restarted:
> RUN conda env create --file goDev.yml
> && rm goDev.yml
> && conda init bash
> && conda activate go_dev
> && go get gopkg.in/yaml.v2
> && echo "source /opt/conda/etc/profile.d/conda.sh"
> && echo "conda activate go_dev" >> ${HOME}/.bashrc
I could update .bashrc further to install the YAML package if this file doesn’t exist:
/root/go/pkg/mod/cache/download/gopkg.in/yaml.v2
Is there a more elegant solution that enables me to install a 3rd party golang package during the docker build instead of checking for it each time the image is run?
答案1
得分: 0
尝试使用Conda Run
conda run
函数提供了一种在环境上下文中运行代码的干净方式,无需手动激活环境。尝试像这样使用:
RUN conda env create --file goDev.yml
&& rm goDev.yml
&& conda run -n go_dev go get gopkg.in/yaml.v2
&& echo '. /opt/conda/etc/profile.d/conda.sh && conda activate go_dev' >> ${HOME}/.bashrc
此外,你可能希望在涉及 Conda 的所有 RUN 链后面添加以下代码,以减小 Docker 镜像层的大小:
conda clean -qafy
英文:
Try Conda Run
The conda run
function provides a clean way to run code within an environment context without having to manually activate. Try something like
RUN conda env create --file goDev.yml
&& rm goDev.yml
&& conda run -n go_dev go get gopkg.in/yaml.v2
&& echo '. /opt/conda/etc/profile.d/conda.sh && conda activate go_dev' >> ${HOME}/.bashrc
Also, you probably want to end all RUN chains involving Conda with
conda clean -qafy
to keep help minimize Docker image layer size.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论