|
2 | 2 | id: docker |
3 | 3 | title: Docker |
4 | 4 | sidebar_label: Docker |
5 | | -description: Docker image of a Jupyter Python Notebook to run OpEn |
| 5 | +description: Docker image of a JupyterLab environment to run OpEn |
6 | 6 | --- |
7 | 7 |
|
8 | 8 | <img src="/optimization-engine/img/docker.gif" alt="OpEn in Docker container" width="200"/> |
9 | 9 |
|
10 | 10 | ## What is Docker? |
11 | 11 |
|
12 | | -[Docker](https://www.docker.com/) is a tool that facilitates the execution and deployment of applications (often web applications) using containers. Containers package the applications along with the entirety of its context such as libraries and configuration files. You may think of containers as independent, isolated and self-sufficient processes. A container is then guaranteed to run on any other Linux-based machine regardless of the exact operating system, local configurations. |
| 12 | +[Docker](https://www.docker.com/) is a tool for packaging applications and their dependencies into containers. A container can run on different machines without requiring you to recreate the same environment manually. |
13 | 13 |
|
14 | | -Docker has nowadays become extremely popular. Developers package their software in bundles known as *Docker images* which can be stored in Docker Hub: an online repository. Then the users can obtain such images and run them locally. |
15 | | - |
16 | | -Docker is open-source and free and can be easily installed on any Linux, Mac OSX or Windows machine. |
17 | | - |
18 | | - |
19 | | -## What is a Jupyter Notebook? |
20 | | -According to [jupyter.org](https://jupyter.org/), "JupyterLab is a web-based interactive development environment for Jupyter notebooks, code, and data." |
| 14 | +## What is JupyterLab? |
21 | 15 |
|
| 16 | +According to [jupyter.org](https://jupyter.org/), JupyterLab is a web-based development environment for notebooks, code, and data. |
22 | 17 |
|
23 | 18 | ## Requirements |
24 | 19 |
|
25 | | -You need to have Docker installed ([instructions](https://docs.docker.com/get-docker/)). |
| 20 | +You need to have Docker installed. See the [official installation instructions](https://docs.docker.com/get-docker/). |
26 | 21 |
|
27 | | -## Pull and Use Docker Image |
| 22 | +## Pull and Run the Docker Image |
28 | 23 |
|
29 | | -Hmm.. that [OpEn](https://alphaville.github.io/optimization-engine/) framework looks really cool. I wonder whether it is possible to try it out very quickly without installing much... |
| 24 | +You can download the current Docker image using: |
30 | 25 |
|
31 | | -TaDaaa... |
| 26 | +```console |
| 27 | +docker pull alphaville/open:0.6.0 |
| 28 | +``` |
32 | 29 |
|
33 | | -You can now download this docker image using |
| 30 | +and then run it with: |
34 | 31 |
|
35 | 32 | ```console |
36 | | -docker pull alphaville/open |
| 33 | +docker run --name open-jupyter -p 127.0.0.1:8888:8888 -it alphaville/open:0.6.0 |
37 | 34 | ``` |
38 | 35 |
|
39 | | -and then run it with |
| 36 | +This starts JupyterLab and makes it available at: |
40 | 37 |
|
41 | | -```console |
42 | | -docker run -p 8888:8888 -it --name=open-notebook alphaville/open |
| 38 | +- [http://127.0.0.1:8888/lab](http://127.0.0.1:8888/lab) |
| 39 | + |
| 40 | +The image currently includes: |
| 41 | + |
| 42 | +- Python 3.12 |
| 43 | +- `opengen==0.10.0` |
| 44 | +- JupyterLab |
| 45 | +- Rust installed through `rustup` |
| 46 | +- Example notebooks under `/open/notebooks` |
| 47 | + |
| 48 | +By default, JupyterLab starts with token authentication enabled. To view the token: |
| 49 | + |
| 50 | +```bash |
| 51 | +docker logs open-jupyter |
43 | 52 | ``` |
44 | | -It will start a Jupyter Notebook at [localhost:8888](http://localhost:8888) without a password. |
45 | 53 |
|
46 | | -It is always a good idea to give your docker container a name using `--name`. For example |
| 54 | +It is always a good idea to give your container a name using `--name`. |
47 | 55 |
|
48 | 56 | <div class="alert alert-info"> |
49 | | -<b>Info:</b> Use <code>docker run alphaville/open</code> only the first time you run the container. Use <code>docker start {container_name}</code> all subsequent times. Always give a name to your containers.</div> |
| 57 | +<b>Info:</b> Use <code>docker run</code> only the first time you create the container. Use <code>docker start -ai open-jupyter</code> to start it again later.</div> |
50 | 58 |
|
51 | 59 | <div class="alert alert-success"> |
52 | | -<b>Tip:</b> To stop a running container do <code>docker stop {container_name}</code>.</div> |
| 60 | +<b>Tip:</b> To stop a running container, do <code>docker stop open-jupyter</code>.</div> |
53 | 61 |
|
| 62 | +## Configure the Docker Image |
54 | 63 |
|
55 | | -## Configure Docker Image |
| 64 | +### Configure password-based access |
56 | 65 |
|
57 | | -### Configure password |
| 66 | +To run JupyterLab with a password instead of the default token, provide a hashed password through `JUPYTER_NOTEBOOK_PASSWORD`: |
58 | 67 |
|
59 | | -To set up a password for your Python Notebook: |
| 68 | +```bash |
| 69 | +docker run \ |
| 70 | + --name open-jupyter \ |
| 71 | + -e JUPYTER_NOTEBOOK_PASSWORD='your hashed password' \ |
| 72 | + -p 127.0.0.1:8888:8888 \ |
| 73 | + -it alphaville/open:0.6.0 |
| 74 | +``` |
60 | 75 |
|
61 | | -- <a href="https://jupyter-notebook.readthedocs.io/en/stable/public_server.html#hashed-pw" target="_blank">Hash your password</a> |
62 | | -- Run your docker image with: |
| 76 | +For password hashing instructions, see the [Jupyter Server documentation](https://jupyter-server.readthedocs.io/en/latest/operators/public-server.html). |
63 | 77 |
|
64 | | -``` |
65 | | -docker run \ |
66 | | - -e JUPYTER_NOTEBOOK_PASSWORD={your hashed password} \ |
67 | | - -p 8888:8888 \ |
68 | | - --name=open-notebook \ |
69 | | - alphaville/open |
70 | | -``` |
| 78 | +<details> |
| 79 | + <summary>How to set up a password</summary> |
71 | 80 |
|
72 | | -For example, let's say you want to set the password **open**. Then do |
| 81 | + You can read more about how to set up a password for your |
| 82 | + Python notebook [here](https://jupyter-server.readthedocs.io/en/latest/operators/public-server.html). |
| 83 | + TL;DR: run the following command: |
73 | 84 |
|
74 | | -```console |
75 | | -docker run \ |
76 | | - -e JUPYTER_NOTEBOOK_PASSWORD=sha1:898ca689bf37:2ee883bfd6ffe82a749a86e37964700bd06a2ff9 \ |
77 | | - -p 8888:8888 \ |
78 | | - --name=open-notebook \ |
79 | | - alphaville/open |
80 | | -``` |
| 85 | + ```bash |
| 86 | + docker run --rm -it --entrypoint /venv/bin/python \ |
| 87 | + alphaville/open:0.6.0 \ |
| 88 | + -c "from jupyter_server.auth import passwd; print(passwd())" |
| 89 | + ``` |
| 90 | + |
| 91 | + You will be asked to provide your password twice. Then a string |
| 92 | + will be printed; this is your hashed password. |
| 93 | +</details> |
81 | 94 |
|
82 | 95 | ### Configure port |
83 | 96 |
|
84 | | -You can access the Jupyter Notebook at a different port by configuring the port forwarding of Docker. For example, to run on port 80 run: |
| 97 | +You can access JupyterLab on a different host port by changing Docker's port forwarding. For example, to use port `80` on your machine: |
85 | 98 |
|
86 | | -```console |
87 | | -docker run -p 80:8888 alphaville/open |
| 99 | +```bash |
| 100 | +docker run -p 80:8888 alphaville/open:0.6.0 |
88 | 101 | ``` |
89 | 102 |
|
90 | | -Then, Jupyter Notebook will be accessible at http://localhost. |
91 | | -The argument `-p 80:8888` means that the internal port `8888` is mapped to the host's port `80`. |
| 103 | +Then JupyterLab will be available at `http://localhost/lab`. |
92 | 104 |
|
| 105 | +### Work with notebooks |
93 | 106 |
|
94 | | -### Load additional python packages |
| 107 | +The bundled example notebook is available inside the container at: |
95 | 108 |
|
96 | | -You can load any packages you like in your Python Notebook using `pip3` and prepend a `!` to your command. For example, if you want to load `matplotlib`, do |
| 109 | +```text |
| 110 | +/open/notebooks/example.ipynb |
| 111 | +``` |
| 112 | + |
| 113 | +To persist your own notebooks across container restarts, mount a Docker volume onto `/open`: |
| 114 | + |
| 115 | +```bash |
| 116 | +docker volume create OpEnVolume |
| 117 | +docker run --name open-jupyter \ |
| 118 | + --mount source=OpEnVolume,destination=/open \ |
| 119 | + -p 127.0.0.1:8888:8888 \ |
| 120 | + -it alphaville/open:0.6.0 |
| 121 | +``` |
| 122 | + |
| 123 | +### Load additional Python packages |
| 124 | + |
| 125 | +You can install additional packages from inside JupyterLab. For example: |
97 | 126 |
|
98 | 127 | ```python |
99 | | -!pip3 install matplotlib |
| 128 | +!pip install matplotlib |
100 | 129 | ``` |
101 | 130 |
|
102 | | -If you install a Python package in a certain docker container, it will be there the next time you access it. |
| 131 | +Packages installed into a persistent container or volume-backed environment will still be there the next time you access it. |
103 | 132 |
|
104 | | -### Open terminal into docker container |
| 133 | +### Open a terminal in the container |
105 | 134 |
|
106 | | -Suppose you have a running docker container with name `open-notebook` (you may specify a name for your docker container when you start it using `--name`). To open a terminal into it you can run |
| 135 | +Suppose you have a running Docker container with name `open-jupyter`. To open a shell in it: |
107 | 136 |
|
108 | | -```console |
109 | | -docker exec -it open-notebook /bin/bash |
| 137 | +```bash |
| 138 | +docker exec -it open-jupyter /bin/bash |
110 | 139 | ``` |
111 | 140 |
|
| 141 | +The Python virtual environment is available at `/venv`. |
| 142 | + |
112 | 143 | ### Download your optimizer |
113 | | -To download your optimizer from Jupyter you first need to create an archive ot it. You can do that using `tar` as follows |
| 144 | + |
| 145 | +To download a generated optimizer from JupyterLab, first create an archive: |
114 | 146 |
|
115 | 147 | ```python |
116 | 148 | !tar -cf rosenbrock.tar.gz optimizers/rosenbrock |
|
0 commit comments