一個容器跑起來,有時候得修改內部的配置文件。但是容器內部的 shell 有時甚至連個 vi 都不帶,笨方法,docker cp 出來,改完再放進去。你知道的問題還好改,如果是邊調試連修改,就很煩人了。
於是,我就寫了個函數。可以實現修改后多次直接上傳。
效果圖:

進入容器查看文件是否修改成功

代碼很簡單,不解釋了。
function docker.open(){ local container="$1" local infile="$2" local outfile=/tmp/`basename $infile` docker cp $container:$infile $outfile && gedit $outfile || return $? local update="" local times=1 echo -e "\033[31m回車更新, exit 退出\033[0m" until [ "$update" == 'exit' ] do read -p "第 $times 次更新:" update [ "$update" == 'exit' ] && break docker cp $outfile $container:$infile || { echo "更新失敗!"; return 1; } times=`expr $times + 1` done }
