|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | +# Dockerfile to build environment and run ECCOv4 Python tutorials. |
| 3 | +# Adapted from pangeo/base-image. |
| 4 | +FROM ubuntu:22.04 |
| 5 | + |
| 6 | +ARG NB_USER |
| 7 | +ARG NB_UID |
| 8 | + |
| 9 | +# Setup environment to match variables set by repo2docker as much as possible |
| 10 | +# The name of the conda environment into which the requested packages are installed |
| 11 | + |
| 12 | +ENV CONDA_ENV=jupyter \ |
| 13 | + # Tell apt-get to not block installs by asking for interactive human input |
| 14 | + # Use /bin/bash as shell, not the default /bin/sh (arrow keys, etc don't work then) |
| 15 | + SHELL=/bin/bash \ |
| 16 | + # Setup locale to be UTF-8, avoiding gnarly hard to debug encoding errors |
| 17 | + LANG=C.UTF-8 \ |
| 18 | + LC_ALL=C.UTF-8 \ |
| 19 | + # Install conda in the same place repo2docker does |
| 20 | + CONDA_DIR=/srv/conda |
| 21 | + |
| 22 | + |
| 23 | +# All env vars that reference other env vars need to be in their own ENV block |
| 24 | +# Path to the python environment where the jupyter notebook packages are installed |
| 25 | +ENV NB_PYTHON_PREFIX=${CONDA_DIR}/envs/${CONDA_ENV} \ |
| 26 | + # Home directory of our non-root user |
| 27 | + HOME=/home/${NB_USER} |
| 28 | + |
| 29 | +# Add both our notebook env as well as default conda installation to $PATH |
| 30 | +# Thus, when we start a `python` process (for kernels, or notebooks, etc), |
| 31 | +# it loads the python in the notebook conda environment, as that comes |
| 32 | +# first here. |
| 33 | +ENV PATH=${NB_PYTHON_PREFIX}/bin:${CONDA_DIR}/bin:${PATH} |
| 34 | + |
| 35 | +# Ask dask to read config from ${CONDA_DIR}/etc rather than |
| 36 | +# the default of /etc, since the non-root user can write |
| 37 | +# to ${CONDA_DIR}/etc but not to /etc |
| 38 | +ENV DASK_ROOT_CONFIG=${CONDA_DIR}/etc |
| 39 | + |
| 40 | + |
| 41 | +RUN echo "Creating ${NB_USER} user..." \ |
| 42 | + # Create a group for the user to be part of, with gid same as uid |
| 43 | + && groupadd --gid ${NB_UID} ${NB_USER} \ |
| 44 | + # Create non-root user, with given gid, uid and create $HOME |
| 45 | + && useradd --create-home --gid ${NB_UID} --no-log-init --uid ${NB_UID} ${NB_USER} |
| 46 | + |
| 47 | +# Make sure that /srv is owned by non-root user, so we can install things there |
| 48 | +USER root |
| 49 | +RUN chown -R ${NB_USER}:${NB_USER} /srv |
| 50 | + |
| 51 | +# Run conda activate each time a bash shell starts, so users don't have to manually type conda activate |
| 52 | +# Note this is only read by shell, but not by the jupyter notebook - that relies |
| 53 | +# on us starting the correct `python` process, which we do by adding the notebook conda environment's |
| 54 | +# bin to PATH earlier ($NB_PYTHON_PREFIX/bin) |
| 55 | +RUN echo ". ${CONDA_DIR}/etc/profile.d/conda.sh ; conda activate ${CONDA_ENV}" > /etc/profile.d/init_conda.sh |
| 56 | + |
| 57 | +# Install basic apt packages |
| 58 | +RUN echo "Installing Apt-get packages..." \ |
| 59 | + && apt-get update --fix-missing > /dev/null \ |
| 60 | + && apt-get install -y apt-utils wget tmux zip tzdata > /dev/null \ |
| 61 | + && apt-get clean \ |
| 62 | + && rm -rf /var/lib/apt/lists/* |
| 63 | + |
| 64 | +# Add TZ configuration - https://github.com/PrefectHQ/prefect/issues/3061 |
| 65 | +ENV TZ=UTC |
| 66 | +# ======================== |
| 67 | + |
| 68 | +USER ${NB_USER} |
| 69 | +WORKDIR ${HOME} |
| 70 | + |
| 71 | +# Install latest mambaforge in ${CONDA_DIR} |
| 72 | +RUN echo "Installing Miniforge..." \ |
| 73 | + && URL="https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-$(uname -m).sh" \ |
| 74 | + && wget --quiet ${URL} -O installer.sh \ |
| 75 | + && /bin/bash installer.sh -u -b -p ${CONDA_DIR} \ |
| 76 | + && rm installer.sh \ |
| 77 | + && mamba clean -afy \ |
| 78 | + # After installing the packages, we cleanup some unnecessary files |
| 79 | + # to try reduce image size - see https://jcristharif.com/conda-docker-tips.html |
| 80 | + # Although we explicitly do *not* delete .pyc files, as that seems to slow down startup |
| 81 | + # quite a bit unfortunately - see https://github.com/2i2c-org/infrastructure/issues/2047 |
| 82 | + && find ${CONDA_DIR} -follow -type f -name '*.a' -delete |
| 83 | + |
| 84 | + |
| 85 | +COPY --chown=${NB_USER}:${NB_USER} ./ECCO-v4-Python-Tutorial ${HOME}/ECCO-v4-Python-Tutorial |
| 86 | + |
| 87 | +RUN echo "Using environment.yml to create conda environment ${CONDA_ENV}" & \ |
| 88 | + mamba env create --name ${CONDA_ENV} \ |
| 89 | + -f ${HOME}/ECCO-v4-Python-Tutorial/Docker/environment.yml |
| 90 | + |
| 91 | +# create symlink from home directory to jupyter_lab_start_docker.sh |
| 92 | +RUN ln -s ${HOME}/ECCO-v4-Python-Tutorial/Docker/jupyter_lab_start_docker.sh \ |
| 93 | + ${HOME}/jupyter_lab_start_docker.sh |
| 94 | + |
| 95 | +EXPOSE 8888 |
| 96 | + |
| 97 | +# start jupyter lab inside the container |
| 98 | +ENTRYPOINT ["${HOME}/jupyter_lab_start_docker.sh"] |
0 commit comments