Activate and switch Anaconda environment in Dockerfile during build

后端 未结 3 1569
走了就别回头了
走了就别回头了 2020-12-09 10:47

I\'ve been trying for hours and can\'t figure out how to activate and switch anaconda environments in a Dockerfile during the build process

Here\'s the initial code:

3条回答
  •  生来不讨喜
    2020-12-09 11:03

    I haven't tested it with the nvidia image, but multi-stage Docker builds should help you out, which will probably look something like:

    # get Miniconda docker image to get a installed conda env; WARNING: That image is Debian based
    FROM continuumio/miniconda3:latest AS miniconda
    
    
    # your Docker commands
    FROM nvidia/cuda:10.1-cudnn7-devel-ubuntu16.04
    
    # Set user
    ENV SETUSER myuser
    
    RUN useradd -m $SETUSER
    USER $SETUSER
    WORKDIR /home/$SETUSER
    
    
    # Miniconda: get necessary files from build
    COPY --from=miniconda /opt/conda /opt/conda
    # Set correct permissions
    RUN chown -R $SETUSER: /opt/conda
    #   New terminals will have conda active
    # If nvidia's Docker image has no .bashrc
    # COPY --from=miniconda /home/$SETUSER/.bashrc
    # else
    RUN echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
        echo "conda activate base" >> ~/.bashrc
    
    # switch shell sh (default in Linux) to bash
    SHELL ["/bin/bash", "-c"]
    
    # give bash access to Anaconda, then normal anaconda commands, e.g. (-q: quiet, -y: answer yes)
    RUN source /home/$SETUSER/.bashrc \
     && conda create -q --name testy \
     && conda activate testy \
     && conda install -y your_package
    

    Inspiration from this GitHub issue: https://github.com/ContinuumIO/docker-images/issues/89

提交回复
热议问题