From be74474025f82be517535431475d33da046bb87a Mon Sep 17 00:00:00 2001 From: Edoardo Baldi Date: Tue, 3 Oct 2023 11:42:47 +0200 Subject: [PATCH] Automatically build Docker image from the repository (#80) Co-authored-by: Simone Baffelli --- .github/workflows/build-docker-image.yml | 29 +++++++++++ README.md | 61 +++++++++++++++++++++--- 2 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/build-docker-image.yml diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml new file mode 100644 index 00000000..930cb33f --- /dev/null +++ b/.github/workflows/build-docker-image.yml @@ -0,0 +1,29 @@ +--- +name: Build Tutorial Container +on: + push: + branches: + - main + paths-ignore: + - '*.md' + - slides/** + - images/** + - .gitignore + workflow_dispatch: + +jobs: + repo2docker: + runs-on: ubuntu-latest + permissions: + packages: write + steps: + - name: checkout files in repo + uses: actions/checkout@main + + - name: update jupyter dependencies with repo2docker + uses: jupyterhub/repo2docker-action@master + with: + DOCKER_USERNAME: ${{ github.actor }} + DOCKER_PASSWORD: ${{ secrets.GITHUB_TOKEN }} + DOCKER_REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} diff --git a/README.md b/README.md index 721f2d41..170cc98f 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,20 @@ # python-tutorial -[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/empa-scientific-it/python-tutorial.git/main?labpath=index.ipynb) - +[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/empa-scientific-it/python-tutorial.git/main?labpath=index.ipynb) [![Build Docker container](https://github.com/empa-scientific-it/python-tutorial/actions/workflows/build-docker-image.yml/badge.svg)](https://github.com/empa-scientific-it/python-tutorial/actions/workflows/build-docker-image.yml) ## Run the tutorial on your computer -### 0. Prerequisites +You have two ways in which you can run the tutorial **locally**. + +### 1. With a `conda` environment + +#### 0. Prerequisites To run the tutorial locally, you should first install [conda](https://docs.conda.io/en/latest/miniconda.html) (or [mamba](https://mamba.readthedocs.io/en/latest/installation.html)). It is also suggested that you have a recent version of `git`. Check out [how to install `git`](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) on your operating system. -### 1. Download the material +#### 1. Download the material Go to the directory on your machine where you want to download the material and clone the repository: @@ -25,7 +28,7 @@ Alternatively, you can manually download a ZIP archive with the latest version o Extract the archive in a directory of your choice. -### 2. Create a dedicated environment +#### 2. Create a dedicated environment Enter the tutorial folder with @@ -45,6 +48,7 @@ conda env create -f binder/environment.yml > If you are on Windows and using Command Prompt or the PowerShell, please make sure to adjust the paths in the commands above accordingly. Then activate the environment with + ```console conda activate python-tutorial ``` @@ -55,9 +59,54 @@ You can update the existing environment (that is, downloading the latest version conda env update -f binder/environment.yml ``` -### 3. Launch the tutorial via Jupyter +#### 3. Launch the tutorial via Jupyter Finally, launch JupyterLab with + ```console jupyter lab ``` + +To update the existing environment, run + +```console +conda env update -f environment.yml +``` + +### 2. With Docker + +> **Note** +> +> The following instructions are for Windows. With minor changes, the steps work on macOS or Linux as well. + +1. Install Docker Desktop: First, you need to install Docker Desktop on your Windows machine. You can download it from the official Docker website: https://www.docker.com/products/docker-desktop. + +2. Create a folder: Open File Explorer and create a new folder where you want to save the tutorial's materials. For example, you could create a folder called "python-tutorial" on your Desktop. + +3. Open PowerShell: Once Docker Desktop is installed, open PowerShell on your Windows machine. You can do this by pressing the "Windows" key and typing "PowerShell" in the search bar. + +4. Pull the Docker image: In PowerShell, run the following command to pull the "empascientificit/python-tutorial" Docker image: + +```console +docker pull ghcr.io/empa-scientific-it/python-tutorial:latest +``` + +5. Run the Docker container: Once the image is downloaded, run the following command to start a Docker container from the image: + +```console +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 +``` + +Replace `/path/to/python-tutorial` with the path to the folder you created in step 2, for example `C:/Users/yourusername/Desktop/python-tutorial`. + +> **Note** +> +> The above command will **mirror** the content of your local folder (e.g., `C:/Users/yourusername/Desktop/python-tutorial`) to the `work/` folder **inside the container**. In this way, every file or folder you copy or create into `work/` will be saved on your machine, and will remain there **even if you stop Docker**. + +6. Access the Jupyter Notebook: Open a web browser and navigate to `http://localhost:8888/lab`. You should see the Jupyter Notebook interface. Enter the token provided in the PowerShell console to access the notebook. Alternatively, you can directly click on the link that appears in the PowerShell after the container has started. + +You can now use the Jupyter in the Docker container to run the python-tutorial. When you're done, you can stop the container by pressing `Ctrl+C` in the PowerShell console. + +> **Note** +> +> If you want to restart the container, you can simply run the command `docker container start python_tutorial`.