Skip to content

Instantly share code, notes, and snippets.

@palkan
Last active August 3, 2023 13:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palkan/f745f4da3e75d4bcf5bbb3bc3f6a4661 to your computer and use it in GitHub Desktop.
Save palkan/f745f4da3e75d4bcf5bbb3bc3f6a4661 to your computer and use it in GitHub Desktop.
Rails-Docker-Box
alias be="bundle exec"
# We might need an editor to work with credentials
vim
# Build tools
autoconf
libtool
libncurses5-dev
libxml2-dev
# ActiveRecord deps
libsqlite3-dev
default-libmysqlclient-dev
# SQLite deps
pkg-config
x-app: &app
build:
context: .
args:
RUBY_VERSION: '3.0.2'
PG_MAJOR: '14'
image: rails-dev:7.1.0
tmpfs:
- /tmp
services:
runner:
<<: *app
stdin_open: true
tty: true
volumes:
- ..:/app:cached
- bundle:/bundle
- history:/usr/local/hist
- ./.psqlrc:/root/.psqlrc:ro
- ./.bashrc:/root/.bashrc:ro
environment:
REDIS_URL: redis://redis:6379/
DATABASE_URL: postgres://postgres:postgres@postgres/
HISTFILE: /usr/local/hist/.bash_history
XDG_DATA_HOME: /app/tmp/caches
EDITOR: vi
ARCONN: ${ARCONN:-postgresql}
working_dir: ${WORK_DIR:-/app}
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
postgres:
image: postgres:14
volumes:
- .psqlrc:/root/.psqlrc:ro
- postgres:/var/lib/postgresql/data
- history:/user/local/hist
environment:
PSQL_HISTFILE: /user/local/hist/.psql_history
POSTGRES_PASSWORD: postgres
# For createdb
PGPASSWORD: postgres
ports:
- 5432
healthcheck:
test: pg_isready -U postgres -h 127.0.0.1
interval: 5s
redis:
image: redis:6.2-alpine
volumes:
- redis:/data
ports:
- 6379
healthcheck:
test: redis-cli ping
interval: 1s
timeout: 3s
retries: 30
volumes:
history:
postgres:
redis:
bundle:
default_connection: <%= defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3' %>
connections:
jdbcderby:
arunit: activerecord_unittest
arunit2: activerecord_unittest2
jdbch2:
arunit: activerecord_unittest
arunit2: activerecord_unittest2
jdbchsqldb:
arunit: activerecord_unittest
arunit2: activerecord_unittest2
jdbcmysql:
arunit:
username: rails
encoding: utf8
arunit2:
username: rails
encoding: utf8
jdbcpostgresql:
arunit:
username: <%= ENV['user'] || 'rails' %>
arunit2:
username: <%= ENV['user'] || 'rails' %>
jdbcsqlite3:
arunit:
database: <%= FIXTURES_ROOT %>/fixture_database.sqlite3
timeout: 5000
arunit2:
database: <%= FIXTURES_ROOT %>/fixture_database_2.sqlite3
timeout: 5000
mysql2:
arunit:
username: rails
encoding: utf8mb4
collation: utf8mb4_unicode_ci
<% if ENV['MYSQL_PREPARED_STATEMENTS'] %>
prepared_statements: true
<% else %>
prepared_statements: false
<% end %>
<% if ENV['MYSQL_HOST'] %>
host: <%= ENV['MYSQL_HOST'] %>
<% end %>
<% if ENV['MYSQL_SOCK'] %>
socket: "<%= ENV['MYSQL_SOCK'] %>"
<% end %>
arunit2:
username: rails
encoding: utf8mb4
collation: utf8mb4_general_ci
<% if ENV['MYSQL_PREPARED_STATEMENTS'] %>
prepared_statements: true
<% else %>
prepared_statements: false
<% end %>
<% if ENV['MYSQL_HOST'] %>
host: <%= ENV['MYSQL_HOST'] %>
<% end %>
<% if ENV['MYSQL_SOCK'] %>
socket: "<%= ENV['MYSQL_SOCK'] %>"
<% end %>
oracle:
arunit:
adapter: oracle_enhanced
database: <%= ENV['ARUNIT_DB_NAME'] || 'orcl' %>
username: <%= ENV['ARUNIT_USER_NAME'] || 'arunit' %>
password: <%= ENV['ARUNIT_PASSWORD'] || 'arunit' %>
emulate_oracle_adapter: true
arunit2:
adapter: oracle_enhanced
database: <%= ENV['ARUNIT_DB_NAME'] || 'orcl' %>
username: <%= ENV['ARUNIT2_USER_NAME'] || 'arunit2' %>
password: <%= ENV['ARUNIT2_PASSWORD'] || 'arunit2' %>
emulate_oracle_adapter: true
postgresql:
arunit:
min_messages: warning
<% if ENV['POSTGRES_DATABASE_URL'] %>
url: <%= ENV['POSTGRES_DATABASE_URL'] %>
<% end %>
arunit_without_prepared_statements:
min_messages: warning
prepared_statements: false
<% if ENV['POSTGRES_DATABASE_URL'] %>
url: <%= ENV['POSTGRES_DATABASE_URL'] %>
<% end %>
arunit2:
min_messages: warning
<% if ENV['POSTGRES_DATABASE_URL'] %>
url: <%= ENV['POSTGRES_DATABASE_URL'] %>
<% end %>
sqlite3:
arunit:
database: <%= FIXTURES_ROOT %>/fixture_database.sqlite3
timeout: 5000
arunit2:
database: <%= FIXTURES_ROOT %>/fixture_database_2.sqlite3
timeout: 5000
sqlite3_mem:
arunit:
adapter: sqlite3
database: ':memory:'
arunit2:
adapter: sqlite3
database: ':memory:'
ARG RUBY_VERSION
ARG DISTRO_NAME=bullseye
FROM ruby:$RUBY_VERSION-slim-$DISTRO_NAME
ARG DISTRO_NAME
# Common dependencies
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
curl \
less \
git \
&& apt-get clean \
&& rm -rf /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
ARG PG_MAJOR
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 $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
libpq-dev \
postgresql-client-$PG_MAJOR && \
apt-get clean && \
rm -rf /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log
# Application dependencies
# We use an external Aptfile for this, stay tuned
COPY Aptfile /tmp/Aptfile
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/Aptfile | xargs) && \
apt-get clean && \
rm -rf /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log
ENV LANG C.UTF-8
ENV GEM_HOME /bundle
ENV BUNDLE_PATH=$GEM_HOME \
BUNDLE_APP_CONFIG=$BUNDLE_PATH \
BUNDLE_BIN=$BUNDLE_PATH/bin \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
ENV PATH /app/bin:$BUNDLE_BIN:$PATH
ARG BUNDLER_VERSION
RUN gem update --system && \
gem install bundler
RUN mkdir -p /app
WORKDIR /app
CMD ["/usr/bin/bash"]
version: '7.1'
environment:
WORK_DIR: /app/${DIP_WORK_DIR_REL_PATH}
compose:
files:
- .dockerdev/compose.yml
project_name: rails_dev
interaction:
# This command spins up a Rails container with the requried dependencies (such as databases),
# and opens a terminal within it.
runner:
description: Open a Bash shell within a Rails container (with dependencies up)
service: runner
command: /bin/bash
# Run a Rails container without any dependent services (useful for non-Rails scripts)
bash:
description: Run an arbitrary script within a container (or open a shell without deps)
service: runner
command: /bin/bash
compose_run_options: [ no-deps ]
# A shortcut to run Bundler commands
bundle:
description: Run Bundler commands
service: runner
command: bundle
compose_run_options: [ no-deps ]
rake:
description: Run Rake commands
service: runner
command: bundle exec rake
ruby:
description: Run Ruby with Bundler activated
service: runner
command: bundle exec ruby
rubocop:
description: Run RuboCop
service: runner
command: bundle exec rubocop
compose_run_options: [ no-deps ]
test:
description: Run a single test file (an alias for ruby -Ilib:test)
service: runner
command: bundle exec ruby -Ilib:test
psql:
description: Run Postgres psql console
service: postgres
default_args: anycasts_dev
command: psql -h postgres -U postgres
createdb:
description: Create a PostgreSQL database
service: postgres
command: createdb -h postgres -U postgres
'redis-cli':
description: Run Redis console
service: redis
command: redis-cli -h redis
provision:
- dip compose down --volumes
- dip compose up -d postgres redis
- dip bundle install
- (test -f activerecord/test/config.yml) || (cp .dockerdev/config.yml activerecord/test/config.yml)
- dip createdb activerecord_unittest
- dip createdb activerecord_unittest2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment