LoginSignup
2
1

More than 3 years have passed since last update.

Dockerコンテナ内での作業中に うっかり Ctrl-D で終了させないようにする

Posted at

Dockerコンテナにログインして, シェルであれこれ操作しているときに うっかりCtrl+D を押してコンテナを終了させてしまい, ムカつくことはないでしょうか (ぼくはありませんが……).

% docker run -it -p5901:5901 ubuntu:20.04 bash
root@dcc1b0029b1f:/# exit  # Ctrl-D押下でexitされる
%

ignoreeof オプション (or 組み込み変数) で回避する

コンテナにログイン後に,

$ set -p ignoreeof

または

$ export IGNOREEOF=10

とすることで, 10回のCtrl-D連続入力までは, Ctrl-Dでシェルを終了させないようにできます.

% docker run -it -ubuntu:20.04 bash
root@00126ad53ed2:/# set -o ignoreeof
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# Use "exit" to leave the shell.
root@00126ad53ed2:/# exit
%

exportコマンドでセットした場合は, セットした値の分だけ連続押下しないと入力が無視されるということです. setコマンドの場合は10回です.

exportコマンドできるということは, コンテナ起動時にもセットできるってことですね.

docker run -it -eIGNOREEOF=1 ubuntu:20.04 bash

おまけ 1 IGNOREEOF (ignoreeof) ってなんなのか

IGNOREEOF がなにかというと, Bashの組み込み変数です. man bash をみると, Shell Variables セクションに登場します.

完全に余談ですが, このオプション zshでも使えます. これがPOSIXによる仕様なのか, bash/zshそれぞれの仕様/実装なのか気になって調べてみました. POSIX仕様で定められているようです. POSIXのドキュメントに登場します.

おまけ 2 POSIX非準拠のシェル (ここでは fish) の場合は?

POSIX非準拠のシェルではどうしたらいいのか. そんなに興味がなかったんですが, 最近使うようになったfish の場合を調べてみました.

IGNOREEOF は使えません. キーバインドを設定してやるのがいいかなと思います.

$ bind | grep "\\cd"
bind --preset \cd delete-or-exit

Ctrl-Dに delete-or-exit (文字の削除or Exit) が割り当てられているので, delete-char へ差し替えてやる?

$ bind \cd delete-char
2
1
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
2
1