Ramses II commited on
Commit
c0616b4
1 Parent(s): ea37cb1
Files changed (5) hide show
  1. Dockerfile +12 -42
  2. entrypoint.sh +17 -24
  3. jupyter_config.py +5 -3
  4. nginx.conf +3 -2
  5. requirements.txt +1 -1
Dockerfile CHANGED
@@ -1,10 +1,11 @@
1
- FROM jupyter/scipy-notebook
2
 
3
- # Set the working directory to /data
4
- WORKDIR /data
5
 
6
- # Copy the requirements file
7
  COPY requirements.txt .
 
8
 
9
  # Copy static files
10
  COPY public /app/public
@@ -12,25 +13,11 @@ COPY public /app/public
12
  # Switch to root user to install system packages and set permissions
13
  USER root
14
 
15
- # Install sudo and other dependencies
16
  RUN apt-get update && apt-get install -y \
17
- wget \
18
  nginx \
19
- sudo \
20
- build-essential \
21
- curl \
22
- git \
23
- vim \
24
  && apt-get clean && rm -rf /var/lib/apt/lists/*
25
 
26
- # Install Python dependencies
27
- RUN pip install --no-cache-dir -r requirements.txt
28
-
29
- # Add jovyan to sudoers
30
- RUN usermod -aG sudo jovyan && \
31
- echo "jovyan ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/jovyan && \
32
- chmod 0440 /etc/sudoers.d/jovyan
33
-
34
  # Copy the configuration files and entrypoint script
35
  COPY jupyter_config.py /etc/jupyter/jupyter_config.py
36
  COPY nginx.conf /etc/nginx/nginx.conf
@@ -39,39 +26,22 @@ COPY entrypoint.sh /app/entrypoint.sh
39
  # Set the entrypoint script as executable
40
  RUN chmod +x /app/entrypoint.sh
41
 
42
- # Create necessary directories and set permissions for Nginx
43
- RUN mkdir -p /var/lib/nginx/body /var/lib/nginx/fastcgi \
44
- /var/lib/nginx/proxy /var/lib/nginx/scgi \
45
- /var/lib/nginx/uwsgi /var/log/nginx \
46
- && chown -R ${NB_UID}:${NB_GID} /var/lib/nginx /var/log/nginx /var/run /run \
47
- && chmod 755 /var/lib/nginx /var/run /run
48
-
49
- # Ensure Nginx has permissions to write to log directory and PID file
50
- RUN touch /var/log/nginx/error.log /var/log/nginx/access.log /run/nginx.pid \
51
- && chown -R ${NB_UID}:${NB_GID} /var/log/nginx /run/nginx.pid
52
-
53
- # Create /data directory and set permissions
54
- RUN mkdir -p /data && chown -R ${NB_UID}:${NB_GID} /data
55
-
56
- # Handle the JUPYTER_TOKEN secret at build time
57
- RUN --mount=type=secret,id=JUPYTER_TOKEN,mode=0444,required=true \
58
- echo "JUPYTER_TOKEN=$(cat /run/secrets/JUPYTER_TOKEN)" >> /etc/environment
59
 
60
  # Switch back to the original user
61
  USER ${NB_UID}
62
 
63
- # Set the working directory to /data
64
  WORKDIR /data
65
 
66
- # Expose the port 8888 for JupyterLab
67
- EXPOSE 8888
68
-
69
- # Expose the port 7860 for Nginx
70
  EXPOSE 7860
71
 
72
  # Set the environment variables
73
  ENV JUPYTERLAB_PORT=8888
74
- ENV NGINX_PORT=7860
75
 
76
  # Run the entrypoint script when the container starts
77
  ENTRYPOINT ["/app/entrypoint.sh"]
 
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
 
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
23
  COPY nginx.conf /etc/nginx/nginx.conf
 
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 && \
31
+ chown -R ${NB_UID}:${NB_GID} /app /data /var/log/nginx /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
+ # Expose the port for Nginx
 
 
 
41
  EXPOSE 7860
42
 
43
  # Set the environment variables
44
  ENV JUPYTERLAB_PORT=8888
 
45
 
46
  # Run the entrypoint script when the container starts
47
  ENTRYPOINT ["/app/entrypoint.sh"]
entrypoint.sh CHANGED
@@ -1,50 +1,43 @@
1
  #!/bin/bash
2
 
3
- # Función para manejar errores
4
  handle_error() {
5
  echo "Error: $1"
6
  exit 1
7
  }
8
 
9
- # Verificar y crear el directorio /data si no existe
10
  if [ ! -d "/data" ]; then
11
- mkdir -p /data || handle_error "No se pudo crear el directorio /data"
12
- echo "Directorio /data creado para almacenamiento persistente"
13
  fi
14
 
15
- # Verificar permisos del directorio /data
16
  if [ ! -w "/data" ]; then
17
- echo "Advertencia: No se tienen permisos de escritura en /data. Algunos datos pueden no ser persistentes."
18
  fi
19
 
20
- # Asegurar que el usuario jovyan sea el propietario de /data
21
- chown -R ${NB_UID}:${NB_GID} /data
22
 
23
- # Cargar el token de JupyterLab desde el archivo de entorno
24
- source /etc/environment
25
-
26
- # Verificar si el token está presente
27
  if [ -z "$JUPYTER_TOKEN" ]; then
28
- handle_error "El token de JupyterLab está vacío. Asegúrate de configurar el secreto JUPYTER_TOKEN en la configuración del Space."
29
  fi
30
 
31
- # Verificar disponibilidad de GPU
32
  if command -v nvidia-smi &> /dev/null; then
33
- echo "GPU detectada. Configurando entorno para uso de GPU."
34
  export NVIDIA_VISIBLE_DEVICES=all
35
  export NVIDIA_DRIVER_CAPABILITIES=compute,utility
36
  else
37
- echo "No se detectó GPU. Se utilizará CPU."
38
  fi
39
 
40
- # Iniciar JupyterLab en segundo plano
41
  jupyter lab --ip=0.0.0.0 --port=${JUPYTERLAB_PORT} --no-browser --allow-root \
42
- --NotebookApp.base_url=/jupyter --NotebookApp.token=${JUPYTER_TOKEN} \
43
  --notebook-dir=/data &
44
 
45
- # Asegurar que el archivo PID de Nginx sea escribible
46
- touch /tmp/nginx.pid
47
- chmod 644 /tmp/nginx.pid
48
-
49
- # Iniciar Nginx en primer plano
50
- nginx -g "daemon off;" -c /etc/nginx/nginx.conf || handle_error "Fallo al iniciar Nginx"
 
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 HF secret
21
+ JUPYTER_TOKEN=$(cat /run/secrets/JUPYTER_TOKEN)
22
 
23
+ # Verify if the token is empty
 
 
 
24
  if [ -z "$JUPYTER_TOKEN" ]; then
25
+ handle_error "JupyterLab token is empty"
26
  fi
27
 
28
+ # Check for GPU availability
29
  if command -v nvidia-smi &> /dev/null; then
30
+ echo "GPU detected. Configuring environment for GPU usage."
31
  export NVIDIA_VISIBLE_DEVICES=all
32
  export NVIDIA_DRIVER_CAPABILITIES=compute,utility
33
  else
34
+ echo "No GPU detected. CPU will be used."
35
  fi
36
 
37
+ # Start JupyterLab in the background
38
  jupyter lab --ip=0.0.0.0 --port=${JUPYTERLAB_PORT} --no-browser --allow-root \
39
+ --NotebookApp.token=${JUPYTER_TOKEN} \
40
  --notebook-dir=/data &
41
 
42
+ # Start Nginx in the foreground
43
+ nginx -g "daemon off;" || handle_error "Failed to start Nginx"
 
 
 
 
jupyter_config.py CHANGED
@@ -1,9 +1,11 @@
 
 
1
  import os
2
 
3
  c.NotebookApp.ip = '0.0.0.0'
4
  c.NotebookApp.port = 8888
5
  c.NotebookApp.open_browser = False
 
6
  c.NotebookApp.allow_root = True
7
- c.NotebookApp.base_url = '/jupyter'
8
- c.NotebookApp.token = os.environ.get('JUPYTERLAB_TOKEN', '')
9
- c.NotebookApp.notebook_dir = '/data'
 
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'
 
nginx.conf CHANGED
@@ -1,12 +1,13 @@
1
- pid /tmp/nginx.pid;
2
  worker_processes 1;
 
3
 
4
  events {
5
  worker_connections 1024;
6
  }
7
 
8
  http {
9
- include mime.types;
10
  default_type application/octet-stream;
11
 
12
  sendfile on;
 
1
+ user $NB_UID;
2
  worker_processes 1;
3
+ pid /tmp/nginx.pid;
4
 
5
  events {
6
  worker_connections 1024;
7
  }
8
 
9
  http {
10
+ include /etc/nginx/mime.types;
11
  default_type application/octet-stream;
12
 
13
  sendfile on;
requirements.txt CHANGED
@@ -1 +1 @@
1
- jupyterlab
 
1
+ jupyterlab==4.0.6