Przeglądaj źródła

Dockerfile para alpine separado

Celestino Rey 9 miesięcy temu
rodzic
commit
b5819980c9
5 zmienionych plików z 109 dodań i 68 usunięć
  1. 20 25
      Dockerfile
  2. 47 0
      Dockerfile.alpine
  3. 0 40
      Dockerfile.singlebuild
  4. 3 3
      K8S/Makefile
  5. 39 0
      src/config/entrypoint-alpine.sh

+ 20 - 25
Dockerfile

@@ -1,30 +1,21 @@
 # yup, python 3.11!
-FROM python:3.11-alpine as builder
+FROM python:3.11-slim
 
 # install nginx y otras cosas
-RUN apk add nginx netcat-openbsd curl vim jq rsync pango pango-dev cairo postgresql-client bash glib fontconfig font-noto
-
-COPY ./nginx/default.conf /etc/nginx/nginx.conf
-COPY ./src/requirements.txt requirements.txt
-RUN pip install --user --no-cache-dir -r requirements.txt
+RUN apt-get update && apt-get install nginx netcat-openbsd curl vim jq rsync libpango-1.0-0 libpangocairo-1.0-0 libcairo2 postgresql-client -y
 
+# copy our nginx configuration to overwrite nginx defaults
+RUN rm /etc/nginx/sites-enabled/default
+RUN rm /etc/nginx/sites-available/default
+COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
 # link nginx logs to container stdout
-RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
-    ln -sf /dev/stderr /var/log/nginx/error.log && \
-    chgrp -R 0 /var/lib/nginx && \
-    chmod -R g=u /var/lib/nginx
-
-FROM python:3.11-alpine as main
-
-COPY --from=builder /root/.local /root/.local
-
-ENV PATH="/root/.local/bin:$PATH"
+RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log
 
 # copy the django code
 COPY ./src ./app
 
-RUN chgrp -R 0 ./app && \
-    chmod -R g=u ./app
+RUN chgrp -R 0 ./app && chmod -R g=u ./app
+RUN chgrp -R 0 /var/lib/nginx && chmod -R g=u /var/lib/nginx
 
 # change our working directory to the django projcet roo
 WORKDIR /app
@@ -32,14 +23,18 @@ WORKDIR /app
 # create virtual env (notice the location?)
 # update pip
 # install requirements
-#RUN python -m venv /opt/venv && \
-    #/opt/venv/bin/python -m pip install pip --upgrade && \
-    #/opt/venv/bin/python -m pip install -r requirements.txt && \
-    #echo "export PATH=/opt/venv/bin/:$PATH" >> /root/.bashrc && \
-    #chmod +x config/entrypoint.sh
+RUN python -m venv /opt/venv && \
+    /opt/venv/bin/python -m pip install pip --upgrade && \
+    /opt/venv/bin/python -m pip install -r requirements.txt
+
+# Añade path a .bashrc
+
+RUN echo "export PATH=/opt/venv/bin/:$PATH" >> /root/.bashrc
+
+# make our entrypoint.sh executable
+RUN chmod +x config/entrypoint.sh
 
 #EXPOSE 8080
 
 # execute our entrypoint.sh file
-# CMD ["./config/entrypoint.sh"]
-CMD ["gunicorn", "misfinanzas.wsgi:application", "--bind", "0.0.0.0:8000", "--access-logfile", "-",  "--error-logfile",  "-", "--log-level", "info"]
+CMD ["./config/entrypoint.sh"]

+ 47 - 0
Dockerfile.alpine

@@ -0,0 +1,47 @@
+# yup, python 3.11!
+FROM python:3.11-alpine as builder
+
+# install nginx y otras cosas
+RUN apk add nginx netcat-openbsd curl vim jq rsync pango pango-dev cairo postgresql-client bash glib fontconfig font-noto
+
+COPY ./nginx/default.conf /etc/nginx/nginx.conf
+COPY ./src/requirements.txt requirements.txt
+RUN pip install --user --no-cache-dir -r requirements.txt
+
+# link nginx logs to container stdout
+RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
+    ln -sf /dev/stderr /var/log/nginx/error.log && \
+    chgrp -R 0 /var/lib/nginx && \
+    chmod -R g=u /var/lib/nginx
+
+FROM python:3.11-alpine as main
+
+RUN apk add nginx netcat-openbsd curl vim jq rsync pango pango-dev cairo postgresql-client bash glib fontconfig font-noto
+
+COPY --from=builder /root/.local /root/.local
+
+ENV PATH="/root/.local/bin:$PATH"
+
+# copy the django code
+COPY ./src ./app
+
+RUN chgrp -R 0 ./app && \
+    chmod -R g=u ./app
+
+# change our working directory to the django projcet roo
+WORKDIR /app
+
+# create virtual env (notice the location?)
+# update pip
+# install requirements
+#RUN python -m venv /opt/venv && \
+    #/opt/venv/bin/python -m pip install pip --upgrade && \
+    #/opt/venv/bin/python -m pip install -r requirements.txt && \
+    #echo "export PATH=/opt/venv/bin/:$PATH" >> /root/.bashrc && \
+    #chmod +x config/entrypoint.sh
+
+#EXPOSE 8080
+
+# execute our entrypoint.sh file
+CMD ["/app/config/entrypoint-alpine.sh"]
+# CMD ["gunicorn", "misfinanzas.wsgi:application", "--bind", "0.0.0.0:8000", "--access-logfile", "-",  "--error-logfile",  "-", "--log-level", "info"]

+ 0 - 40
Dockerfile.singlebuild

@@ -1,40 +0,0 @@
-# yup, python 3.11!
-FROM python:3.11-slim
-
-# install nginx y otras cosas
-RUN apt-get update && apt-get install nginx netcat-openbsd curl vim jq rsync libpango-1.0-0 libpangocairo-1.0-0 libcairo2 postgresql-client -y
-
-# copy our nginx configuration to overwrite nginx defaults
-RUN rm /etc/nginx/sites-enabled/default
-RUN rm /etc/nginx/sites-available/default
-COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
-# link nginx logs to container stdout
-RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log
-
-# copy the django code
-COPY ./src ./app
-
-RUN chgrp -R 0 ./app && chmod -R g=u ./app
-RUN chgrp -R 0 /var/lib/nginx && chmod -R g=u /var/lib/nginx
-
-# change our working directory to the django projcet roo
-WORKDIR /app
-
-# create virtual env (notice the location?)
-# update pip
-# install requirements
-RUN python -m venv /opt/venv && \
-    /opt/venv/bin/python -m pip install pip --upgrade && \
-    /opt/venv/bin/python -m pip install -r requirements.txt
-
-# Añade path a .bashrc
-
-RUN echo "export PATH=/opt/venv/bin/:$PATH" >> /root/.bashrc
-
-# make our entrypoint.sh executable
-RUN chmod +x config/entrypoint.sh
-
-#EXPOSE 8080
-
-# execute our entrypoint.sh file
-CMD ["./config/entrypoint.sh"]

+ 3 - 3
K8S/Makefile

@@ -1,7 +1,7 @@
 export REGISTRY=registry.reymota.es
-export IMG_VERSION = alpine-4
-export PUERTO = 30347
-export NAMESPACE = fdev
+export IMG_VERSION = alpine-9
+export PUERTO = 30346
+export NAMESPACE = finanzas
 
 # limpia todo
 all: imagen clean install

+ 39 - 0
src/config/entrypoint-alpine.sh

@@ -0,0 +1,39 @@
+#!/bin/bash
+RUN_PORT="8000"
+
+if [ "$DATABASE" = "postgres" ]
+then
+    echo "Esperando a postgres..."
+
+    while ! nc -z $SQL_HOST $SQL_PORT; do
+      sleep 0.1
+    done
+
+    echo "PostgreSQL ha arrancado"
+
+	python manage.py migrate --no-input
+	python manage.py collectstatic --no-input
+
+	#/opt/venv/bin/gunicorn cartera_acciones.wsgi:application --bind "0.0.0.0:${RUN_PORT}" --daemon
+
+  # Start nginx as a daemon so it doesn't block, then run gunicorn in
+  # foreground (PID 1) so logs go to stdout/stderr and are visible to kubectl logs
+  nginx
+
+  # Determine log level
+  if [ "$DEBUG" = "True" ] || [ "$DEBUG" = "1" ]; then
+    LOG_LEVEL=debug
+  else
+    LOG_LEVEL=info
+  fi
+
+  echo "Lanzando gunicorn con log level: $LOG_LEVEL"
+
+  exec gunicorn misfinanzas.wsgi:application \
+    --bind "0.0.0.0:${RUN_PORT}" \
+    --access-logfile - \
+    --error-logfile - \
+    --log-level "$LOG_LEVEL"
+else
+    echo "la base de datos no es postgres: '$DATABASE'"
+fi