14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker+pecoでコンテナ(イメージ)操作

Last updated at Posted at 2014-07-24
#alias docker='sudo docker'

# pecoで選択したコンテナに対して操作を行う
docker_peco_containers() {

  if [ $# -lt 1 ]; then
    echo "Usage: dpc [OPTIONS] COMMAND [args]" >&2
    return 1
  fi

  docker ps -a | peco | while read CONTAINER
  do
    docker $@ `echo $CONTAINER | awk '{print $1}'`
  done

}

# pecoで選択したイメージに対して操作を行う
docker_peco_images() {

  if [ $# -lt 1 ]; then
    echo "Usage: dpi [OPTIONS] COMMAND [args]" >&2
    return 1
  fi

  unset DOCKER_OPTS

  [[ -z $ENVS ]] || \
    for e in $ENVS; do DOCKER_OPTS="$DOCKER_OPTS -e $e"; done

  [[ -z $VOLUMES ]] || \
    for v in $VOLUMES; do DOCKER_OPTS="$DOCKER_OPTS -v $v"; done

  [[ -z $PORTS ]] || \
    for p in $PORTS; do DOCKER_OPTS="$DOCKER_OPTS -p $p"; done

  docker images | peco | while read IMAGE
  do
    docker $DOCKER_OPTS $@ `echo $IMAGE | awk '{print $3}'`
  done

}

# ついでに一括削除系(peco関係なし)
docker_container_all_clear() {
  echo "Dockerコンテナを削除中..."
  docker kill `docker ps -q`
  docker rm `docker ps -aq`
  echo "すべてのコンテナを削除しました。"
}

# エイリアスはお好みで
alias dpc='docker_peco_containers'
alias dpi='docker_peco_images'
alias dcac='docker_container_all_clear'

これを.bashrcなり/etc/profile.d/docker-peco.shなりに書いておいて以下のように使う。

コンテナの停止、削除

dpc kill; dpc rm

コンテナの起動 (例: Rails+MySQL)

ENVS="RAILS_ENV=production" PORTS="3000:80 3306:3306" VOLUMES="/data:/var/lib/mysql" dpi -d run

イメージの削除

dpi rmi

Dockerの操作がちょっとだけ捗る。

14
14
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
14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?