LoginSignup
15
13

More than 5 years have passed since last update.

Dockerのtips的な自分のメモ

Last updated at Posted at 2017-07-08

Dockerで使える小技集

Dockerを使っていて、「あーこれいいね」と思ったものを集めた
自分のメモを公開しました。

皆様のお役に立てたらいいねしていただけると嬉しいです。

よく使うオプション一覧

  • -a 全ての
  • -q IDのみの取得
  • --filter フィルターの利用
  • -f 強制削除(--force)
  • -v 関連付けボリューム(--volumes)

Dockerの初期化

DockerにはFactory Reset(初期化)なるものがあり
これをすることで全てをまっさらにしてくれます。
全てを抹消しDockerそのものを再インストールします。

スクリーンショット 2017-07-08 22.40.20.png

手順

Dockerアイコンをクリック > Preferences > Reset > Reset to factory defaults

タグの付いてないimageを削除する

タグのついていないDockerのimageの取得方法は以下の通り

$ docker images --filter "dangling=true"

合わせて削除をするのであれば

$ docker rmi -f $(docker images --filter "dangling=true" -q)

ちょっと長ったらしいですが…

$ docker rm -f $(docker ps -aq) & docker rmi -f $(docker images -qf "dangling=true") & docker system prune

なんかしてみるといいかもですね。
.bashrcなんかにaliasとして追加しておくのもありかもしれません

ちなみにprocessでも下記の通りでできます

$ docker rm -f $(docker ps -aq)

あれこれ一括で削除をする

  • 停止中のコンテナ全て
  • 紐ついていないVOLUME全て
  • 紐ついていないネットワーク全て
  • 紐ついていないイメージ全て

を一括で削除できるコマンドが

$ docker system prune
WARNING! This will remove:
    - all stopped containers
    - all volumes not used by at least one container
    - all networks not used by at least one container
    - all dangling images
Are you sure you want to continue? [y/N]

これら消しちゃうけど本当に大丈夫?続けてもいい?
的な感じのメッセージが来るのでyでokです

GUIでもいける!!!!

Dockerアイコンをクリック > Preferences > Reset > Remove all data

スクリーンショット 2017-07-08 22.40.20.png

Railsコンテナを立ち上げてコンテナに入らずにコマンドを打つ!

$ docker-compose run {service} rake routes

とか

$ docker-compose run --rm app rake db:migrate

とか

$ docker-compose run --rm app rails generate controller user

ちなみにrun系のオプション一覧はこちらです
http://docs.docker.jp/compose/reference/run.html

コンテナ内のファイルをコピーする

$ docker cp {コンテナID} or {プロセス名} :{パス} ローカルの保存したい場所
$ docker cp b5e5bab3c242:/etc/httpd/conf/httpd.conf $HOME/Downloads/

もちろんコンテナ内へローカルからコピーも可能です

httpd.confの上書き

$ docker cp $HOME/Downloads/httpd.conf b5e5bab3c242:/etc/httpd/conf/httpd.conf
15
13
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
15
13