maciek
maciek

Reputation: 2117

How to install packages with miniconda in Dockerfile?

I have a simple Dockerfile:

FROM ubuntu:18.04

RUN apt-get update

RUN apt-get install -y wget && rm -rf /var/lib/apt/lists/*

RUN wget \
    https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && mkdir /root/.conda \
    && bash Miniconda3-latest-Linux-x86_64.sh -b \
    && rm -f Miniconda3-latest-Linux-x86_64.sh \
    && echo PATH="/root/miniconda3/bin":$PATH >> .bashrc \
    && exec bash \
    && conda --version

RUN conda --version

And it cannot be built. At the very last step I get /bin/sh: 1: conda: not found....
The first appearance of conda --version did not raise an error which makes me wonder is that an PATH problem?
I would like to have another RUN entry in this Dockerfile in which I would install packages with conda install ...
At the end I want to have CMD ["bash", "test.py"] entry so that when in do docker run this image it automatically runs a simple python script that imports all the libraries installed with conda. Maybe also a CMD ["bash", "test.sh"] script that would test if conda and python interpreter are indeed installed.

This is a simplified example, there will be a lot of software so I do not want to change the base image.

Upvotes: 57

Views: 73226

Answers (3)

LinPy
LinPy

Reputation: 18608

This will work using ARG and ENV:

FROM ubuntu:18.04

ENV PATH="/root/miniconda3/bin:${PATH}"
ARG PATH="/root/miniconda3/bin:${PATH}"

# Install wget to fetch Miniconda
RUN apt-get update && \
    apt-get install -y wget && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Install Miniconda on x86 or ARM platforms
RUN arch=$(uname -m) && \
    if [ "$arch" = "x86_64" ]; then \
    MINICONDA_URL="https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh"; \
    elif [ "$arch" = "aarch64" ]; then \
    MINICONDA_URL="https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh"; \
    else \
    echo "Unsupported architecture: $arch"; \
    exit 1; \
    fi && \
    wget $MINICONDA_URL -O miniconda.sh && \
    mkdir -p /root/.conda && \
    bash miniconda.sh -b -p /root/miniconda3 && \
    rm -f miniconda.sh

RUN conda --version

Upvotes: 108

pfRodenas
pfRodenas

Reputation: 347

If you want to install miniconda without being root:

RUN wget https://repo.continuum.io/miniconda/Miniconda3-4.5.4-Linux-x86_64.sh -O /tmp/miniconda.sh
RUN /bin/bash /tmp/miniconda.sh -b -p /opt/conda && \
    rm /tmp/miniconda.sh && \
    echo "export PATH=/opt/conda/bin:$PATH" > /etc/profile.d/conda.sh
ENV PATH /opt/conda/bin:$PATH

Note: replace version Miniconda3-x.x.x with the one that you need

Upvotes: 1

iamh2o
iamh2o

Reputation: 11

@soren -- you must run $CONDA_BIN/conda init. restart a new shell. then conda activate should work. conda init updates your shall login profiles to setup conda upon login (or sourcing, say .bashrc)

Upvotes: -5

Related Questions