Ramses II commited on
Commit
66030fa
·
1 Parent(s): ddf774c
Files changed (4) hide show
  1. Dockerfile +15 -17
  2. entrypoint.sh +4 -42
  3. jupyter_config.py +1 -7
  4. nginx.conf +5 -30
Dockerfile CHANGED
@@ -1,22 +1,18 @@
1
- FROM jupyter/scipy-notebook:latest
2
 
3
  # Set the working directory to /app
4
  WORKDIR /app
5
 
6
- # Copy the requirements file and install Python dependencies
7
  COPY requirements.txt .
8
- RUN pip install --no-cache-dir -r requirements.txt
9
 
10
- # Copy static files
11
  COPY public /app/public
12
 
13
  # Switch to root user to install system packages and set permissions
14
  USER root
15
 
16
- # Install necessary dependencies
17
- RUN apt-get update && apt-get install -y \
18
- nginx \
19
- && apt-get clean && rm -rf /var/lib/apt/lists/*
20
 
21
  # Copy the configuration files and entrypoint script
22
  COPY jupyter_config.py /etc/jupyter/jupyter_config.py
@@ -26,23 +22,25 @@ COPY entrypoint.sh /app/entrypoint.sh
26
  # Set the entrypoint script as executable
27
  RUN chmod +x /app/entrypoint.sh
28
 
29
- # Create necessary directories and set permissions
30
- RUN mkdir -p /data /var/log/nginx /var/log/pods && \
31
- chown -R ${NB_UID}:${NB_GID} /app /data /var/log/nginx /var/log/pods /etc/nginx/nginx.conf && \
32
- chmod 755 /data
33
 
34
  # Switch back to the original user
35
  USER ${NB_UID}
36
 
37
  # Set the working directory for the user
38
- WORKDIR /data
39
 
40
- # Mount the secret
41
- RUN --mount=type=secret,id=JUPYTER_TOKEN,mode=0444,required=true \
42
- cat /run/secrets/JUPYTER_TOKEN
43
 
44
- # Expose the port for Nginx
45
  EXPOSE 7860
46
 
 
 
 
 
 
47
  # Run the entrypoint script when the container starts
48
  ENTRYPOINT ["/app/entrypoint.sh"]
 
1
+ FROM jupyter/scipy-notebook
2
 
3
  # Set the working directory to /app
4
  WORKDIR /app
5
 
6
+ # Copy the requirements file
7
  COPY requirements.txt .
 
8
 
 
9
  COPY public /app/public
10
 
11
  # Switch to root user to install system packages and set permissions
12
  USER root
13
 
14
+ # Install the dependencies
15
+ RUN apt-get update && apt-get install -y wget nginx && pip install -r requirements.txt
 
 
16
 
17
  # Copy the configuration files and entrypoint script
18
  COPY jupyter_config.py /etc/jupyter/jupyter_config.py
 
22
  # Set the entrypoint script as executable
23
  RUN chmod +x /app/entrypoint.sh
24
 
25
+ # Set the permissions for the app directory to the existing user
26
+ RUN chown -R ${NB_UID}:${NB_GID} /app
 
 
27
 
28
  # Switch back to the original user
29
  USER ${NB_UID}
30
 
31
  # Set the working directory for the user
32
+ WORKDIR /home/${NB_USER}/app
33
 
34
+ # Expose the port 8888 for JupyterLab
35
+ EXPOSE 8888
 
36
 
37
+ # Expose the port 7860 for Nginx
38
  EXPOSE 7860
39
 
40
+ # Set the environment variables
41
+ ENV JUPYTERLAB_PORT=8888
42
+ ENV JUPYTERLAB_TOKEN=your_secret_token
43
+ ENV NGINX_PORT=7860
44
+
45
  # Run the entrypoint script when the container starts
46
  ENTRYPOINT ["/app/entrypoint.sh"]
entrypoint.sh CHANGED
@@ -1,52 +1,14 @@
1
  #!/bin/bash
2
 
3
- # Function to handle errors
4
- handle_error() {
5
- echo "Error: $1"
6
- exit 1
7
- }
8
-
9
- # Verify and create the /data directory if it doesn't exist
10
- if [ ! -d "/data" ]; then
11
- mkdir -p /data || handle_error "Could not create /data directory"
12
- echo "/data directory created for persistent storage"
13
- fi
14
-
15
- # Verify write permissions for /data directory
16
- if [ ! -w "/data" ]; then
17
- echo "Warning: No write permissions for /data. Some data may not be persistent."
18
- fi
19
-
20
- # Read JupyterLab token from the mounted secret file
21
- if [ -f "/run/secrets/JUPYTER_TOKEN" ]; then
22
- export JUPYTER_TOKEN=$(cat /run/secrets/JUPYTER_TOKEN)
23
- else
24
- echo "JUPYTER_TOKEN secret not found. Using AUX_TOKEN as fallback."
25
- export JUPYTER_TOKEN=${AUX_TOKEN:-}
26
- fi
27
-
28
- # Verify if the token is empty
29
- if [ -z "$JUPYTER_TOKEN" ]; then
30
- handle_error "JupyterLab token is empty"
31
- fi
32
-
33
- # Check for GPU availability
34
- if command -v nvidia-smi &> /dev/null; then
35
- echo "GPU detected. Configuring environment for GPU usage."
36
- export NVIDIA_VISIBLE_DEVICES=all
37
- export NVIDIA_DRIVER_CAPABILITIES=compute,utility
38
- else
39
- echo "No GPU detected. CPU will be used."
40
- fi
41
 
42
  # Start JupyterLab in the background
43
- jupyter lab --ip=0.0.0.0 --port=${JUPYTERLAB_PORT} --no-browser --allow-root \
44
- --NotebookApp.token=${JUPYTER_TOKEN} \
45
- --notebook-dir=/data &
46
 
47
  # Ensure the Nginx PID file is writable
48
  touch /tmp/nginx.pid
49
  chmod 644 /tmp/nginx.pid
50
 
51
  # Start Nginx in the foreground
52
- nginx -g "daemon off;" -c /etc/nginx/nginx.conf || handle_error "Failed to start Nginx"
 
1
  #!/bin/bash
2
 
3
+ # Read the JupyterLab token from the HF secret
4
+ JUPYTERLAB_TOKEN=$(cat /run/secrets/JUPYTER_TOKEN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Start JupyterLab in the background
7
+ jupyter lab --ip=0.0.0.0 --port=${JUPYTERLAB_PORT} --no-browser --allow-root --NotebookApp.base_url=/jupyter --NotebookApp.token=${JUPYTERLAB_TOKEN} &
 
 
8
 
9
  # Ensure the Nginx PID file is writable
10
  touch /tmp/nginx.pid
11
  chmod 644 /tmp/nginx.pid
12
 
13
  # Start Nginx in the foreground
14
+ nginx -g "daemon off;" -c /etc/nginx/nginx.conf
jupyter_config.py CHANGED
@@ -1,11 +1,5 @@
1
- # Configuration file for jupyter-notebook.
2
-
3
- import os
4
-
5
  c.NotebookApp.ip = '0.0.0.0'
6
  c.NotebookApp.port = 8888
7
  c.NotebookApp.open_browser = False
8
- c.NotebookApp.notebook_dir = '/data'
9
  c.NotebookApp.allow_root = True
10
- c.NotebookApp.token = os.environ.get('JUPYTER_TOKEN')
11
- c.NotebookApp.base_url = '/jupyter'
 
 
 
 
 
1
  c.NotebookApp.ip = '0.0.0.0'
2
  c.NotebookApp.port = 8888
3
  c.NotebookApp.open_browser = False
 
4
  c.NotebookApp.allow_root = True
5
+ c.NotebookApp.token = 'your_secret_token'
 
nginx.conf CHANGED
@@ -1,42 +1,17 @@
1
- worker_processes 1;
2
- pid /tmp/nginx.pid;
3
-
4
- events {
5
- worker_connections 1024;
6
- }
7
-
8
  http {
9
- include /etc/nginx/mime.types;
10
- default_type application/octet-stream;
11
-
12
- sendfile on;
13
- keepalive_timeout 65;
14
-
15
  server {
16
- listen 7860;
17
- server_name localhost;
18
 
19
- # Serve static files from /app/public
20
  location / {
21
  root /app/public;
22
- try_files $uri $uri/ =404;
23
  }
24
 
25
- # Proxy JupyterLab to /jupyter/
26
  location /jupyter/ {
27
- proxy_pass http://127.0.0.1:8888;
28
  proxy_set_header Host $host;
29
  proxy_set_header X-Real-IP $remote_addr;
30
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
31
- proxy_set_header X-Forwarded-Proto $scheme;
32
-
33
- # WebSocket support
34
- proxy_http_version 1.1;
35
- proxy_set_header Upgrade $http_upgrade;
36
- proxy_set_header Connection "upgrade";
37
  }
38
-
39
- error_log /tmp/nginx_error.log;
40
- access_log /tmp/nginx_access.log;
41
  }
42
- }
 
 
 
 
 
 
 
 
1
  http {
 
 
 
 
 
 
2
  server {
3
+ listen 7860;
4
+ server_name _;
5
 
 
6
  location / {
7
  root /app/public;
8
+ index index.html;
9
  }
10
 
 
11
  location /jupyter/ {
12
+ proxy_pass http://localhost:8888;
13
  proxy_set_header Host $host;
14
  proxy_set_header X-Real-IP $remote_addr;
 
 
 
 
 
 
 
15
  }
 
 
 
16
  }
17
+ }