今回はdocker-composeでの作業を効率化できる機能「dip」を試してみたので、その導入手順や使い方について解説したいと思います。これまで「docker-compose run app…」とわざわざ入力していた部分が省略できる点は非常に魅力的な機能になります。
dipとは
dipとは「Docker Interaction Program」の略で、docker-composeでの作業を効率的に行うことができる機能です。dipはRubyで書かれているため、Rubyの実行環境が必要になります。
Development-environment CLI program providing the native-like interaction with a Dockerized application. It creates the feeling that you are working without mind-blowing commands to run the containers.
Docker化されたアプリケーションとのネイティブライクなinteraction(対話)を提供する開発環境のCLIプログラムです。コンテナを実行するための煩わしいコマンドなしで作業している感覚を提供します。
参考: Github/dip
dipを導入するメリット
コンテナにログインせずに作業できる
これまでbundle install
やrails console
といった決まりきったコマンドを実行する際でも、わざわざシェルにアクセスしなければなりませんでした。
しかし、dipを導入しdip bundle install
やdip rails console
を入力することで、シェルにアクセスすることなく作業が実現できてしまいます。
コンテナの起動状態を気にしなくてよい
docker-composeでコンテナに入る場合、コンテナが起動していなければdocker compose run app bash,,,
、コンテナが起動していればdocker compose exec app bash,,,
とするなど、面倒な使い分けが必要です。
しかしdipを使用することで、dip sh
やdip rails s
はコンテナが起動していなくても使用することができ、dipを終了すれば自動的にコンテナも終了してくれます。
Dockerfileやdocker-compose.ymlの変更が不要
dip.yml
はDockerfile
やdocker-compose.yml
と別々に設定できるため、管理が非常に簡単になります。また、dip.yml
をgitignore
に設定することで自分流にカスタマイズすることもできるようになります。
dipを実装してみる
Dockerとdocker-composeの導入
以下の記事を参考にRailsアプリケーションにDockerとdocker-composeを導入します。

dipのインストール
gemコマンドを用いてdipをインストールします。
% gem install dip
dip.ymlの作成と編集
以下のコマンドでdip.yml
を作成します。
% touch dip.yml
作成したdip.yml
に設定を追加します。
bash
、bundle
、rails
、rubocop
、clean_cache
を指定したdip.yml
は以下のようになります。他にも追加したいコマンドがある場合はinteraction配下に適宜追記してください。
version: '7.0'
environment:
COMPOSE_EXT: development
compose:
files:
- docker/docker-compose.yml
project_name: dip_demo
interaction:
bash:
description: Open the Bash shell in app's container
service: web
command: bash
compose:
run_options: [no-deps]
bundle:
description: Run Bundler commands
service: web
command: bundle
rails:
description: Run Rails commands
service: web
command: bundle exec rails
subcommands:
s:
description: Run Rails server at http://localhost:3000
service: web
compose:
run_options: [service-ports, use-aliases]
rubocop:
description: Run RuboCop commands
service: web
command: bundle exec rubocop
clean_cache:
description: Delete cache files on the host machine
command: rm -rf $(pwd)/tmp/cache/*
provision:
- dip compose down --volumes
- dip clean_cache
- dip compose up -d
- dip bash -c ./bin/setup
READMEには以下の参考例が記載されていますが、これはPostgreSQLを前提にコードがが書かれているため、MySQLを使用したアプリケーションでは動かないので注意してください。
# Required minimum dip version
version: '7.1'
environment:
COMPOSE_EXT: development
compose:
files:
- docker/docker-compose.yml
- docker/docker-compose.$COMPOSE_EXT.yml
- docker/docker-compose.$DIP_OS.yml
project_name: bear
interaction:
shell:
description: Open the Bash shell in app's container
service: app
command: bash
compose:
run_options: [no-deps]
bundle:
description: Run Bundler commands
service: app
command: bundle
rake:
description: Run Rake commands
service: app
command: bundle exec rake
rspec:
description: Run Rspec commands
service: app
environment:
RAILS_ENV: test
command: bundle exec rspec
rails:
description: Run Rails commands
service: app
command: bundle exec rails
subcommands:
s:
description: Run Rails server at http://localhost:3000
service: web
compose:
run_options: [service-ports, use-aliases]
sidekiq:
description: Run sidekiq in background
service: worker
compose:
method: up
run_options: [detach]
psql:
description: Run Postgres psql console
service: app
default_args: db_dev
command: psql -h pg -U postgres
setup_key:
description: Copy key
service: app
command: cp `pwd`/config/key.pem /root/keys/
shell: false # you can disable shell interpolations on the host machine and send the command as is
clean_cache:
description: Delete cache files on the host machine
command: rm -rf $(pwd)/tmp/cache/*
provision:
- dip compose down --volumes
- dip clean_cache
- dip compose up -d pg redis
- dip bash -c ./bin/setup
dipコマンドの実行
ここからはdipコマンドを実際に実行してみます。
まずはprovisionコマンドを実行しセットアップを行いましょう。
% dip provision
[+] Running 2/2
⠿ Container dip_demo-db-1 Removed 4.1s
⠿ Network dip_demo_default Removed 0.1s
[+] Running 3/3
⠿ Network dip_demo_default Created 0.1s
⠿ Container dip_demo-db-1 Started 1.6s
⠿ Container dip_demo-web-1 Started 3.4s
== Installing dependencies ==
The Gemfile's dependencies are satisfied
== Preparing database ==
== Removing old logs and tempfiles ==
== Restarting application server ==
続いて、適当なgemをGemfile
に追加しbundle install
を実行してみます。
今回はデバッグ用のgemであるpry-rails
を追加しました。
group :development do
gem 'pry-rails'
end
% dip bundle
・
・
・
Bundle complete! 15 Gemfile dependencies, 72 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
次にサーバーを起動してみます。
サーバー起動後、http://localhost:3000/にアクセスするとRailsの初期画面が表示されるはずです。
% dip rails s
=> Booting Puma
=> Rails 7.0.4 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version")
* Min threads: 5
* Max threads: 5
* Environment: development
* PID: 1
* Listening on http://0.0.0.0:3000
Use Ctrl-C to stop

まとめ
- dipとは「Docker Interaction Program」の略で、docker-composeでの作業を効率的に行うことができる機能
- dipはRubyで書かれているため、Rubyの実行環境が必要
- dipを導入するメリットは、「コンテナにログインせずに作業できること」「コンテナの起動状態を気にしなくてよいこと」「Dockerfileやdocker-compose.ymlの変更が不要であること」が挙げられる
- 追加したいコマンドについてはdip.ymlのinteraction配下に追記する
参考
- Github/dip
- docker-composeを便利にするツール「dip」を使ってみた
- クジラに乗ったRuby: Evil Martians流Docker+Ruby/Rails開発環境構築(更新翻訳)
今回はdocker-composeでの作業を効率化できる機能「dip」について解説しました。Docker環境下でRailsアプリケーションを運用している場合は、dipの導入を検討してみてもよいかもしれません。