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"]
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.
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).
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.
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.
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With