Yjhhh commited on
Commit
a8861d8
verified
1 Parent(s): 46e6149

Create Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +142 -0
Dockerfile ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Usa una imagen base de Redis
2
+ FROM redis:latest
3
+
4
+ # Crear directorios necesarios
5
+ RUN mkdir -p /mnt/redis-data && \
6
+ mkdir -p /usr/local/etc/redis && \
7
+ mkdir -p /usr/local/bin && \
8
+ mkdir -p /root/.config/ngrok && \
9
+ chmod -R 777 /mnt/redis-data /usr/local/etc/redis /usr/local/bin /root /root/.config
10
+
11
+ # Instalaci贸n de herramientas necesarias
12
+ RUN apt-get update && \
13
+ apt-get install -y wget unzip python3 python3-venv python3-pip cron curl gnupg2 lsb-release
14
+
15
+ # Agregar repositorio de Google Cloud y gcsfuse
16
+ RUN echo "deb http://packages.cloud.google.com/apt gcsfuse-$(lsb_release -c -s) main" | tee /etc/apt/sources.list.d/gcsfuse.list && \
17
+ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \
18
+ apt-get update && \
19
+ apt-get install -y gcsfuse && \
20
+ apt-get clean
21
+
22
+ # Descargar e instalar ngrok v3
23
+ RUN wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz && \
24
+ tar -xvzf ngrok-v3-stable-linux-amd64.tgz -C /usr/local/bin && \
25
+ chmod +x /usr/local/bin/ngrok && \
26
+ rm ngrok-v3-stable-linux-amd64.tgz
27
+
28
+ # Crear un entorno virtual para Python
29
+ RUN python3 -m venv /opt/venv
30
+
31
+ # Activar el entorno virtual e instalar paquetes Python
32
+ RUN /opt/venv/bin/pip install --upgrade pip && \
33
+ /opt/venv/bin/pip install gcsfs google-cloud-storage
34
+
35
+ # Agregar el script de montaje del bucket
36
+ RUN echo '#!/bin/bash\n\
37
+ \n\
38
+ # Leer variables de entorno\n\
39
+ BUCKET_NAME="${BUCKET_NAME}"\n\
40
+ GOOGLE_APPLICATION_CREDENTIALS_JSON="${GOOGLE_APPLICATION_CREDENTIALS_JSON}"\n\
41
+ \n\
42
+ # Guardar las credenciales en un archivo temporal\n\
43
+ echo "$GOOGLE_APPLICATION_CREDENTIALS_JSON" > /opt/venv/bin/gcloud-credentials.json\n\
44
+ \n\
45
+ # Intentar montar el bucket de Google Cloud Storage\n\
46
+ gcsfuse --implicit-dirs --key-file /opt/venv/bin/gcloud-credentials.json $BUCKET_NAME /mnt/redis-data\n\
47
+ \n\
48
+ # Verificar si el bucket est谩 montado\n\
49
+ if mountpoint -q /mnt/redis-data\n\
50
+ then\n\
51
+ echo "Bucket montado correctamente."\n\
52
+ else\n\
53
+ echo "Error al montar el bucket. Intentando nuevamente..."\n\
54
+ gcsfuse --implicit-dirs --key-file /opt/venv/bin/gcloud-credentials.json $BUCKET_NAME /mnt/redis-data\n\
55
+ fi\n' > /usr/local/bin/mount_bucket.sh
56
+
57
+ # Asignar permisos de ejecuci贸n al script
58
+ RUN chmod +x /usr/local/bin/mount_bucket.sh
59
+
60
+ # Agregar cron job para montar el bucket y recuperar datos
61
+ RUN echo "* * * * * /usr/local/bin/mount_bucket.sh" >> /etc/crontab
62
+
63
+ # Configura Redis para usar Google Cloud Storage como persistencia
64
+ RUN echo "dir /mnt/redis-data" >> /usr/local/etc/redis/redis.conf && \
65
+ echo "save 900 1" >> /usr/local/etc/redis/redis.conf && \
66
+ echo "save 300 10" >> /usr/local/etc/redis/redis.conf && \
67
+ echo "save 60 10000" >> /usr/local/etc/redis/redis.conf
68
+
69
+ # Configuraci贸n de la contrase帽a en Redis (corregida)
70
+ ARG REDIS_PASSWORD
71
+ RUN echo "requirepass ${REDIS_PASSWORD}" >> /usr/local/etc/redis/redis.conf
72
+
73
+ # Crear el script de recuperaci贸n
74
+ RUN echo '#!/bin/bash\n\
75
+ \n\
76
+ # Leer variables de entorno\n\
77
+ BUCKET_NAME="${BUCKET_NAME}"\n\
78
+ GOOGLE_APPLICATION_CREDENTIALS_JSON="${GOOGLE_APPLICATION_CREDENTIALS_JSON}"\n\
79
+ \n\
80
+ # Verificar si Redis est谩 en ejecuci贸n\n\
81
+ if pgrep redis-server > /dev/null\n\
82
+ then\n\
83
+ echo "Redis est谩 en ejecuci贸n. Verificando datos en Redis..."\n\
84
+ \n\
85
+ # Verificar si el bucket est谩 montado\n\
86
+ if mountpoint -q /mnt/redis-data\n\
87
+ then\n\
88
+ echo "Bucket montado correctamente. Verificando datos en Redis..."\n\
89
+ \n\
90
+ # Recuperar claves de Redis\n\
91
+ redis-cli -a ${REDIS_PASSWORD} KEYS "*" | while read key; do\n\
92
+ redis-cli -a ${REDIS_PASSWORD} GET "$key" > /dev/null || echo "Clave $key no recuperada"\n\
93
+ done\n\
94
+ else\n\
95
+ echo "Error al montar el bucket. Intentando nuevamente..."\n\
96
+ gcsfuse --implicit-dirs --key-file /opt/venv/bin/gcloud-credentials.json $BUCKET_NAME /mnt/redis-data\n\
97
+ fi\n\
98
+ else\n\
99
+ echo "Redis no est谩 en ejecuci贸n. Intentando recuperar datos..."\n\
100
+ gcsfuse --implicit-dirs --key-file /opt/venv/bin/gcloud-credentials.json $BUCKET_NAME /mnt/redis-data\n\
101
+ redis-server /usr/local/etc/redis/redis.conf\n\
102
+ fi\n' > /usr/local/bin/recover_data.sh
103
+
104
+ # Asignar permisos de ejecuci贸n al script de recuperaci贸n
105
+ RUN chmod +x /usr/local/bin/recover_data.sh
106
+
107
+ # Agregar cron job para monitorear fallos y recuperar datos
108
+ RUN echo "* * * * * /usr/local/bin/recover_data.sh" >> /etc/crontab
109
+
110
+ # Exponer el puerto necesario para Hugging Face Spaces
111
+ EXPOSE 7860
112
+
113
+ # Crear y configurar el script de inicio para Redis, ngrok y configuraci贸n
114
+ RUN echo '#!/bin/bash\n\
115
+ \n\
116
+ # Crear la carpeta /root/.config/ngrok si no existe\n\
117
+ mkdir -p /root/.config/ngrok\n\
118
+ chmod -R 777 /root/.config/ngrok\n\
119
+ \n\
120
+ # Iniciar Redis\n\
121
+ redis-server /usr/local/etc/redis/redis.conf &\n\
122
+ \n\
123
+ # Configurar ngrok con el token de autenticaci贸n\n\
124
+ if [ -z "$NGROK_AUTH_TOKEN" ]; then\n\
125
+ echo "No se ha proporcionado un token de autenticaci贸n para ngrok."\n\
126
+ else\n\
127
+ echo "authtoken: $NGROK_AUTH_TOKEN" > /root/.config/ngrok/ngrok.yml\n\
128
+ /usr/local/bin/ngrok http 7860 &\n\
129
+ fi\n\
130
+ \n\
131
+ # Mantener el contenedor en ejecuci贸n\n\
132
+ while true; do sleep 3600; done\n' > /usr/local/bin/start.sh
133
+
134
+ # Asignar permisos de ejecuci贸n al script de inicio
135
+ RUN chmod +x /usr/local/bin/start.sh
136
+
137
+ # Configura el contenedor para ejecutar el script de inicio
138
+ CMD ["/usr/local/bin/start.sh"]
139
+
140
+ # Configurar el contenedor para que se reinicie siempre
141
+ HEALTHCHECK --interval=5m --timeout=3s \
142
+ CMD curl -f http://0.0.0.0:7860/ || exit 1