I need to activate environment in docker and run a command in this environment. I create the environment, but then I try to activate this environment and run the command in this way:
CMD [ "source activate mro_env && ipython kernel install --user --name=mro_env" ]
but when I ran docker I get an error:
[FATAL tini (8)] exec source activate mro_env && ipython kernel install
--user --name=mro_env failed: No such file or directory
This is the whole Dockerfile:
FROM continuumio/miniconda3
ADD /src/mro_env.yml /src/mro_env.yml
RUN conda env create -f /src/mro_env.yml
# Pull the environment name out of the mro_env.yml
RUN echo "source activate $(head -1 /src/mro_env.yml | cut -d' ' -f2)" > ~/.bashrc
ENV PATH /opt/conda/envs/$(head -1 /src/mro_env.yml | cut -d' ' -f2)/bin:$PATH
CMD [ "source activate mro_env && ipython kernel install --user --name=mro_env" ]
To activate your Conda environment, type source activate <yourenvironmentname> . Note that conda activate will not work on Discovery with this version. To install a specific package, type conda install -n <yourenvironmentname> [package] . To deactivate the current, active Conda environment, type conda deactivate .
The Conda packaging tool implements environments, that enable different applications to have different libraries installed. So when you're building a Docker image for a Conda-based application, you'll need to activate a Conda environment.
With conda, you can create, export, list, remove, and update environments that have different versions of Python and/or packages installed in them. Switching or moving between environments is called activating the environment. You can also share an environment file.
Followed this tutorial and it worked. Example Dockerfile:
FROM continuumio/miniconda
WORKDIR /usr/src/app
COPY ./ ./
RUN conda env create -f environment.yml
# Make RUN commands use the new environment:
SHELL ["conda", "run", "-n", "myenv", "/bin/bash", "-c"]
EXPOSE 5003
# The code to run when container is started:
ENTRYPOINT ["conda", "run", "-n", "myenv", "python3", "src/server.py"]
Update:
You can use "conda run --no-capture-output" to not buffer IO if you use the 4.9 version of conda. Updated Dockerfile:
FROM continuumio/miniconda
WORKDIR /usr/src/app
COPY ./ ./
RUN conda env create -f environment.yml
# Make RUN commands use the new environment:
SHELL ["conda", "run", "--no-capture-output", "-n", "myenv", "/bin/bash", "-c"]
EXPOSE 5003
# The code to run when container is started:
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "myenv", "python3", "src/server.py"]
You can set CONDA_DEFAULT_ENV
Like this:
FROM continuumio/miniconda3
ARG conda_env=mro_env
ADD /src/environment.yml /src/environment.yml
RUN conda env create -f /src/environment.yml
ENV PATH /opt/conda/envs/$conda_env/bin:$PATH
ENV CONDA_DEFAULT_ENV $conda_env
CMD [ "python", "test.py" ]
UPDATE:
Better use activate. Work for me:
FROM continuumio/miniconda3
ADD /src/environment.yml /src/environment.yml
RUN conda env create -f /src/environment.yml
ENV PATH /opt/conda/envs/mro_env/bin:$PATH
RUN /bin/bash -c "source activate mro_env"
CMD [ "python", "test.py" ]
For me, the solution introduced here worked seemlessly:
FROM continuumio/miniconda3
RUN conda create -n env python=3.6
RUN echo "source activate env" > ~/.bashrc
ENV PATH /opt/conda/envs/env/bin:$PATH
As the user merv points out in one of the comments above (sorry don't have enough rep to vote up the comment) conda run
buffers all stdout/stderr, thus making it not workable for applications interacting or even just displaying logs over I/O.
I noticed there was no accepted answer, so I just post what has worked very well for me:
You can use an entrypoint script to activate the conda enviroment and let it take over further input from the Dockerfile such that the python script can be executed within the activated conda environment
Example Dockerfile:
FROM continuumio/miniconda3
# make docker use bash instead of sh
SHELL ["/bin/bash", "--login", "-c"]
# copy all necessary files
COPY environment.yml .
COPY ownchain/mypyscript.py .
COPY entrypoint.sh /usr/local/bin/
# make entrypoint script executable
RUN chmod u+x /usr/local/bin/entrypoint.sh
# create environment
RUN conda env create -f environment.yml
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["python", "mypyscript.py"]
Where the entrypoint.sh looks like this:
#!/bin/bash --login
set -e
# activate conda environment and let the following process take over
conda activate myenv
exec "$@"
All credit to David R. Pugh from this post that has more details, in particular with regards to Jupyter.
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