Skip to content

Commit

Permalink
Use modern Dockerfile, switch from Yarn -> npm
Browse files Browse the repository at this point in the history
  • Loading branch information
hulloitskai committed Sep 14, 2024
1 parent b19c164 commit f8d25be
Show file tree
Hide file tree
Showing 11 changed files with 13,167 additions and 7,864 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version-file: .node-version
cache: yarn
cache-dependency-path: "**/yarn.lock"
cache: npm
- name: Set up Overmind
run: |
sudo apt-get install tmux \
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

. "$(dirname "$0")/_/husky.sh"

bin/yarn lint-staged
npx lint-staged
219 changes: 115 additions & 104 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,125 +1,136 @@
# Declare arguments, with default values
ARG DISTRO_NAME=bullseye
ARG RUBY_VERSION=3.2.2
ARG NODE_MAJOR_VERSION=18
ARG YARN_VERSION=1.22.19
ARG POSTGRES_MAJOR_VERSION=14
ARG OVERMIND_VERSION=2.4.0
ARG CHROME_VERSION=stable

# Configure base image
FROM ruby:$RUBY_VERSION-slim-$DISTRO_NAME

# Re-declare arguments, since they are reset by the FROM instructions
#
# See: https://github.com/moby/moby/issues/34129
ARG DISTRO_NAME
ARG RUBY_VERSION
ARG NODE_MAJOR_VERSION
ARG YARN_VERSION
ARG POSTGRES_MAJOR_VERSION
ARG OVERMIND_VERSION
ARG DEBIAN_FRONTEND=noninteractive

# Install curl
RUN apt-get update -qq \
&& apt-get install -yq --no-install-recommends curl \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log

# Install Bundler
ENV LANG=C.UTF-8 BUNDLE_JOBS=4 BUNDLE_RETRY=3 BUNDLE_APP_CONFIG=.bundle
RUN gem update --system && gem install bundler

# Install jemalloc
RUN apt-get update \
&& apt-get install -y --no-install-recommends libjemalloc2 \
&& rm -rf /var/lib/apt/lists/*
ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2

# Install NodeJS and Yarn
RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR_VERSION.x | bash -
RUN apt-get update -qq \
&& apt-get -yq dist-upgrade \
&& apt-get install -yq --no-install-recommends nodejs \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
RUN npm install -g yarn@$YARN_VERSION
# syntax = docker/dockerfile:1.2


# == System ==
FROM debian:bookworm-slim AS sys
ENV OVERMIND_VERSION=2.5.1
ENV STARSHIP_VERSION=1.20.1
ENV DEVTOOLS="vim less"
ENV APPLICATION_DEPS="libvips"

# Configure workdir
WORKDIR /app

# Ensure packages are cached
RUN rm /etc/apt/apt.conf.d/docker-clean

# Install runtime programs and dependencies
RUN --mount=type=cache,target=/var/cache,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
apt-get update -yq && \
echo "ca-certificates tmux $DEVTOOLS $APPLICATION_DEPS" | xargs apt-get install -yq --no-install-recommends && \
apt-get purge -yq --auto-remove -o APT::AutoRemove::RecommendsImportant=false llvm && \
rm -r /var/log/* && \
tmux -V

# Install Ruby and Bundler
COPY .ruby-version ./
ENV LANG=C.UTF-8 GEM_HOME=/usr/local/bundle
ENV BUNDLE_SILENCE_ROOT_WARNING=1 BUNDLE_APP_CONFIG="$GEM_HOME" PATH="$GEM_HOME/bin:$PATH"
RUN --mount=type=cache,target=/var/cache,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
BUILD_DEPS="git curl build-essential zlib1g-dev libssl-dev libgmp-dev libyaml-dev libjemalloc-dev" set -eux && \
RUNTIME_DEPS="libyaml-0-2 libjemalloc2" && \
apt-get update -yq && \
echo $BUILD_DEPS $RUNTIME_DEPS | xargs apt-get install -yq --no-install-recommends; \
git clone --depth 1 https://github.com/rbenv/ruby-build.git && \
PREFIX=/tmp ./ruby-build/install.sh && \
mkdir -p "$GEM_HOME" && chmod 1777 "$GEM_HOME" && \
RUBY_CONFIGURE_OPTS=--with-jemalloc /tmp/bin/ruby-build "$(cat .ruby-version)" /usr/local && \
echo $BUILD_DEPS | xargs apt-get purge -yq --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \
rm -r ./ruby-build /tmp/* /var/log/* && \
ruby --version && gem --version && bundle --version

# Install NodeJS
COPY .node-version ./
ENV NODE_ENV=production
RUN --mount=type=cache,target=/var/cache,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
BUILD_DEPS="git curl" set -eux && \
apt-get update -yq && \
echo $BUILD_DEPS | xargs apt-get install -yq --no-install-recommends; \
git clone --depth 1 https://github.com/nodenv/node-build.git && \
PREFIX=/tmp ./node-build/install.sh && \
/tmp/bin/node-build "$(cat .node-version)" /usr/local && \
echo $BUILD_DEPS | xargs apt-get purge -yq --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \
rm -r ./node-build /var/log/* && \
node --version && npm --version

# Install Overmind
RUN apt-get update -qq \
&& apt-get install -yq --no-install-recommends tmux \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log \
&& curl -Lo /usr/bin/overmind.gz https://github.com/DarthSim/overmind/releases/download/v$OVERMIND_VERSION/overmind-v$OVERMIND_VERSION-linux-amd64.gz \
&& gzip -d /usr/bin/overmind.gz \
&& chmod u+x /usr/bin/overmind

# Install Postgres client
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo deb http://apt.postgresql.org/pub/repos/apt/ $DISTRO_NAME-pgdg main $POSTGRES_MAJOR_VERSION > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update -qq \
&& apt-get -yq dist-upgrade \
&& apt-get install -yq --no-install-recommends libpq-dev postgresql-client-$POSTGRES_MAJOR_VERSION \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log

# Install Google Chrome
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main >> /etc/apt/sources.list.d/google-chrome.list
RUN apt-get -y update -qq \
&& apt-get -yq dist-upgrade \
&& apt-get install -yq --no-install-recommends google-chrome-stable \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log

# Install programs
COPY Aptfile /tmp/Aptfile
RUN apt-get update -qq \
&& apt-get -yq dist-upgrade \
&& apt-get install -yq --no-install-recommends $(grep -Ev '^\s*#' /tmp/Aptfile | xargs) \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log

# Install Starship
COPY starship.toml /root/.config/starship.toml
RUN curl -sS https://starship.rs/install.sh | sh -s -- --yes
RUN --mount=type=cache,target=/var/cache,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
BUILD_DEPS="curl" set -eux && \
apt-get update -yq && \
echo $BUILD_DEPS | xargs apt-get install -yq --no-install-recommends; \
curl -Lo /usr/bin/overmind.gz https://github.com/DarthSim/overmind/releases/download/v$OVERMIND_VERSION/overmind-v$OVERMIND_VERSION-linux-amd64.gz && \
gzip -d /usr/bin/overmind.gz && \
chmod u+x /usr/bin/overmind && \
echo $BUILD_DEPS | xargs apt-get purge -yq --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \
rm -r /var/log/* && \
overmind --version

# Configure shell
COPY .zcustomizations .inputrc /root/
RUN echo '\n. "$HOME/.zcustomizations"' >> ~/.zshrc && chsh -s /bin/zsh
ENV SHELL=/bin/bash
RUN --mount=type=cache,target=/var/cache,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
BUILD_DEPS="curl" set -eux && \
apt-get update -yq && \
echo $BUILD_DEPS | xargs apt-get install -yq --no-install-recommends; \
curl -sS https://starship.rs/install.sh | sh -s -- -y -v="v$STARSHIP_VERSION" && \
echo $BUILD_DEPS | xargs apt-get purge -yq --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \
rm -r /tmp/* /var/log/* && \
starship --version
COPY .bash_profile .inputrc /root/
COPY starship.toml /root/.config/starship.toml

# Configure workdir and environment
WORKDIR /app
ENV BUNDLE_WITHOUT="development test" RAILS_ENV=production RAILS_LOG_TO_STDOUT=true NODE_ENV=$RAILS_ENV

# Copy dependency lists
COPY Gemfile Gemfile.lock package.json yarn.lock ./
# == Dependencies ==
FROM sys AS deps

# Install Ruby dependencies
COPY Gemfile Gemfile.lock ./
ENV BUNDLE_WITHOUT="development test"
RUN --mount=type=cache,target=/var/cache,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
BUILD_DEPS="build-essential libreadline-dev libjemalloc-dev libpq-dev" \
RUNTIME_DEPS="libpq5" set -eux && \
apt-get update -yq && \
echo $BUILD_DEPS $RUNTIME_DEPS | xargs apt-get install -yq --no-install-recommends; \
BUNDLE_IGNORE_MESSAGES=1 bundle install && \
echo $BUILD_DEPS | xargs apt-get purge -yq --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \
rm -r /var/log/*

# Install dependencies
RUN bundle install && yarn install
# Install NodeJS dependencies
COPY package.json package-lock.json ./
ENV NODE_ENV=production
RUN --mount=type=cache,target=/root/.npm,sharing=locked \
npm install && \
rm -r /tmp/*


# == Application ==
FROM deps AS app
ENV PORT=3000

# Copy application code
COPY . ./

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile --gemfile app/ lib/

# Configure application environment
ENV RAILS_ENV=production RAILS_LOG_TO_STDOUT=true MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true"

# Precompile assets
RUN bundle exec rails assets:precompile RAILS_SECRET_KEY_BASE=dummy
RUN --mount=type=cache,target=/root/.npm,sharing=locked \
SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile

# Expose ports
EXPOSE 3000
EXPOSE ${PORT}

# Configure healthcheck
HEALTHCHECK --interval=15s --timeout=2s --start-period=10s --retries=3 \
CMD curl -f http://127.0.0.1:3000/status || exit 1
CMD curl -f http://127.0.0.1:${PORT}/status

# Set command
CMD ["/app/bin/run"]
# Set entrypoint and default command
CMD [ "bin/run" ]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ _invite your friends to your everyday activities :)_

```bash
# Install tools
brew install docker rbenv nodenv yarn watchman overmind
brew install docker rbenv nodenv watchman overmind

# Set up environment
git clone git@github.com:opencal-me/opencal
Expand Down
5 changes: 2 additions & 3 deletions bin/fmt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def scoped(&block) = yield
# == Dependencies
system("bundle", "install", "--quiet") ||
abort("Failed to install Ruby dependencies")
system("bin/yarn", "install", "--silent") ||
abort("Failed to install Node dependencies")
system("npm", "install") || abort("Failed to install Node dependencies")

# == Options
require "optparse"
Expand All @@ -32,7 +31,7 @@ scoped do
args = ["--cache"]
args << (options[:fix] ? "--write" : "--check")
args << "."
system! "bin/yarn", "--silent", "prettier", *args
system! "npx", "prettier", *args
end

puts "\n=> Runnning Rubocop"
Expand Down
2 changes: 1 addition & 1 deletion bin/graphql-codegen
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ if [ ! -z "$1" ]; then
fi
CONFIG="config/graphql/codegen.$TARGET.ts"

bin/yarn --silent graphql-codegen --config "$CONFIG"
npx graphql-codegen --config "$CONFIG"
10 changes: 3 additions & 7 deletions bin/lint
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def scoped(&block) = yield
# == Dependencies
system("bundle", "install", "--quiet") ||
abort("Failed to install Ruby dependencies")
system("bin/yarn", "install", "--silent") ||
abort("Failed to install Node dependencies")
system("npm", "install") || abort("Failed to install Node dependencies")

# == Options
require "optparse"
Expand All @@ -39,15 +38,12 @@ scoped do
args = %w[--ext .js,.jsx,.ts,.tsx,.graphql]
args << "--fix" if options[:fix]
args << "."
system!(
"bin/yarn", "--silent", "eslint", "--report-unused-disable-directives",
*args,
)
system!("npx", "eslint", "--report-unused-disable-directives", *args)
puts "No problems or warnings"
end

puts "\n=> Running Typescript compiler"
system! "bin/yarn", "--silent", "tsc"
system! "npx", "tsc"
puts "No errors found"

puts "\n=> Running Sorbet"
Expand Down
4 changes: 2 additions & 2 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ FileUtils.chdir APP_ROOT do
system("bundle check") || system!("bundle install")

puts "\n=> Installing Node dependencies"
system!("bin/yarn --silent install") &&
system!("npm install") &&
puts("Node dependencies are installed")

puts "\n=> Installing githooks"
system! "bin/yarn --silent husky install"
system! "npx husky install"

# puts "\n=> Copying sample files"
# unless File.exist?("config/database.yml")
Expand Down
19 changes: 0 additions & 19 deletions bin/yarn

This file was deleted.

Loading

0 comments on commit f8d25be

Please sign in to comment.