Skip to content

Commit b6e3338

Browse files
authored
Simplify and optimize the Docker image build (empa-scientific-it#264)
* Use Dockerfile and official Docker actions to build and push the Docker image. * Build images in 3 cases: commit to the main, pull request, and release (`vX.Y` or `vX.Y.Z`). * Build the image for the arch and amd64 architectures. * Update the README instructions for local use. * Update dependencies. * Remove old workflow that was using the `jupyter-repo2docker` action.
1 parent ac2b4db commit b6e3338

File tree

6 files changed

+155
-29
lines changed

6 files changed

+155
-29
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.git
2+
slides/
3+
.DS_Store
4+
__pycache__/

.github/workflows/build-docker-image.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

.github/workflows/docker-build.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
name: Build Tutorial Container
3+
4+
on:
5+
push:
6+
branches:
7+
- main
8+
paths-ignore:
9+
- "*.md"
10+
- slides/**
11+
- images/**
12+
- .gitignore
13+
tags:
14+
- "v*.*"
15+
- "v*.*.*"
16+
pull_request:
17+
paths-ignore:
18+
- "*.md"
19+
- slides/**
20+
- images/**
21+
- .gitignore
22+
workflow_dispatch:
23+
24+
jobs:
25+
build-and-push:
26+
runs-on: ubuntu-latest
27+
permissions:
28+
packages: write
29+
strategy:
30+
matrix:
31+
arch: [amd64, arm64]
32+
fail-fast: false
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v4
36+
37+
- name: Set up QEMU
38+
uses: docker/setup-qemu-action@v3
39+
40+
- name: Setup Docker Buildx
41+
uses: docker/setup-buildx-action@v3
42+
43+
- name: Log in to GHCR
44+
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
45+
uses: docker/login-action@v3
46+
with:
47+
registry: ghcr.io
48+
username: ${{ github.actor }}
49+
password: ${{ secrets.GITHUB_TOKEN }}
50+
51+
- name: Docker meta
52+
id: meta
53+
uses: docker/metadata-action@v5
54+
with:
55+
images: ghcr.io/${{ github.repository }}
56+
tags: |
57+
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
58+
type=ref,event=pr
59+
type=ref,event=tag
60+
type=sha,format=short
61+
62+
- name: Build and push
63+
uses: docker/build-push-action@v6
64+
with:
65+
context: .
66+
platforms: linux/${{ matrix.arch }}
67+
push: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}
68+
tags: ${{ steps.meta.outputs.tags }}
69+
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Use the jupyter/minimal-notebook as the base image
2+
FROM quay.io/jupyter/minimal-notebook:latest
3+
4+
# Metadata labels
5+
LABEL org.opencontainers.image.title="Python Tutorial"
6+
LABEL org.opencontainers.image.description="A containerized Python tutorial environment with Jupyter Lab."
7+
LABEL org.opencontainers.image.authors="Empa Scientific IT <scientificit@empa.ch>"
8+
LABEL org.opencontainers.image.url="https://github.com/empa-scientific-it/python-tutorial"
9+
LABEL org.opencontainers.image.source="https://github.com/empa-scientific-it/python-tutorial"
10+
LABEL org.opencontainers.image.version="1.0.0"
11+
LABEL org.opencontainers.image.licenses="MIT"
12+
13+
# Set environment variables for the tutorial and repository
14+
ENV BASENAME="python-tutorial"
15+
ENV REPO=${HOME}/${BASENAME}
16+
ENV IPYTHONDIR="${HOME}/.ipython"
17+
18+
# Switch to root user to install additional dependencies
19+
USER root
20+
RUN apt-get update && \
21+
apt-get install -y --no-install-recommends \
22+
build-essential \
23+
gcc \
24+
g++ \
25+
libffi-dev \
26+
libgl1 && \
27+
apt-get clean && \
28+
rm -rf /var/lib/apt/lists/*
29+
30+
# Switch back to the default notebook user
31+
USER ${NB_UID}
32+
33+
# Set up the Conda environment
34+
COPY docker/environment.yml /tmp/environment.yml
35+
RUN mamba env update -n base -f /tmp/environment.yml && \
36+
mamba clean --all -f -y && \
37+
fix-permissions "${CONDA_DIR}" && \
38+
fix-permissions "/home/${NB_USER}"
39+
40+
# Prepare IPython configuration (move earlier in the build)
41+
RUN mkdir -p ${HOME}/.ipython/profile_default
42+
COPY --chown=${NB_UID}:${NB_GID} binder/ipython_config.py ${HOME}/.ipython/profile_default/
43+
44+
# Set the working directory to the repository
45+
WORKDIR ${REPO}
46+
47+
# Use the default ENTRYPOINT from the base image to start Jupyter Lab
48+
ENTRYPOINT ["tini", "-g", "--", "start.sh"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ docker pull ghcr.io/empa-scientific-it/python-tutorial:latest
9393
5. Run the Docker container: Once the image is downloaded, run the following command to start a Docker container from the image:
9494

9595
```console
96-
docker run -p 8888:8888 --name python_tutorial -v /path/to/python-tutorial:/home/jovyan/work ghcr.io/empa-scientific-it/python-tutorial:latest jupyter lab --ip 0.0.0.0 --no-browser
96+
docker run -p 8888:8888 --name python_tutorial -v /path/to/python-tutorial:/home/jovyan/python-tutorial ghcr.io/empa-scientific-it/python-tutorial:latest jupyter lab --ip 0.0.0.0 --no-browser
9797
```
9898

9999
Replace `/path/to/python-tutorial` with the path to the folder you created in step 2, for example `C:/Users/yourusername/Desktop/python-tutorial`.

docker/environment.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
name: base
3+
channels:
4+
- conda-forge
5+
dependencies:
6+
- pip
7+
- pip:
8+
- numpy
9+
- matplotlib
10+
- pandas
11+
- ipywidgets
12+
- ipynbname
13+
- jupyterlab
14+
- pytest
15+
- pytest-timeout
16+
- markdown
17+
- pre-commit
18+
- geostatspy
19+
- gstools
20+
- scikit-learn
21+
- attrs
22+
- multiprocess
23+
- openai
24+
- tenacity
25+
- markdown2
26+
- python-dotenv
27+
- pillow
28+
- opencv-python
29+
- torch
30+
- torchaudio
31+
- torchvision
32+
- albumentations
33+
- grad-cam

0 commit comments

Comments
 (0)