Home > Mobile >  Is there an elegant way to have conda install a 3rd party golang package during a docker build?
Is there an elegant way to have conda install a 3rd party golang package during a docker build?

Time:10-07

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?

CodePudding user response:

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.

  • Related