5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-06-03 03:14:16 +00:00
panel/Dockerfile
2019-09-07 16:45:57 -03:00

120 lines
4.2 KiB
Docker

# Este Dockerfile está armado pensando en una compilación lanzada desde
# el mismo repositorio de trabajo. Cuando tengamos CI/CD algunas cosas
# como el tarball van a tener que cambiar porque ya vamos a haber hecho
# un clone/pull limpio.
FROM sutty/sdk-ruby:latest as build
MAINTAINER "f <f@sutty.nl>"
ARG RAILS_MASTER_KEY
# Un entorno base
ENV NOKOGIRI_USE_SYSTEM_LIBRARIES=1
ENV SECRET_KEY_BASE solo_es_necesaria_para_correr_rake
ENV RAILS_ENV production
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY
# Para compilar los assets en brotli
RUN apk add --no-cache brotli libgit2-dev
# Empezamos con la usuaria app creada por sdk-ruby
USER app
# Vamos a trabajar dentro de este directorio
WORKDIR /home/app/sutty
# Copiamos solo el Gemfile para poder instalar las gemas necesarias
COPY --chown=app:www-data ./Gemfile .
COPY --chown=app:www-data ./Gemfile.lock .
# XXX: No usamos la flag --production porque luego no nos deja
# desinstalar las gemas de los assets
# RUN --mount=type=cache,target=/home/app/.ccache \
RUN bundle install --path=./vendor --without test development
# Vaciar la caché
RUN rm vendor/ruby/2.5.0/cache/*.gem
# Limpiar las librerías nativas, esto ahorra más espacio y uso de
# memoria ya que no hay que cargar símbolos que no se van a usar.
RUN find vendor -name "*.so" | xargs -rn 1 strip --strip-unneeded
# Copiar el repositorio git
COPY --chown=app:www-data ./.git/ ./.git/
# Hacer un tarball de los archivos desde el repositorio
RUN git archive -o ../sutty.tar.gz HEAD
# Extraer archivos necesarios para compilar los assets
RUN tar xf ../sutty.tar.gz Rakefile config app bin yarn.lock package.json
# Instalar secretos
COPY --chown=app:www-data ./config/credentials.yml.enc ./config/
# Pre-compilar los assets
RUN bundle exec rake assets:precompile
# Comprimirlos usando brotli
RUN find public/assets -type f -name "*.gz" | sed -re "s/\.gz$//" | xargs -r brotli -k -9
# Eliminar la necesidad de un runtime JS en producción, porque los
# assets ya están pre-compilados.
RUN sed -re "/(uglifier|bootstrap|coffee-rails)/d" -i Gemfile
RUN bundle clean
RUN sed -re "s/def visit\(object\)/def visit(object, collection = nil)/" \
-i `bundle show mobility`/lib/mobility/arel/visitor.rb
# Contenedor final
FROM sutty/monit:latest
ENV RAILS_ENV production
# Instalar las dependencias, separamos la librería de base de datos para
# poder reutilizar este primer paso desde otros contenedores
RUN apk add --no-cache libxslt libxml2 tzdata ruby ruby-bundler ruby-json ruby-bigdecimal ruby-rake
RUN apk add --no-cache sqlite-libs
# Necesitamos yarn para que Jekyll pueda generar los sitios
# XXX: Eliminarlo cuando extraigamos la generación de sitios del proceso
# principal
RUN apk add --no-cache yarn
RUN apk add --no-cache libgit2
# Instalar foreman para poder correr los servicios
RUN gem install --no-document --no-user-install foreman
RUN apk add --no-cache file
# Agregar el grupo del servidor web
RUN addgroup -g 82 -S www-data
# Agregar la usuaria
RUN adduser -s /bin/sh -G www-data -h /srv/http -D app
# Convertirse en app para instalar
USER app
WORKDIR /srv/http
# Traer los archivos y colocarlos donde van definitivamente
COPY --from=build --chown=app:www-data /home/app/sutty.tar.gz /tmp/
RUN tar xf /tmp/sutty.tar.gz
# Publicar el código!
RUN mv /tmp/sutty.tar.gz ./public/
# Traer los assets compilados y las gemas
COPY --from=build /home/app/sutty/public/assets public/assets
COPY --from=build /home/app/sutty/vendor vendor
COPY --from=build /home/app/sutty/.bundle .bundle
COPY --from=build /home/app/sutty/Gemfile Gemfile
COPY --from=build /home/app/sutty/Gemfile.lock Gemfile.lock
# Volver a root para cerrar la compilación
USER root
# Convertir la aplicación en solo lectura
#RUN chown -R root:root /srv/http
#RUN chmod -R o=g /srv/http
#RUN chown -R app:www-data _deploy _sites
# Sincronizar los assets a un directorio compartido
RUN apk add --no-cache rsync
COPY ./sync_assets.sh /usr/local/bin/sync_assets
RUN chmod 755 /usr/local/bin/sync_assets
# Instalar la configuración de monit y comprobarla
RUN install -m 640 -o root -g root ./monit.conf /etc/monit.d/sutty.conf
RUN monit -t
# Mantener estos directorios!
VOLUME "/srv/http/_deploy"
VOLUME "/srv/http/_sites"
VOLUME "/srv/http/_public"
# El puerto de puma
EXPOSE 3000