Skip to content

Commit

Permalink
Fix and improve dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
AnesBenmerzoug committed May 1, 2024
1 parent f48e9ee commit 348a12b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

!src
!notebooks
notebooks/.ipynb_checkpoints
!setup.py
!scripts
!build_scripts
Expand Down
56 changes: 31 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#-------------- Base Image -------------------
FROM jupyter/minimal-notebook:python-3.11 as BASE
FROM jupyter/minimal-notebook:python-3.10 as BASE

ARG CODE_DIR=/tmp/code
ARG POETRY_VERSION=1.8.2
Expand All @@ -19,24 +19,31 @@ ENV PATH="${POETRY_HOME}/bin:$PATH"

USER root

RUN curl -sSL https://install.python-poetry.org | python -
# make is needed to install matplotlib 3.5.0 for python 3.11
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl make \
&& rm -rf /var/lib/apt/lists/*

RUN curl -sSL https://install.python-poetry.org | python - \
&& poetry self add poetry-plugin-export

USER ${NB_UID}

WORKDIR $CODE_DIR

COPY --chown=${NB_UID}:${NB_GID} poetry.lock pyproject.toml .
COPY --chown=${NB_UID}:${NB_GID} poetry.lock pyproject.toml ./

RUN poetry install --no-interaction --no-ansi --no-root --without=dev
RUN poetry export --no-interaction --no-ansi --without=dev -f requirements.txt --output requirements.txt \
&& pip install --no-cache-dir --requirement requirements.txt

COPY --chown=${NB_UID}:${NB_GID} src/ src/
COPY --chown=${NB_UID}:${NB_GID} README.md .

# Build code
RUN poetry build


#-------------- Main Image -------------------
FROM jupyter/minimal-notebook:python-3.11 as MAIN
FROM jupyter/minimal-notebook:python-3.10 as MAIN

ARG CODE_DIR=/tmp/code

Expand All @@ -46,7 +53,7 @@ ENV DEBIAN_FRONTEND=noninteractive\
PYTHONFAULTHANDLER=1 \
CODE_DIR=$CODE_DIR

ENV PATH="${CODE_DIR}/.venv/bin:$PATH"
# ENV PATH="${CODE_DIR}/.venv/bin:$PATH"

USER root

Expand All @@ -61,34 +68,33 @@ USER ${NB_UID}

WORKDIR ${CODE_DIR}

# Copy virtual environment from base image
COPY --from=BASE ${CODE_DIR}/.venv ${CODE_DIR}/.venv
# Copy conda directory from base image
COPY --from=BASE "${CONDA_DIR}" "${CONDA_DIR}"
# Copy built package from base image
COPY --from=BASE ${CODE_DIR}/dist ${CODE_DIR}/dist
# Install the built package
RUN pip install --no-cache-dir dist/*.whl
# Install kernel
RUN ipython kernel install --name "tfl-training-ml-control" --user

# Start of HACK: the home directory is overwritten by a mount when a jhub server is started off this image
############## Start of HACK
# the home directory is overwritten by a mount when a jhub server is started off this image
# Thus, we create a jovyan-owned directory to which we copy the code and then move it to the home dir as part
# of the entrypoint
COPY --chown=${NB_UID}:${NB_GID} entrypoint.sh $CODE_DIR
COPY --chown=${NB_UID}:${NB_GID} entrypoint.sh /usr/local/bin/

RUN chmod +x "${CODE_DIR}/"entrypoint.sh
RUN chmod +x "/usr/local/bin/entrypoint.sh"
# Unfortunately, we cannot use ${CODE_DIR} in the ENTRYPOINT directive, so we have to hardcode it
# Keep in sync with the value of CODE_DIR above
ENTRYPOINT ["/tmp/code/entrypoint.sh"]

# End of HACK
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

WORKDIR "${HOME}"
############## End of HACK

COPY --chown=${NB_UID}:${NB_GID} . $CODE_DIR

# Move to the code dir to install dependencies as the CODE_DIR contains the
# complete code base, including the poetry.lock file
WORKDIR $CODE_DIR

RUN pip install --no-cache-dir dist/*.whl

RUN ipython kernel install --name "tfl-training-ml-control" --user
WORKDIR "${CODE_DIR}"

# Copy notebooks
COPY --chown=${NB_UID}:${NB_GID} . .
# Trust all notebooks
RUN find notebooks -name '*.ipynb' -exec jupyter trust {} \;

WORKDIR "${HOME}"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ There are multiple ways of viewing/executing the content.
You can then start the container e.g., with

```shell
docker run -it -p 8888:8888 tfl-training-ml-control:local jupyter notebook --ip=0.0.0.0
docker run -it -p 8888:8888 tfl-training-ml-control:local jupyter lab --ip=0.0.0.0
```

4. Finally, for creating source code documentation, you can run
Expand All @@ -66,7 +66,7 @@ There are multiple ways of viewing/executing the content.
```shell
docker run -it --rm --privileged --net=host \
--env DISPLAY --volume /tmp/.X11-unix:/tmp/.X11-unix \
tfl-training-ml-control:local jupyter notebook --ip=0.0.0.0
tfl-training-ml-control:local jupyter lab --ip=0.0.0.0
```

> **Note** There is some non-trivial logic in the entrypoint that may collide
Expand Down
6 changes: 3 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ if [ ! -d "${ROOT_DIR}" ]; then
echo "Code not found in ${ROOT_DIR}, copying it during entrypoint. With jupyterhub this should happen only once"
mkdir "${ROOT_DIR}"
cp -rf "${CODE_DIR}"/* "${ROOT_DIR}/"
find notebooks -name '*.ipynb' -exec jupyter trust {} \;
fi

cd "${ROOT_DIR}" || exit


# original entrypoint, see https://github.com/jupyter/docker-stacks/blob/master/base-notebook/Dockerfile#L150
# original entrypoint, see https://github.com/jupyter/docker-stacks/blob/main/images/docker-stacks-foundation/Dockerfile#L131
# need -s option for tini to work properly when started not as PID 1
tini -s -g -- "$@"
tini -s -g -- start.sh "$@"

0 comments on commit 348a12b

Please sign in to comment.