Wednesday, 16 November 2022

How to install conda and environment inside a Dockerfile?

step1 - Dockerfile:-

FROM xxx/client_xxx


#

# Install Miniconda in /opt/conda

#


ENV PATH /opt/conda/bin:$PATH


RUN apt-get update --fix-missing && \

    apt-get install -y wget bzip2 ca-certificates curl git && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*


RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-py39_4.11.0-Linux-x86_64.sh -O ~/miniconda.sh && \

    /bin/bash ~/miniconda.sh -b -p /opt/conda && \

    rm ~/miniconda.sh && \

    /opt/conda/bin/conda clean -tipsy && \

    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \

    echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \

    echo "conda activate base" >> ~/.bashrc


ENV PATH /opt/conda/bin:$PATH

ENV LD_LIBRARY_PATH /usr/local/cuda-11.1/lib64:/usr/local/cuda-11.1/extras/CUPTI/lib64:$LD_LIBRARY_PATH


step1 - build.sh:-

docker build . -t hello:world


step2 - Dockerfile:-

FROM hello:world


#

# create conda environment

#

COPY env.yml .

RUN conda env create -f env.yml

RUN echo "completed setup conda..."


#

# copy the source code

#

WORKDIR /data

COPY code ./code


ENV PATH /opt/conda/bin:$PATH

ENV LD_LIBRARY_PATH /usr/local/cuda-11.1/lib64:/usr/local/cuda-11.1/extras/CUPTI/lib64:$LD_LIBRARY_PATH


# Override default shell and use bash

WORKDIR /data/code


step2 - build.sh:-

docker build . -t hello:world


step3 - inside docker, put a run.sh:-

eval "$(conda shell.bash hook)"

conda activate ninja

cd /data/code

python main.py

conda deactivate


reference:-
https://pythonspeed.com/articles/activate-conda-dockerfile/

No comments:

Post a Comment