Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate a conda environment before running Docker commands

I'm creating a docker image with, inside, a conda environment myEnv using a Dockerfile.

I would like, when running the docker image with

docker run -it myDockerImage

to get to a bash terminal with the environment already activated. I know we can pass variables and commands to docker run but I would like it to be done automatically.

I tried adding the following variants to the end of the Dockerfile but nothing seems to work:

CMD ["source /root/miniconda/bin/activate myEnv"]
CMD [".", "/root/miniconda/bin/activate", "myEnv"]
CMD ["source /root/miniconda/bin/activate myEnv; /bin/bash"]
like image 543
BiBi Avatar asked Apr 03 '19 12:04

BiBi


People also ask

How do I activate an existing conda environment?

You activate (deactivate) an environment using the conda activate ( conda deactivate ) commands. You install packages into environments using conda install ; you install packages into an active environment using pip install . Use the conda env list command to list existing environments and their respective locations.

What is the difference between conda activate and source activate?

Generally, you won't find too much of a difference between conda activate and the old source activate , except that it's meant to be faster, and work the same across different operating systems (the latter difference makes conda activate a huge improvement IMO).

How do I enable conda base in terminal?

If the environment is not activated, in your Terminal window or an Anaconda Prompt, run: conda list -n myenv. If the environment is activated, in your Terminal window or an Anaconda Prompt, run: conda list.

How does conda activate work?

activate . This function creates an instance of the Activator class (defined in the same file) and runs the execute method. The execute method processes the arguments and stores the passed environment name into an instance variable, then decides that the activate command has been passed, so it runs the activate method.


1 Answers

Do this with an ENTRYPOINT in your Dockerfile.

src/entrypoint.sh

#!/bin/bash

# enable conda for this shell
. /opt/conda/etc/profile.d/conda.sh

# activate the environment
conda activate my_environment

# exec the cmd/command in this process, making it pid 1
exec "$@"

src/Dockerfile

# ...
COPY ./entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
like image 116
Arseniy Banayev Avatar answered Oct 21 '22 14:10

Arseniy Banayev