4
3

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 3 years have passed since last update.

Docker。自分がよく使うコマンドをaliasに設定「説明付き」

Last updated at Posted at 2020-07-09

はじめに

私が思うよく使うDockerコマンドは、bash_profileや、bash_rcでaliasに設定して使っています。

自分の誹謗録ようなものですが、興味のある方はお試しください。
そして、もし有用なaliasを使っているならぜひ共有頂けると嬉しいです。

自分も勉強目的で書いているので、間違った情報があるかもしれません。
そこはご指摘頂けるとありがたいです。

alias設定内容

~/.bash_profile

以下の構文を追加

if [ -f ~/.myalias ]; then
  source ~/.myalias
fi

~/.myalias

● gist
https://gist.github.com/genie-oh/d73a224e7cb3cffab2868182eb79ccad

# show full command & execute 
alias al='_(){ CMD=$(alias | grep "alias $1=" | cut -d = -f 2- | sed "s:^.\(.*\).$:\1:"); ARG=$(echo $@ | sed "s/^$1//"); CMD="${CMD}${ARG}"; echo "execute : ${CMD}"; echo " "; bash -c "${CMD}"; };_'

# docker
alias al-dock='cat ~/.myalias | grep dock | sed "s/=/      \t\= /"'
alias dock='docker'
alias docki='docker images'
alias dockps='docker ps -a'
alias dockrrm='docker run --rm'
alias dockeit='docker exec -it'
alias dockrm='docker rm -f'
alias dockrmi='docker rmi -f'
alias dockrma='docker rm -f $(docker ps -aq)'
alias dockrmia='docker rmi -f $(docker images -aq)'
alias dockins='docker inspect'
alias dockip='docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}"'
alias docklogs='dock logs --tail 50 --follow --timestamps'
alias dockc='docker-compose'
alias dockcb='docker-compose build'
alias dockcu='docker-compose up -d'
alias dockcd='docker-compose down'
alias dockccl='grep container_name docker-compose.yml'

※説明

al-dock

Docker関連aliasのリストを表示

$ al al-dock
execute : cat ~/.myalias | grep dock | sed "s/=/      \t\= /"
 
# docker
alias al-dock           = 'cat ~/.myalias | grep dock | sed "s/=/      \t\= /"'
alias dock              = 'docker'
alias docki             = 'docker images'
alias dockps            = 'docker ps -a'
alias dockrrm           = 'docker run --rm'
alias dockeit           = 'docker exec -it'
alias dockrm            = 'docker rm -f'
alias dockrmi           = 'docker rmi -f'
alias dockrma           = 'docker rm -f $(docker ps -aq)'
alias dockrmia          = 'docker rmi -f $(docker images -aq)'
alias dockins           = 'docker inspect'
alias dockip            = 'docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}"'
alias docklogs          = 'dock logs --tail 50 --follow --timestamps'
alias dockc             = 'docker-compose'
alias dockcb            = 'docker-compose build'
alias dockcu            = 'docker-compose up -d'
alias dockcd            = 'docker-compose down'
alias dockccl           = 'grep container_name docker-compose.yml'

dockc, dockcb, dockcu, dockcd

docker-compose関連
docker-composeのbuild,up,down

alias dockc             = 'docker-compose'
alias dockcb            = 'docker-compose build'
alias dockcd            = 'docker-compose down'
alias dockcu            = 'docker-compose up -d'

dockccl

docker-compose.yml内のコンテナーネームのリストを出力

alias dockccl           = 'grep container_name docker-compose.yml'
$ al dockccl
execute : grep container_name docker-compose.yml

    container_name: lamp-web
    container_name: lamp-php
...省略

docki

docker imageのリストを出力

alias docki             = 'docker images'
$ al docki
execute : docker images

REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
centos                   7                   b5b4d78bc90c        8 weeks ago         203MB
docker-lamp-test_php     latest              e6d67d8f48cf        17 minutes ago      666MB
...省略

dockps

dockerのコンテナーリストと実行状態を表示

alias dockps            = 'docker ps -a'
$ al dockps
execute : docker ps -a

CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                          PORTS                     NAMES
6116324251dd        docker-lamp-test_php     "docker-php-entrypoi…"   17 minutes ago      Up 17 minutes                   0.0.0.0:32769->9000/tcp   lamp-php
...省略

dockeit

実行中のコンテナーで、interactive&ttyモードでコマンド実行
docker exec --interective --tty

alias dockeit           = 'docker exec -it'
$ al dockeit lamp-php bash
execute : docker exec -it lamp-php bash

root@37bcdbd02f92:/var/www/html# ls
config  gulpfile.js  home_root  home_sub  nodeapp

dockrrm

コンテナーでコマンドを実行後、即時にコンテナーを終了する
コンテナー作成▶コマンド実行▶コンテナー終了

alias dockrrm           = 'docker run --rm'
$ al dockrrm composer php -v
execute : docker run --rm composer php -v

PHP 7.4.7 (cli) (built: Jun 11 2020 18:58:32) ( NTS )
...省略

dockins

コンテナーの状態を確認

alias dockins           = 'docker inspect'
$ al dockins lamp-php
execute : docker inspect lamp-php

[
    {
        "Id": "6116324251ddffc8090cc605d391f89a951aeb46d32636dd62476a225a894c51",
        "Created": "2020-07-01T13:30:27.11145Z",
...省略

dockip

コンテナーの状態で、IPだけを取得

alias dockip            = 'docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}"'
$ al dockip lamp-php
execute : docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" lamp-php

172.30.0.5

docklogs

コンテナーのログを確認

alias docklogs          = 'dock logs --tail 50 --follow --timestamps'
$ al docklogs lamp-php
execute : docker logs --tail 50 --follow --timestamps lamp-php

2020-07-01T13:30:28.338589500Z [01-Jul-2020 22:30:28] NOTICE: fpm is running, pid 1
2020-07-01T13:30:28.340324600Z [01-Jul-2020 22:30:28] NOTICE: ready to handle connections

dockrm, dockrmi

特定のコンテナー、またはイメージを削除

alias dockrm            = 'docker rm -f'
alias dockrmi           = 'docker rmi -f'

dockrma, dockrmia

すべてのコンテナー、またはイメージを削除

alias dockrma           = 'docker rm -f $(docker ps -aq)'
alias dockrmia          = 'docker rmi -f $(docker images -aq)'
4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?