dockerでよく使うコマンドメモ

  1. イメージからコンテナを作成し、起動
    • シンプル
      docker run -it (image name) (start program path)
      ex: docker run -it debian /bin/bash
    • データボリュームをマウント
      docker run -it -v (datavolume name):(mount point) (image name) (start program path)
      ex: docker run -it -v vol01:/mnt deb_pgsql /bin/bash
    • 特定のユーザーで実行
      docker run -it -u (user name) (image name) (start program path)
      ex: docker run -it -u postgres deb_pgsql /bin/bash
      ※-uのユーザー名はコンテナ側に設定されていれば基本はOK
    • ポートをマッピング
      -p (host port):(container port)
      ex: -p 80:80
  2. コンテナからイメージを作成
    docker commit (container-id) (image-name)
  3. イメージのタグを変更
    docker tag (current-image):(current-tag) (new-image):(new-tag)
    ex: docker tag 
  4. コンテナスタート
    docker start -ai (container-id)
  5. デタッチとアタッチ
    • デタッチ(コンテナを起動したまま抜ける)
      ctrl+P -> ctrl-Q
    • アタッチ(起動したコンテナに接続)
      docker attach (container-id)
  6. 起動中のコンテナに接続
    docker exec -it (container-id) /bin/bash

  7. コンテナ状態確認
    • 実行中のみ
      docker ps
    • 停止中のものを含む
      docker ps -a
  8. データボリューム作成
    docker volume create --name=datavolumename
  9. その他
    • ユーザーの環境変数設定などのログイン時処理は.bashrcではなく.bash_profile