Ramses II commited on
Commit
da5c857
1 Parent(s): e9576fd
Files changed (3) hide show
  1. Dockerfile +48 -41
  2. entrypoint.sh +42 -6
  3. jupyter_config.py +2 -1
Dockerfile CHANGED
@@ -1,66 +1,73 @@
1
  FROM jupyter/scipy-notebook
2
 
3
- # Create a user with ID 1000
4
- RUN useradd -m -u 1000 user
5
 
6
- # Set environment variables
7
- ENV HOME=/home/user \
8
- PATH=/home/user/.local/bin:$PATH
9
 
10
- # Set working directory
11
- WORKDIR $HOME/app
12
 
13
- # Install system dependencies
14
  USER root
 
 
15
  RUN apt-get update && apt-get install -y \
16
  wget \
17
  nginx \
 
 
 
 
 
18
  && apt-get clean && rm -rf /var/lib/apt/lists/*
19
 
20
- # Switch back to the user
21
- USER user
22
-
23
- # Upgrade pip
24
- RUN pip install --no-cache-dir --upgrade pip
25
-
26
- # Copy requirements and install Python dependencies
27
- COPY --chown=user requirements.txt .
28
- RUN pip install --user --no-cache-dir -r requirements.txt
29
 
30
- # Copy static files
31
- COPY --chown=user public $HOME/app/public
 
 
32
 
33
- # Copy configuration files
34
- COPY --chown=user jupyter_config.py $HOME/.jupyter/jupyter_config.py
35
- COPY --chown=user nginx.conf $HOME/nginx.conf
36
- COPY --chown=user entrypoint.sh $HOME/entrypoint.sh
37
 
38
- # Make entrypoint executable
39
- RUN chmod +x $HOME/entrypoint.sh
40
 
41
- # Create necessary directories for Nginx
42
- USER root
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 user:user /var/lib/nginx /var/log/nginx /var/run /run \
47
  && chmod 755 /var/lib/nginx /var/run /run
48
 
49
- # Create Nginx log files
50
- RUN touch /var/log/nginx/error.log /var/log/nginx/access.log \
51
- && chown user:user /var/log/nginx/error.log /var/log/nginx/access.log
 
 
 
 
 
 
52
 
53
- # Create a directory for persistent data
54
- RUN mkdir -p /data && chown user:user /data && chmod 777 /data
55
 
56
- USER user
 
57
 
58
- # Expose ports
59
- EXPOSE 8888 7860
60
 
61
- # Set environment variables
62
- ENV JUPYTERLAB_PORT=8888 \
63
- NGINX_PORT=7860
64
 
65
- # Run the entrypoint script
66
- ENTRYPOINT ["$HOME/entrypoint.sh"]
 
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
11
 
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
37
+ COPY entrypoint.sh /app/entrypoint.sh
38
 
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
+ # Switch back to the original user
57
+ USER ${NB_UID}
58
 
59
+ # Set the working directory to /data
60
+ WORKDIR /data
61
 
62
+ # Expose the port 8888 for JupyterLab
63
+ EXPOSE 8888
64
 
65
+ # Expose the port 7860 for Nginx
66
+ EXPOSE 7860
67
 
68
+ # Set the environment variables
69
+ ENV JUPYTERLAB_PORT=8888
70
+ ENV NGINX_PORT=7860
71
 
72
+ # Run the entrypoint script when the container starts
73
+ ENTRYPOINT ["/app/entrypoint.sh"]
entrypoint.sh CHANGED
@@ -1,14 +1,50 @@
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
 
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
+ # Leer el token de JupyterLab del secreto de HF
24
  JUPYTERLAB_TOKEN=$(cat /run/secrets/JUPYTER_TOKEN)
25
 
26
+ # Verificar si el token está vacío
27
+ if [ -z "$JUPYTERLAB_TOKEN" ]; then
28
+ handle_error "El token de JupyterLab está vacío"
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=${JUPYTERLAB_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"
jupyter_config.py CHANGED
@@ -5,4 +5,5 @@ 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', '')
 
 
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'