Skip to content

Instantly share code, notes, and snippets.

@bibendi
Last active December 5, 2023 12:52
Show Gist options
  • Save bibendi/a1c48ed087902a1638982fb597bee8ef to your computer and use it in GitHub Desktop.
Save bibendi/a1c48ed087902a1638982fb597bee8ef to your computer and use it in GitHub Desktop.
Dip on Rails
Pry.config.history.should_save = true
Pry.config.history.file = File.join(__dir__, '.pry_history')
-- Don't display the "helpful" message on startup.
\set QUIET 1
-- psql writes to a temporary file before then moving that temporary file on top of the old history file
-- a bind mount of a file only bind mounts the inode, so a rename like this won't ever work
\set HISTFILE /var/log/psql_history/.psql_history
-- Show how long each query takes to execute
\timing
-- Use best available output format
\x auto
-- Verbose error reports
\set VERBOSITY verbose
-- If a command is run more than once in a row,
-- only store it once in the history
\set HISTCONTROL ignoredups
\set COMP_KEYWORD_CASE upper
-- By default, NULL displays as an empty space. Is it actually an empty
-- string, or is it null? This makes that distinction visible
\pset null '[NULL]'
\unset QUIET

Docker on Dip Development Configuration

We have Docker development.

In addition to it you can also use dip–CLI utility for straightforward provisioning and interacting with an applications configured by docker-compose.

To install dip copy and run the command below:

gem install dip

Usage

# provision application
dip provision

# run web app
dip rails s
# or simple
dip up web

# run rails console
dip rails c

# run webpacker
dip up -d webpacker
# `-d` - mean that service will run in detached (background) mode

# run migrations
dip rake db:migrate

# pass env variables into application
dip VERSION=20100905201547 rake db:migrate:down

# run sidekiq
dip up sidekiq

# run webhooks receiver
ULTRAHOOK_API_KEY=00000 dip compose up webhook
# or simple
ULTRAHOOK_API_KEY=00000 dip webhook
# also you can move ULTRAHOOK_API_KEY variable to ~/.bashrc or ~/.zshrc

# run the whole app
dip up web sidekiq webpacker

# simply launch bash within app directory
dip bash

# Additional commands

# update gems or packages
dip bundle install
dip yarn install

# run psql console
dip psql common
# where `common` is a database name. It might be `common_test`.

# run tests
# TIP: `dip rspec` is already auto prefixed with `RAILS_ENV=test`
dip rspec spec/path/to/single/test.rb:23

# run functional tests
# webpacker should be running with test environment
RAILS_ENV=test dip up webpacker
dip rspec spec/features

# shutdown all containers
dip down

Starting from version 3.5, Dip can be easily integrated into ZSH shell, especially if used agnostic theme. So, there is allow us using all above commands without dip prefix. And it looks like we are not using Docker at all (really that's not true). Also, I recommend to install zsh-autosuggestions. And don't use bundler or rails ZSH plugins 'cause Dip is conflicting.

dip console | source /dev/stdin

rails c
rails s
rspec spec/test_spec.rb:23
psql common
VERSION=20100905201547 rake db:migrate:down
rake routes | grep community

But after we get out to somewhere from the common-admin directory, then all Dip's aliases will be cleared. And if we get back, then Dip's aliases will be restored.

#!/usr/bin/env ruby
require 'pathname'
# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
Dir.chdir APP_ROOT do
# This script is a starting point to setup your application.
# Add necessary setup steps to this file:
puts "== Installing dependencies =="
system "gem install bundler --conservative"
system "bundle check || bundle install"
system "yarn install"
# puts "\n== Copying sample files =="
# unless File.exist?("config/database.yml")
# system "cp config/database.yml.sample config/database.yml"
# end
puts "\n== Preparing database =="
system "bundle exec rake db:create db:structure:load"
system "bundle exec rake db:test:prepare"
system "bundle exec rake db:seed"
# puts "\n== Removing old logs and tempfiles =="
# system "rm -f log/*"
# system "rm -rf tmp/cache"
end
version: '2'
environment:
RAILS_ENV: development
compose:
files:
- docker-compose.yml
interaction:
bash:
service: runner
command: /bin/bash
compose_run_options: [no-deps]
gem:
service: runner
command: gem
compose_run_options: [no-deps]
bundle:
service: runner
command: bundle
compose_run_options: [no-deps]
yarn:
service: runner
command: yarn
compose_run_options: [no-deps]
rake:
service: runner
command: bundle exec rake
rails:
service: runner
command: bundle exec rails
subcommands:
s:
service: web
compose_method: up
rspec:
service: runner
environment:
RAILS_ENV: test
command: bundle exec rspec
psql:
service: postgres
command: psql -h postgres -U postgres
'redis-cli':
service: redis
command: redis-cli -h redis
provision:
- dip compose down --volumes
- dip compose up -d postgres redis
- dip bash -c ./bin/setup
version: '3.4'
services:
app: &app
build:
context: .
dockerfile: ./Dockerfile
args:
PG_VERSION: '9.5'
NODE_VERSION: '8.11.1'
YARN_VERSION: '1.7.0'
image: common-dev:2.0.0
volumes:
- .:/app:cached
tmpfs:
- /tmp
backend: &backend
<<: *app
volumes:
- .:/app:cached
- rails_cache:/app/tmp/cache
- bundle:/bundle
- node_modules:/app/node_modules
- assets:/app/public/assets
- packs:/app/public/packs
- packs_test:/app/public/packs-test
environment:
- RAILS_ENV=${RAILS_ENV:-development}
- REDIS_URL=redis://redis:6379/
- DB_HOST=postgres
- DB_USER=postgres
- DB_PASSWORD=postgres
- BOOTSNAP_CACHE=/bundle/bootsnap
- CAPYBARA_HEADLESS=1
- WEBPACKER_DEV_SERVER_HOST=webpacker
- BOOTSNAP=1
depends_on:
- postgres
- redis
runner:
<<: *backend
stdin_open: true
tty: true
command: /bin/bash
web:
<<: *backend
command: rails server -b 0.0.0.0
ports:
- '3000:3000'
sidekiq:
<<: *backend
command: sidekiq -q mailers -q default -q notifier
postgres:
image: postgres:9.5
volumes:
- .psqlrc:/root/.psqlrc:ro
- postgres:/var/lib/postgresql/data
- postgres_history:/var/log/psql_history
ports:
- 5432
redis:
image: redis:3.2-alpine
volumes:
- redis:/data
ports:
- 6379
mail:
image: drujensen/mailcatcher:latest
ports:
- '1025:1025'
- '1080:1080'
webpacker:
<<: *app
command: ./bin/webpack-dev-server
ports:
- '3035:3035'
volumes:
- .:/app:cached
- bundle:/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
- packs_test:/app/public/packs-test
environment:
- NODE_ENV=development
- RAILS_ENV=${RAILS_ENV:-development}
- WEBPACKER_DEV_SERVER_HOST=0.0.0.0
webhook:
<<: *app
command: bundle exec ultrahook common web:3000
volumes:
- .:/app:cached
- bundle:/bundle
environment:
- ULTRAHOOK_API_KEY=${ULTRAHOOK_API_KEY:-""}
volumes:
postgres:
postgres_history:
redis:
bundle:
node_modules:
assets:
rails_cache:
packs:
packs_test:
FROM ruby:2.5.1
ARG PG_VERSION
ARG NODE_VERSION
ARG YARN_VERSION
ARG TINI_VERSION=v0.18.0
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' $PG_VERSION > /etc/apt/sources.list.d/pgdg.list \
&& curl -o /tmp/nodejs.deb https://deb.nodesource.com/node_8.x/pool/main/n/nodejs/nodejs_$NODE_VERSION-1nodesource1_amd64.deb \
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list \
&& curl 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 \
&& apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
less \
vim \
postgresql-client-$PG_VERSION \
/tmp/nodejs.deb \
yarn=$YARN_VERSION-1 \
google-chrome-unstable \
ghostscript libgs-dev \
graphviz \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
ENV LANG=C.UTF-8 \
GEM_HOME=/bundle \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_APP_CONFIG=$BUNDLE_PATH \
BUNDLE_BIN=$BUNDLE_PATH/bin
ENV PATH $BUNDLE_BIN:$PATH
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini
RUN mkdir -p /app
WORKDIR /app
ENTRYPOINT ["/tini", "--"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment