Skip to content

Instantly share code, notes, and snippets.

@hirowatari
Last active September 18, 2022 05:32
Show Gist options
  • Save hirowatari/832658c3ffdc476db44436d2e0ff7452 to your computer and use it in GitHub Desktop.
Save hirowatari/832658c3ffdc476db44436d2e0ff7452 to your computer and use it in GitHub Desktop.
Rails, docker, selenium
version: '3.7'
services:
rails:
build: docker/rails
ports:
- 3000:3000
- 3035:3035
- 3333:3333
volumes:
- ./prolo-rails:/prolo-rails/
- ./.netrc:/home/rails_user/.netrc
environment:
WEB_CONCURRENCY: 1
SELENIUM_HOST: selenium
SELENIUM_PORT: 4444
env_file:
- prolo-rails/.env
tty: true
stdin_open: true
networks:
default:
aliases:
- app.prolo-test.test
- api.prolo-test.test
- embed.prolo-test.test
depends_on:
- db
- redis
db:
image: mysql
networks: [default]
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: prolo
redis:
image: redis:4.0.9-alpine
ports:
- 6379:6379
selenium:
image: selenium/standalone-chrome-debug
ports:
- 4444:4444
- 5900:5900
volumes:
- /dev/shm:/dev/shm
networks:
default:
# rails/Dockerfile
FROM ubuntu:18.04
ARG GROUP_ID=1000
ARG USER_ID=1000
ARG USER=rails_user
ENV DEBIAN_FRONTEND noninteractive
ENV BUNDLER_VERSION 2.0.2
ENV RUBY_VERSION 2.6.4
RUN apt-get update
RUN apt-get upgrade -qq -y
RUN apt-get install --no-install-recommends -qq -y curl wget # for dependency managment eg rbenv heroku
RUN apt-get install --no-install-recommends -qq -y sudo # for installing things as user
RUN apt-get install --no-install-recommends -qq -y nodejs # for rails
RUN apt-get install --no-install-recommends -qq -y git # for ruby gem git installation and heroku deployments
RUN apt-get install --no-install-recommends -qq -y libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev # for install rbenv
RUN apt-get install --no-install-recommends -qq -y ca-certificates # for wget ssl verification
RUN apt-get install --no-install-recommends -qq -y mysql-client libmysqlclient-dev # for using mysqlgem
RUN apt-get install --no-install-recommends -qq -y tzdata # rails dependency
RUN apt-get install --no-install-recommends -qq -y libicu-dev # for charlock_holmes gem
RUN apt-get -qq -y install lsb-release gnupg # for postgres package package
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get -qq update
RUN apt-get install --no-install-recommends -qq -y postgresql-client-10 libpq-dev
RUN apt-get install --no-install-recommends -qq -y yarn
RUN apt-get install --no-install-recommends -qq -y netcat # for detecting if postgres is ready
RUN apt-get install --no-install-recommends -qq -y vim # for debugging
# hack to set uid/gid to the same as host user
RUN groupadd -g $GROUP_ID $USER && \
adduser --disabled-password --gecos '' --uid=$USER_ID --gid=$GROUP_ID $USER && \
adduser $USER sudo && \
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER $USER
ENV HOME /home/$USER
ENV PATH $HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH
WORKDIR $HOME
# install heroku
RUN curl https://cli-assets.heroku.com/install.sh | sh
# install rbenv
RUN wget -O - https://github.com/sstephenson/rbenv/archive/master.tar.gz \
| tar zxf - \
&& mv rbenv-master $HOME/.rbenv
RUN wget -O - https://github.com/sstephenson/ruby-build/archive/master.tar.gz \
| tar zxf - \
&& mkdir -p $HOME/.rbenv/plugins \
&& mv ruby-build-master $HOME/.rbenv/plugins/ruby-build
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.profile
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bashrc
# install bundler
RUN rbenv install $RUBY_VERSION \
&& rbenv global $RUBY_VERSION
RUN gem install bundler --version $BUNDLER_VERSION && \
gem install invoker && \
yes | sudo env PATH=$PATH invoker setup
# setup entrypoint
COPY entrypoint.sh /
RUN sudo chown $GROUP_ID:$USER_ID /entrypoint.sh
RUN sudo chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
# Disable skylight dev env warnings
RUN echo 'disable_dev_warning: true' > $HOME/.skylight
VOLUME /prolo-rails
WORKDIR /prolo-rails
EXPOSE 3000
CMD ["invoker", "start", "Procfile.dev"]
# rails/entrypoint.sh
#!/usr/bin/env bash
set -e
bundle check || bundle install --path=vendor/bundle --jobs 8 &
yarn install --check-files &
wait
MAX_RETRIES=30
for ((i=1; i <= $MAX_RETRIES; i++)); do
nc -w 1 -z db 3306 && break || sleep 1;
echo "$(date) - waiting for mysql...";
if [[ $i -eq $MAX_RETRIES ]]; then
echo 'FAILED TO CONNECT TO MYSQL'
exit 1
fi
done
# sometimes rails will refuse to start because this pid wasn't removed
# likely because it was forcibly killed
if [ -f tmp/pids/server.pid ]; then
rm tmp/pids/server.pid >> /dev/null
fi
mv tmp/cache /tmp/
bundle exec rake db:drop db:create db:structure:load db:seed
RAILS_ENV=test bundle exec rake parallel:drop parallel:create parallel:load_structure parallel:seed
exec "$@"
#spec/support/system.rb (include this file in your rails_helper)
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
Capybara.server_host = '0.0.0.0'
url = "http://#{ENV['SELENIUM_HOST']}:#{ENV['SELENIUM_PORT']}/wd/hub"
driver = File.file?(Rails.root.join('tmp', 'no_headless')) ? :chrome : :headless_chrome
driven_by :selenium, using: driver, screen_size: [1400, 1400], options: { url: url }
include Devise::Test::IntegrationHelpers
host! Rails.configuration.domains.fetch(:rails_base_url)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment