0
0

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 1 year has passed since last update.

docker-composeのコマンドが毎回めんどくさいのでbashテンプレ化

Last updated at Posted at 2023-02-06

まえがき

dockerのコマンドが毎回めんどくさいのでbashテンプレ化に続きdocker-composeもテンプレ化

注意点

  • 前提として私用開発用(雑)、自己責任で煮るなり焼くなりどうぞ。
  • 一階層掘った場所を想定(ex. $ ./scripts/ctl)
  • # executeのところで雑にコールしてるので脆弱性に繋がる可能性あり

script

scripts/ctl
#!/bin/bash
SCRIPT_DIR=`dirname "${0}"`
SCRIPT_NAME=`basename "${0}"`

# cd to exec dir
cd "${SCRIPT_DIR}/../"

# --------------------------------------------------
# set docker-compose command
CMD_DOCKER_COMPOSE='docker compose'
${CMD_DOCKER_COMPOSE} > /dev/null 2>&1
if [ "${?}" -ne "0" ]; then
  CMD_DOCKER_COMPOSE='docker-compose'
fi


# --------------------------------------------------
# define aliases
SERVICE_ALIASES=$(cat << EOS
b backend
f frontend
EOS
)

_alias() {
  echo "${SERVICE_ALIASES}" | grep -m1 -e "^${1} " | cut -d' ' -f2
}


# --------------------------------------------------
# args
CTL_CMD="${1}"
shift

ALIASED=$(_alias "${1}")
MAYBE_SERVICE="${ALIASED:-${1}}"
shift

REST_ARGS="${@}"


# --------------------------------------------------
# functions
usage() {
  cat << EOS

Usage: ${SCRIPT_DIR}/${SCRIPT_NAME} COMMAND [MAYBE_SERVICE] [REST_ARGS]

Commands:
  build   `type _build | grep '^ '`
  up      `type _up    | grep '^ '`
  run     `type _run   | grep '^ '`
  exec    `type _exec  | grep '^ '`
  stop    `type _stop  | grep '^ '`
  down    `type _down  | grep '^ '`
ServiceAliases:
`echo "${SERVICE_ALIASES}" | sed -e 's/^/  /'`

EOS
  exit 1
}

_build() {
  ${CMD_DOCKER_COMPOSE} build ${MAYBE_SERVICE} ${REST_ARGS}
}

_up() {
  ${CMD_DOCKER_COMPOSE} up ${MAYBE_SERVICE} ${REST_ARGS}
}

_run() {
  ${CMD_DOCKER_COMPOSE} run --rm ${MAYBE_SERVICE} ${REST_ARGS}
}

_exec() {
  ${CMD_DOCKER_COMPOSE} exec -it ${MAYBE_SERVICE} ${REST_ARGS}
}

_stop() {
  ${CMD_DOCKER_COMPOSE} stop ${MAYBE_SERVICE} ${REST_ARGS}
}

_down() {
  ${CMD_DOCKER_COMPOSE} down ${MAYBE_SERVICE} ${REST_ARGS}
}


__main() {
  if [ -z "${CTL_CMD}" ]; then
    usage
  fi

  type "_${CTL_CMD}" > /dev/null 2>&1

  if [ "${?}" -eq "0" ]; then
    # execute
    _${CTL_CMD}
  else
    echo "-${0}: ${SCRIPT_NAME}: ${CTL_CMD} command not found" 1>&2
    usage
  fi
}

__main

ex.usage


$ ./scripts/ctl

Usage: ./scripts/ctl COMMAND [MAYBE_SERVICE] [REST_ARGS]

Commands:
  build       ${CMD_DOCKER_COMPOSE} build ${MAYBE_SERVICE} ${REST_ARGS}
  up          ${CMD_DOCKER_COMPOSE} up ${MAYBE_SERVICE} ${REST_ARGS}
  run         ${CMD_DOCKER_COMPOSE} run --rm ${MAYBE_SERVICE} ${REST_ARGS}
  exec        ${CMD_DOCKER_COMPOSE} exec -it ${MAYBE_SERVICE} ${REST_ARGS}
  stop        ${CMD_DOCKER_COMPOSE} stop ${MAYBE_SERVICE} ${REST_ARGS}
  down        ${CMD_DOCKER_COMPOSE} down ${MAYBE_SERVICE} ${REST_ARGS}
ServiceAliases:
  b backend
  f frontend

$ ./scripts/ctl run frontend echo 'Lorem ipsum'
Lorem ipsum

できること

rspecを例に

example
$ docker-compose run --rm backend_api_rails bundle exec rspec
↓
$ ./scripts/ctl rspec # ex. コマンド追加
or
$ ./scripts/ctl run rails bundle exec rspec # ex. エイリアス利用

ex. コマンド追加

_rspec() {
  ${CMD_DOCKER_COMPOSE} run --rm backend_api_rails bundle exec rspec ${MAYBE_SERVICE} ${REST_ARGS}
}

を定義しコマンド追加

ex. エイリアス利用

SERVICE_ALIASESにスペース区切りで[alias] [service_name]

SERVICE_ALIASES=$(cat << EOS
rails backend_api_rails
EOS
)

あとがき

declare -A使えない子がいて遠回りした・・
これで長いサービス名やコマンドとお別れできる。

突貫なので不都合出てきたら修正するかも。ある程度枯れたらgithubあげるかな。
もっと良い感じにできそうだったら教えて頂けますと幸いです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?