tmuxの環境構築にはtmuxinatorが便利ですが、tmuxinator自体(もしくはRuby)をインストールしたくない場合があります。
その場合、tmuxinatorのdebugオプションを使うとtmuxコマンドが書かれたbashスクリプトが生成されるので、Dockerイメージを使ってそれを利用します。
- 環境
- Windows 10
- docker-toolkit 17.03
- Git for Windows (コマンドは全てこれ上で実行しています)
- tmux 2.3
設定ファイル
docker-compose設定
Dockerイメージ benizar/tmuxinator は公式ではありませんが使わせていただきます。
volumeは/rootにマウントする必要があります。
ホスト側は$HOMEを指定していますが別ディレクトリでもOKです。
docker-compose.yml
version: '3'
services:
tmuxinator:
image: benizar/tmuxinator
volumes:
- $HOME:/root
サンプルtmuxinatorプロジェクトファイル
ホスト側のマウントするディレクトリに.tmuxinator
ディレクトリを作成し、その中にYAMLファイルを作成します。
文法は公式を参照してください。
~/.tmuxinator/my-project.yml
name: my-project
root: /c/Users/user
windows:
- main:
layout: tiled
panes:
- vagrant status
- git status
- sub:
panes:
- docker ps
設定ファイルは以上です。
余談ですが、Windows(MSYS2)の場合、docker-composeを使わずdocker run
コマンドに --volume(-v) オプションをつけるとうまくいきません。MSYS2_ARG_CONV_EXCL環境変数を使ったり、他の回避方法もありますがdocker-composeが楽だと思います。
Macならdocker run -v ...
でいけると思います。
スクリプト生成
tmuxinatorのdebug
オプションで生成します。引数にymlファイルの拡張子を除いた部分を指定します。
$ docker-compose run --rm tmuxinator debug my-project > my-project.sh
$ cat my-project.sh
#!/bin/bash
# Clear rbenv variables before starting tmux
unset RBENV_VERSION
unset RBENV_DIR
tmux start-server;
cd /c/Users/user
# Run pre command.
# Create the session and the first window. Manually switch to root
# directory if required to support tmux < 1.9
TMUX= tmux new-session -d -s my-project -n main
tmux send-keys -t my-project:0 cd\ /c/Users/user C-m
# Create other windows.
tmux new-window -c /c/Users/user -t my-project:1 -n sub
# Window "main"
tmux send-keys -t my-project:0.0 vagrant\ status C-m
tmux splitw -c /c/Users/user -t my-project:0
tmux select-layout -t my-project:0 tiled
tmux send-keys -t my-project:0.1 git\ status C-m
tmux select-layout -t my-project:0 tiled
tmux select-layout -t my-project:0 tiled
tmux select-pane -t my-project:0.0
# Window "sub"
tmux send-keys -t my-project:1.0 docker\ ps C-m
tmux select-layout -t my-project:1 tiled
tmux select-layout -t my-project:1
tmux select-pane -t my-project:1.0
tmux select-window -t 0
tmux select-pane -t 0
if [ -z "$TMUX" ]; then
tmux -u attach-session -t my-project
else
tmux -u switch-client -t my-project
fi
スクリプト実行
生成されたスクリプトを実行するとtmuxが設定された状態で立ち上がります。
$ chmod +x my-porject.sh # Windowsだと不要です
$ ./my-project.sh
以上です。