Run conda from virtual environment inside Docker and start Jupyterlab (with trading and ML libraries)

Here we will create docker image that is using miniconda and jupyterlab as our development environment for Machine Learning tasks and stock trading technical analysis.

The centos 8 image is using Python 3.7 by default, but to install ta-lib library we needed to create virtual environment with Python 3.5. Conda is having issues activating within the same shell, so workaround described in the Dockerfile needed to be used (using conda run -n env_name).

Dockerfile

# custom miniconda build that contains only libraries that we really need
# we can specify for example numpy, pandas, matplotlib ...
# maintainer: tcoil.info
# 
# build as
# sudo docker build -t custom_miniconda .
#
# run this image as
# coil@coil:~/Desktop/miniconda_docker_build$ sudo docker run --name custom_miniconda -i -t -p 8888:8888 -v "${PWD}:/notebooks" custom_miniconda
# or with docker compose demonized

FROM centos:8

RUN yum update -y
RUN yum install -y wget

RUN wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh

# install in batch (silent) mode, does not edit PATH or .bashrc or .bash_profile
# -p path
# -f force
RUN bash Miniconda3-latest-Linux-x86_64.sh -b

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

#RUN source /root/.bashrc
#RUN source /root/.bash_profile

# cleanup
RUN rm Miniconda3-latest-Linux-x86_64.sh

# create directory for notebooks
RUN mkdir /notebooks
WORKDIR /notebooks
COPY . /notebooks/

##############################################################
RUN conda create -n trading_env python=3.6 pip
SHELL ["conda", "run", "-n", "trading_env", "/bin/bash", "-c"]

# should work as well
#  conda init bash
#  source ~/.bashrc
#  conda activate trading_env

RUN python --version
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

######RUN pip install --upgrade mplfinance

RUN conda update -y --all
RUN conda list
RUN conda install -c quantopian ta-lib
###############RUN conda install -c conda-forge jupyterlab

# experimental
####RUN conda install -y -c plotly plotly
####RUN conda install -y -c plotly chart-studio
####RUN conda install jupyterlab "ipywidgets=7.5"
RUN conda install -c conda-forge mplfinance
RUN conda install -c anaconda pandas-datareader

# have everything updated
RUN conda update -y --all

EXPOSE 8888

# start the jupyter notebook in server mode
CMD ["conda", "run", "-n", "trading_env", "jupyter","notebook","--ip=0.0.0.0","--port=8888","--no-browser","--allow-root", "--notebook-dir=/notebooks"]

# alternatively start the jupyter-lab notebook in server mode
#CMD ["conda", "run", "-n", "trading_env", "jupyter-lab","--ip=0.0.0.0","--port=8888","--no-browser","--allow-root", "--notebook-dir=/notebooks"]

requirements.txt

cython
yfinance
jupyterlab
jupyter

How to run:

sudo docker build -t custom_miniconda .
docker run --name custom_miniconda -i -t -p 8888:8888 -v "${PWD}:/notebooks" custom_miniconda 

and in separate terminal get the sign in token:

docker exec -it custom_miniconda conda run -n trading_env jupyter notebook list

Source: