|
|
|
FROM redis:latest |
|
|
|
|
|
RUN mkdir -p /mnt/redis-data && \ |
|
mkdir -p /usr/local/etc/redis && \ |
|
mkdir -p /usr/local/bin && \ |
|
mkdir -p /etc/ngrok && \ |
|
chmod -R 777 /mnt/redis-data /usr/local/etc/redis /usr/local/bin /etc/ngrok |
|
|
|
|
|
RUN apt-get update && \ |
|
apt-get install -y wget unzip python3 python3-venv python3-pip cron curl gnupg2 lsb-release |
|
|
|
|
|
RUN echo "deb http://packages.cloud.google.com/apt gcsfuse-$(lsb_release -c -s) main" | tee /etc/apt/sources.list.d/gcsfuse.list && \ |
|
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \ |
|
apt-get update && \ |
|
apt-get install -y gcsfuse && \ |
|
apt-get clean |
|
|
|
|
|
RUN wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz && \ |
|
tar -xvzf ngrok-v3-stable-linux-amd64.tgz -C /usr/local/bin && \ |
|
chmod +x /usr/local/bin/ngrok && \ |
|
rm ngrok-v3-stable-linux-amd64.tgz |
|
|
|
|
|
RUN python3 -m venv /opt/venv |
|
|
|
|
|
RUN /opt/venv/bin/pip install --upgrade pip && \ |
|
/opt/venv/bin/pip install gcsfs google-cloud-storage |
|
|
|
|
|
RUN echo '#!/bin/bash\n\ |
|
\n\ |
|
# Leer variables de entorno\n\ |
|
BUCKET_NAME="${BUCKET_NAME}"\n\ |
|
GOOGLE_APPLICATION_CREDENTIALS_JSON="${GOOGLE_APPLICATION_CREDENTIALS_JSON}"\n\ |
|
\n\ |
|
# Guardar las credenciales en un archivo temporal\n\ |
|
echo "$GOOGLE_APPLICATION_CREDENTIALS_JSON" > /opt/venv/bin/gcloud-credentials.json\n\ |
|
\n\ |
|
# Intentar montar el bucket de Google Cloud Storage\n\ |
|
gcsfuse --implicit-dirs --key-file /opt/venv/bin/gcloud-credentials.json $BUCKET_NAME /mnt/redis-data\n\ |
|
\n\ |
|
# Verificar si el bucket est谩 montado\n\ |
|
if mountpoint -q /mnt/redis-data\n\ |
|
then\n\ |
|
echo "Bucket montado correctamente."\n\ |
|
else\n\ |
|
echo "Error al montar el bucket. Intentando nuevamente..."\n\ |
|
gcsfuse --implicit-dirs --key-file /opt/venv/bin/gcloud-credentials.json $BUCKET_NAME /mnt/redis-data\n\ |
|
fi\n' > /usr/local/bin/mount_bucket.sh |
|
|
|
|
|
RUN chmod +x /usr/local/bin/mount_bucket.sh |
|
|
|
|
|
RUN echo "* * * * * /usr/local/bin/mount_bucket.sh" >> /etc/crontab |
|
|
|
|
|
RUN echo "dir /mnt/redis-data" >> /usr/local/etc/redis/redis.conf && \ |
|
echo "save 900 1" >> /usr/local/etc/redis/redis.conf && \ |
|
echo "save 300 10" >> /usr/local/etc/redis/redis.conf && \ |
|
echo "save 60 10000" >> /usr/local/etc/redis/redis.conf |
|
|
|
|
|
RUN echo '#!/bin/bash\n\ |
|
\n\ |
|
# Leer variables de entorno\n\ |
|
BUCKET_NAME="${BUCKET_NAME}"\n\ |
|
GOOGLE_APPLICATION_CREDENTIALS_JSON="${GOOGLE_APPLICATION_CREDENTIALS_JSON}"\n\ |
|
REDIS_PASSWORD="${REDIS_PASSWORD}"\n\ |
|
\n\ |
|
# Verificar si Redis est谩 en ejecuci贸n\n\ |
|
if pgrep redis-server > /dev/null\n\ |
|
then\n\ |
|
echo "Redis est谩 en ejecuci贸n. Verificando datos en Redis..."\n\ |
|
\n\ |
|
# Verificar si el bucket est谩 montado\n\ |
|
if mountpoint -q /mnt/redis-data\n\ |
|
then\n\ |
|
echo "Bucket montado correctamente. Verificando datos en Redis..."\n\ |
|
\n\ |
|
# Recuperar claves de Redis\n\ |
|
redis-cli -a ${REDIS_PASSWORD} KEYS "*" | while read key; do\n\ |
|
redis-cli -a ${REDIS_PASSWORD} GET "$key" > /dev/null || echo "Clave $key no recuperada"\n\ |
|
done\n\ |
|
else\n\ |
|
echo "Error al montar el bucket. Intentando nuevamente..."\n\ |
|
gcsfuse --implicit-dirs --key-file /opt/venv/bin/gcloud-credentials.json $BUCKET_NAME /mnt/redis-data\n\ |
|
fi\n\ |
|
else\n\ |
|
echo "Redis no est谩 en ejecuci贸n. Intentando recuperar datos..."\n\ |
|
gcsfuse --implicit-dirs --key-file /opt/venv/bin/gcloud-credentials.json $BUCKET_NAME /mnt/redis-data\n\ |
|
redis-server /usr/local/etc/redis/redis.conf\n\ |
|
fi\n' > /usr/local/bin/recover_data.sh |
|
|
|
|
|
RUN chmod +x /usr/local/bin/recover_data.sh |
|
|
|
|
|
RUN echo "* * * * * /usr/local/bin/recover_data.sh" >> /etc/crontab |
|
|
|
|
|
EXPOSE 7860 |
|
|
|
|
|
RUN echo '#!/bin/bash\n\ |
|
\n\ |
|
# Crear la carpeta /etc/ngrok si no existe\n\ |
|
mkdir -p /etc/ngrok\n\ |
|
touch /etc/ngrok/ngrok.yml\n\ |
|
chmod -R 777 /etc/ngrok\n\ |
|
\n\ |
|
# Iniciar Redis\n\ |
|
redis-server /usr/local/etc/redis/redis.conf &\n\ |
|
\n\ |
|
# Configurar ngrok con el token de autenticaci贸n\n\ |
|
if [ -z "$NGROK_AUTH_TOKEN" ]; then\n\ |
|
echo "No se ha proporcionado un token de autenticaci贸n para ngrok."\n\ |
|
else\n\ |
|
echo "authtoken: $NGROK_AUTH_TOKEN" > /etc/ngrok/ngrok.yml\n\ |
|
/usr/local/bin/ngrok http 7860 &\n\ |
|
fi\n\ |
|
\n\ |
|
# Mantener el contenedor en ejecuci贸n\n\ |
|
while true; do sleep 3600; done\n' > /usr/local/bin/start.sh |
|
|
|
|
|
RUN chmod +x /usr/local/bin/start.sh |
|
|
|
|
|
CMD ["/usr/local/bin/start.sh"] |
|
|
|
|
|
HEALTHCHECK --interval=5m --timeout=3s \ |
|
CMD curl -f http://localhost:7860/ || exit 1 |
|
|