
FROM continuumio/miniconda3:4.12.0
# 2022/06/13 - debian amd64

# - # 
ARG workdir=/app
WORKDIR ${workdir}
#
ARG myenv=100_prisoners_problem_streamlit_app
# python major.minor.micro versions
# ARG py_version="3.8.10"
ARG py_version_minor="3.10"
ARG pkg_dir="/opt/conda/envs/${myenv}/lib/python${py_version_minor}/site-packages" 

# - # Install Linux System Dependencies
# RUN apt-get update \
#     && apt-get -y install xvfb libfontconfig wkhtmltopdf

# - # Create environment with specified Python version and Pip
# RUN conda create --name=${myenv} python=${py_version} pip

# - # Create environment from conda environment.yml file
# File generated by: "conda env export > environment.yml"
# platform-free file:  "conda env export > environment.yml --no-builds" 
# platform-free file / only the specified by the user:
#   "conda env export > environment.yml --from-history" 
# https://stackoverflow.com/a/39299669/13684783
# NOTE: Recommendation: manually create an environment.yml file and specify
# or pin only the dependencies that you care about, since a .yml file create
# in one system most likely will not work in other.
# NOTE: Importance in setting strict priority to increase speed
# https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-channels.html#strict-channel-priority
# Ways to set priority:
# https://stackoverflow.com/a/50671425
ARG env_yml="environment_manual.yml"
COPY ${env_yml} ${workdir} 
# NOTE: set priority in an .yml file using this trick:
# https://stackoverflow.com/a/65983247/13684783
RUN conda install mamba --name=base -c conda-forge \
    && mamba env create --name=${myenv} --file=${env_yml} \
    # Shrink Docker Image Size:
    # https://jcristharif.com/conda-docker-tips.html
    && conda clean --yes --all -f \
    && find /opt/conda/ -follow -type f -name '*.a' -delete \
    && find /opt/conda/ -follow -type f -name '*.pyc' -delete \
    && find /opt/conda/ -follow -type f -name '*.js.map' -delete 

# - # Create environment from pip requirements.txt
# File generated by: "pip freeze > requirements.txt"
# RUN pip install --no-cache-dir -r requirements.txt 

# REVIEW: It is very hard to enable using 'conda activate' and other conda
# commands inside docker. I tried most things but they didn't work. Therefore,
# my work around was to specify the environment and path to install the
# packages.
# Another workaround is to use just the 'base' conda environment.
# https://github.com/conda/conda/issues/7980#issuecomment-441358406
# https://stackoverflow.com/a/62737129/13684783
# Add miniconda to system path
ENV PATH /opt/conda/envs/${myenv}/bin:$PATH

# - # Install packages manually via Conda
# RUN conda install --yes --freeze-installed \
#     --name=${myenv} --channel=conda-forge \
#    # this reduces size: use openblas instead of mkl (unavailable on Windows)
#    # nomkl \ 
#    numpy==1.21.1 

# - # Install packages manually via Pip
# RUN pip install --target=${pkg_dir} --no-cache-dir --no-input \
#     numpy==1.21.2 --upgrade

# - # Copy all files from current directory to docker /app
COPY . ${workdir}

# - # This will activate the environment (> overwrites, >> appends)
# NOTE: However, just when the container is created, not right now.
# NOTE: 'conda activate' does not work, you have to use 'source activate'
RUN echo "source activate ${myenv}" > ~/.bashrc
# RUN echo "source activate base" > ~/.bashrc

# - # streamlit-specific commands for config
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
#
RUN mkdir -p /root/.streamlit
#
RUN bash -c 'echo -e "\
[general]\n\
email = \"\"\n\
" > /root/.streamlit/credentials.toml'
#
RUN bash -c 'echo -e "\
[server]\n\
enableCORS = false\n\
" > /root/.streamlit/config.toml'
#
EXPOSE 8501

# - # Switch shell sh to bash (default in Linux)
# SHELL ["/bin/bash", "-c"]

# - # Set a command to run when the container is started
# NOTE: This might be handy, however if I set CMD, I could not find a
# way to run the container interactively (-i -t)
# CMD ["streamlit","run","app.py"]

# - # References
# + https://docs.anaconda.com/anaconda/user-guide/tasks/docker/
# + https://hub.docker.com/r/continuumio/miniconda3

