LoginSignup
6
3

More than 3 years have passed since last update.

CircleCI x Docker Compose の volume mounts でハマったメモ

Last updated at Posted at 2019-04-30

概要

Ruby on Rails の勉強のために sandbox-rails を作って色々実験中。

まずは Quickstart: Compose and Rails を参考に基本となるプロジェクトを作成。
次に自動テスト環境を構築したいので、一先ず rubocop を入れて CircleCI 上で動くようにしたい。
そこで Installing and Using docker-compose を参考にして CircleCI 用の設定を書いていく。

ここで問題が。手元で動く rubocop が CircleCI 上で動いてない。

Using Docker Compose with Docker Executor に書いてあるけど、setup_remote_docker を使っている場合 ボリュームマウントは機能しない とのこと。
これに気付いて docker-compose.yml からボリュームマウントの設定を削除したら、rubocopが動いた。

症状

設定ファイル:

実行結果:

  • Could not locate Gemfile or .bundle/ directory rubocop_failed.png

解析

実行結果から分かる通り、必要なファイルが必要な場所にない。
では何故ないのか? CircleCI 上で実行したJOBの中身を覗かないと分からない。

Rerun job with SSH で実行したJOBにSSHできる
rerun.png

JOBの終了を待って、SSHでログインして以下のコマンドを実行:

circleci@acd63e5b1200:~$ cd project/
circleci@acd63e5b1200:~/project$ docker-compose run web bash
Starting project_db_1 ... done
root@7d2d5dd7111b:/myapp# ls -al
total 12
drwxr-xr-x  3 root root 4096 Apr 30 03:00 .
drwxr-xr-x 70 root root 4096 Apr 30 03:03 ..
drwxr-xr-x  3 root root 4096 Apr 30 03:00 tmp

なぜか tmp/ だけ存在してる。
(Docker とか CircleCI とか全然詳しくないので頭の中が真っ白に。。。)

わかっているのは以下:

  • JOBのWorking Directoryにはデータが存在する
    • project/ 配下にはsandbox-railsの内容が配置されている
  • JOBのWorking Directoryから起動した docker-compose のWorking Directoryには配置されていない
    • そのため Could not locate Gemfile or .bundle/ directory って言われている

悶々としながら似たようなケースがないか検索するも引っかからず。。。
一旦冷静になる。
基本に立ち返ろうと読んだ Using Docker Compose with Docker Executor に、 setup_remote_docker を使っている場合 ボリュームマウントは機能しない と書かれているのに気づく(ドキュメントちゃんと読もう)。
そこから、 docker-compose.yml でボリュームマウントの設定していることに気付く。

解消方法

原因は以下:

  • CircleCI のJOBで setup_remote_docker を使っている場合 ボリュームマウントは機能しない
  • docker-compose.yml でボリュームマウントの設定している

なので、以下の設定を削除した状態でJOBを実行

docker-compose.yml
    volumes:
      - .:/myapp

その結果、 rubocop が動き出した。

設定変更前に tmp/ しか配置されていなかったdocker-compose のWorking Directoryにも、ちゃんとsandbox-railsの内容が配置されている。

circleci@96d8114ad6f3:~/project$ cd
circleci@96d8114ad6f3:~$ cd project/
circleci@96d8114ad6f3:~/project$ docker-compose run web bash
Starting project_db_1 ... done
root@9418efa94560:/myapp# ls -al
total 108
drwxr-xr-x 15 root root 4096 Apr 30 05:29 .
drwxr-xr-x 70 root root 4096 Apr 30 05:31 ..
drwxr-xr-x  2 root root 4096 Apr 30 05:26 .circleci
drwxr-xr-x  8 root root 4096 Apr 30 05:26 .git
-rw-r--r--  1 root root  647 Apr 30 05:26 .gitignore
~(省略)~

まとめ

そもそも Using Docker Compose with Docker Executor でボリュームマウント機能しない件や、 Quickstart: Compose and Rails でボリュームマウントの設定がされていることを認識していなかった(設定内容ちゃんと把握しよう)。
まぁ勉強とか実験のためにやってるので、そこはご愛嬌ってことで(´ω`)
逆に色々把握できてよかった(๑•̀ㅂ•́)و✧

6
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3