LoginSignup
6
5

More than 3 years have passed since last update.

DockerとRailsを使用して、個人的に使うコマンド

Last updated at Posted at 2020-06-06

6月から本格的にRails案件に入っています。レガシーコードと悪戦苦闘していますが、今回DockerとRailsを使っていく中で、この1ヶ月強で使用したコマンドを紹介しようと思います。少しでも参考になれれば幸いです。

1:特定のcontainer内に入るには?

どのコンテナに入れば良いか分からない問題。docker container lsで確認しましょう

terminal.
docker container ls

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
db84000c93ea        hoge_web            "/docker-entrypoint.…"   4 days ago          Up 12 minutes       0.0.0.0:3000->3000/tcp   hoge_web_1
0f0250cf3891        mysql:5.6           "docker-entrypoint.s…"   4 days ago          Up 12 minutes       0.0.0.0:3306->3306/tcp   hoge_db_1

コンテナが稼働している場合、docker ps。 今回の場合使われているのはhoge_web_1なのでhoge_web_1のcontainerの中に入ることとします。

docker exec -i -t db84000c93ea bash 
root@hoge:/app# //これでcontainerの中に入っています。

オプションについて
docker exec でコンテナの中に
-i, --interactive アタッチしていなくても STDIN を開き続ける
-t, --tty 疑似ターミナル (pseudo-TTY) を割り当て

STDINは、この記事で初めて使い方がわかりました。
https://qiita.com/kyosuke_sumitani/items/7fbeccafd0950019c704

ちなみに、docker-composeでもアプリのcontainerの中に入れます

docker-compose exec コンテナ名 bash

無事に入れたら、次にコンテナ内でよく使うコマンドを紹介します。

rake -T でタスク確認する

terminal.
root@hoge:/app# bundle exec rake -T
rake about                            # List versions of all Rails frameworks and the environment
rake assets:clean[keep]               # Remove old compiled assets
rake assets:clobber                   # Remove compiled assets
rake assets:environment               # Load asset compile environment
rake assets:precompile                # Compile all the assets named in config.assets.precompile
...

rspecを実行する

bundle exec rspec spec/hoge/hoge_spec.rb

// くそ長いspecファイルの場合は行を指定して実行する。下の場合2415行目から実行される。
bundle exec rspec spec/hoge/hoge_spec.rb:2415

rake routesでroutesを確認する

terminal.
root@hoge:/app# bundle exec rails routes
   search_stories  GET  /stories/search(.:format)  stories#search
   ranking_stories GET  /stories/ranking(.:format) stories#ranking
   newlist_stories GET  /stories/newlist(.:format) stories#newlist
...

cronで何が実行されているのか確認する

terminal.
root@hoge:/app# bundle exec whenever —update-crontab //crontabへの反映
root@hoge:/app# crontab -l //ログ出力確認
...

rails cでconsole実行する

terminal.
root@hoge:/app# bundle exec rails c

コンテナでよくいじっているのは、上記のコマンドでした。
次に、コンテナ外でdockerコマンドで触るものを載せていきます。


containerをリスタート

terminal.

docker-compose restart [container_id]

CPU, RAM, IOなどの使用量

terminal.
docker stats
docker stats [container_id]

docker ps, docker ps -a

terminal.
docker ps // 稼働しているcontainerの確認
docker ps -a // 稼働している&&していないcontainerの確認

未使用のイメージを掃除する

terminal.
docker image prune

キャッシュを除いて、新しくDockerfileでイメージを作る

terminal.
docker-compose build --no-cache

docker-sync-stack

terminal.
docker-sync-stack start

これで、docker-compose up と docker-sync start の両方を実行します。

docker-syncの調子が悪い

terminal.
docker-sync stop
docker-sync clean

docker-sync start

コンテナだけを全部削除する

terminal.
docker-compose down

イメージとコンテナ削除

terminal.
docker rm [container_id]
docker rmi [container_image]
// コンテナを削除してからイメージを消すこと

docker全削除

terminal.
docker system prune -a

imageもcontainerも全部削除されるので、使う頻度はほぼ無いですが。

6
5
0

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
5