Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Kagemaru committed Jul 14, 2023
1 parent ebe6e19 commit 4064daa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 99 deletions.
18 changes: 11 additions & 7 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
.bundle
tmp
log
dist
node_modules

frontend/node_modules
frontend/tmp
uploads/person/picture/
frontend/dist

.yarn/cache
spec/coverage
tmp
log
spec/reports
uploads/person/picture/
vendor/bundle
vendor/cache
.yarn/cache
node_modules
dist
spec/reports
134 changes: 42 additions & 92 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,20 @@

# Versioning
ARG RUBY_VERSION="3.2"
ARG BUNDLER_VERSION="2.4.6"
ARG NODEJS_VERSION="16"
ARG YARN_VERSION="1.22.19"

# Packages
ARG BUILD_PACKAGES="libpq-dev npm build-essential"
ARG BUILD_PACKAGES="libpq-dev build-essential"
ARG RUN_PACKAGES="git imagemagick libpq5 libjemalloc-dev libjemalloc2"

# Scripts
ARG PRE_INSTALL_SCRIPT=" \
curl -sL https://deb.nodesource.com/setup_${NODEJS_VERSION}.x -o /tmp/nodesource_setup.sh \
&& bash /tmp/nodesource_setup.sh \
"
ARG INSTALL_SCRIPT="node -v && npm -v && npm install -g yarn && yarn set version ${YARN_VERSION}"
ARG PRE_BUILD_SCRIPT=" \
# The newer ruby images are delivered with Python 3 which breaks the node-sass build
# to counteract this, we build Python 2 ourselves
"
ARG BUILD_SCRIPT="yarn install"
ARG POST_BUILD_SCRIPT=" \
cd frontend \
&& yarn install --no-progress \
&& yarn build-prod \
&& mv -v dist/* ../public \
&& echo \"(built at: $(date '+%Y-%m-%d %H:%M:%S'))\" > /${HOME}/BUILD_INFO \
"
ARG PRE_INSTALL_SCRIPT
ARG INSTALL_SCRIPT
ARG PRE_BUILD_SCRIPT
ARG BUILD_SCRIPT
ARG POST_BUILD_SCRIPT="echo \"(built at: $(date '+%Y-%m-%d %H:%M:%S'))\" > /${HOME}/BUILD_INFO"

# Bundler specific
ARG BUNDLE_WITHOUT="development:metrics:test"
ARG BUNDLE_WITHOUT_GROUPS="development:metrics:test"

# App specific
ARG RAILS_ENV="production"
Expand All @@ -42,7 +27,7 @@ ARG RAILS_HOST_NAME="unused.example.net"
ARG SECRET_KEY_BASE="needs-to-be-set"

# Runtime ENV vars
ARG HOME=/app-src
ARG HOME="/app-src"
ARG PS1="[\${SENTRY_CURRENT_ENV}] `uname -n`:\${PWD}\$ "
ARG TZ="Europe/Zurich"

Expand All @@ -56,9 +41,8 @@ FROM ruby:${RUBY_VERSION} AS build
ARG PRE_INSTALL_SCRIPT
ARG BUILD_PACKAGES
ARG INSTALL_SCRIPT
ARG BUNDLER_VERSION
ARG PRE_BUILD_SCRIPT
ARG BUNDLE_WITHOUT
ARG BUNDLE_WITHOUT_GROUPS
ARG BUILD_SCRIPT
ARG POST_BUILD_SCRIPT

Expand All @@ -77,10 +61,6 @@ SHELL ["/bin/bash", "-c"]
# Use root user
USER root

# OLD:
# # Get proper node version via nodesource
# RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash -

RUN bash -vxc "${PRE_INSTALL_SCRIPT:-"echo 'no PRE_INSTALL_SCRIPT provided'"}"

# Install dependencies
Expand All @@ -89,64 +69,26 @@ RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends ${BUILD_PACKAGES}

# OLD:
# RUN apt-get install -y npm

RUN bash -vxc "${INSTALL_SCRIPT:-"echo 'no INSTALL_SCRIPT provided'"}"

# Install specific versions of dependencies
RUN gem install bundler:${BUNDLER_VERSION} --no-document

# TODO: Load artifacts

# OLD:
# # set up app-src directory
# COPY . /app-src
# WORKDIR /app-src

# set up app-src directory
WORKDIR ${HOME}
COPY Gemfile Gemfile.lock ./

RUN bash -vxc "${PRE_BUILD_SCRIPT:-"echo 'no PRE_BUILD_SCRIPT provided'"}"

# TODO: Move to PRE_BUILD_SCRIPT
RUN apt-get install -y --no-install-recommends \
checkinstall libreadline-dev libncursesw5-dev libssl-dev \
libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
RUN curl https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz -o /tmp/python.tgz
RUN mkdir /tmp/python2
RUN tar xzf /tmp/python.tgz -C /tmp/python2
RUN apt-get remove -y python python3
RUN cd /tmp/python2 && mv **/* . && ls -hals
RUN cd /tmp/python2 && ./configure --prefix=/usr/bin
RUN cd /tmp/python2 && make -j8

# Cached ^-----^
RUN apt-get install -y npm
RUN echo $PATH && cd /tmp/python2 && make install -j8

# install gems and build the app
RUN bundle config set --local deployment 'true' \
&& bundle config set --local without ${BUNDLE_WITHOUT} \
&& bundle package \
# install bundler, gems and build the app
RUN gem install bundler \
&& bundle config set --local deployment 'true' \
&& bundle config set --local without ${BUNDLE_WITHOUT_GROUPS} \
&& bundle install \
&& bundle clean \
&& bundle exec bootsnap precompile --gemfile

COPY . .

# These args might be used by the build script and have to be specified
# from outside of the build. They change with each build, so only define them
# here for optimal layer caching.
# Also see https://docs.docker.com/engine/reference/builder/#impact-on-build-caching
# ARG OPENSHIFT_BUILD_COMMIT
# ARG OPENSHIFT_BUILD_SOURCE
# ARG OPENSHIFT_BUILD_REFERENCE
# ARG BUILD_COMMIT="${OPENSHIFT_BUILD_COMMIT}"
# ARG BUILD_REPO="${OPENSHIFT_BUILD_SOURCE}"
# ARG BUILD_REF="${OPENSHIFT_BUILD_REFERENCE}"

RUN bash -vxc "${BUILD_SCRIPT:-"echo 'no BUILD_SCRIPT provided'"}"

RUN bash -vxc "${POST_BUILD_SCRIPT:-"echo 'no POST_BUILD_SCRIPT provided'"}"
Expand All @@ -157,35 +99,41 @@ RUN bundle exec bootsnap precompile app/ lib/

RUN rm -rf vendor/cache/ .git spec/ node_modules/

#################################
# Frontend Build Stage #
#################################

# Selected for the specific ember / node combination
FROM danlynn/ember-cli:3.28.2-node_14.18 AS frontend-build

WORKDIR /myapp
COPY --from=build /app-src/frontend /myapp
RUN yarn install --frozen-lockfile --no-progress && yarn build-prod

#################################
# Run Stage #
#################################

# This image will be replaced by Openshift
FROM ruby:${RUBY_VERSION}-slim AS app
FROM ruby:${RUBY_VERSION}-slim AS run

# arguments for steps
ARG RUN_PACKAGES
ARG BUNDLER_VERSION
ARG BUNDLE_WITHOUT
ARG BUNDLE_WITHOUT_GROUPS

# arguments potentially used by steps
ARG HOME
ARG NODE_ENV
ARG PS1
ARG RACK_ENV
ARG RAILS_ENV

# data persisted in the image
ARG PS1
ARG TZ

# data persisted in the image
ENV HOME="${HOME}" \
PATH="${HOME}/bin:${PATH}" \
PS1="${PS1}" \
TZ="${TZ}" \
BUILD_REPO="${BUILD_REPO}" \
BUILD_REF="${BUILD_REF}" \
BUILD_COMMIT="${BUILD_COMMIT}" \
NODE_ENV="${NODE_ENV}" \
RAILS_ENV="${RAILS_ENV}" \
RACK_ENV="${RACK_ENV}" \
Expand All @@ -207,32 +155,37 @@ RUN export DEBIAN_FRONTEND=noninteractive \
&& truncate -s 0 /var/log/*log

# Copy deployment ready source code from build
COPY --from=build ${HOME} ${HOME}
WORKDIR ${HOME}
COPY --from=build ${HOME} ${HOME}

# Copy from additional, app specific stages
COPY --from=frontend-build /myapp/dist/* ${HOME}/public

# Create pids folder for puma and
# set group permissions to folders that need write permissions.
# Beware that docker builds on OpenShift produce different permissions
# than local docker builds!
RUN mkdir -p ${HOME}/tmp/pids \
RUN mkdir -p ${HOME}/log \
&& mkdir -p ${HOME}/tmp/pids \
&& chgrp 0 ${HOME} \
&& chgrp -R 0 ${HOME}/tmp \
&& chgrp -R 0 ${HOME}/log \
&& chgrp -R 0 ${HOME}/tmp \
&& chmod u+w,g=u ${HOME} \
&& chmod -R u+w,g=u ${HOME}/tmp \
&& chmod -R u+w,g=u ${HOME}/log
&& chmod -R u+w,g=u ${HOME}/log \
&& chmod -R u+w,g=u ${HOME}/tmp

# # Install specific versions of dependencies
# RUN gem install bundler:${BUNDLER_VERSION} --no-document
# Set runtime user (although OpenShift uses a custom user per project instead)
USER 1001

# Use cached gems
RUN bundle config set --local deployment 'true' \
&& bundle config set --local without ${BUNDLE_WITHOUT}
&& bundle config set --local without ${BUNDLE_WITHOUT_GROUPS}

# TODO: Move to docs
# These args contain build information. Also see build stage.
# They change with each build, so only define them here for optimal layer caching.
# Also see https://docs.docker.com/engine/reference/builder/#impact-on-build-caching
# Openshift specific
# Openshift specific / Cache busting
ARG BUILD_REPO
ARG BUILD_REF
ARG BUILD_COMMIT
Expand All @@ -243,7 +196,4 @@ ENV BUILD_REPO="${BUILD_REPO}" \
BUILD_COMMIT="${BUILD_COMMIT}" \
BUILD_DATE="${BUILD_DATE}"

# Set runtime user (although OpenShift uses a custom user per project instead)
USER 1001

CMD ["bundle", "exec", "puma", "-t", "8"]

0 comments on commit 4064daa

Please sign in to comment.