我正在嘗試創建一個簡單的腳本,它將:
- 連接到 docker 容器的 BASH shell
- 進入 redis-cli
flushall在 redis-cli 中執行命令
到目前為止,我在我的docker_script.sh(這基本上復制了手動程式)中有這個:
docker exec -it redis /bin/bash
redis-cli
flushall
但是,當我運行它時,它只連接到容器的 BASH shell,不執行任何其他操作。然后,如果我輸入exit容器的 BASH shell,它會輸出以下內容:
root@5ce358657ee4:/data# exit
exit
./docker_script.sh: line 2: redis-cli: command not found
./docker_script.sh: line 3: keys: command not found
當我手動執行相同的程序時,為什么command not foundif 命令redis-cli和flushall存在并在容器中作業?如何通過創建這么小的 BASH 腳本來“自動化”它?
謝謝
uj5u.com熱心網友回復:
似乎您正在嘗試/bin/bash在 redis 容器內運行,而redis-cli和flushall命令則是在您當前的 shell 實體之后安排的。嘗試將您的redis-cli命令傳遞給 bash ,如下所示:
docker exec -it redis /bin/bash -c "redis-cli FLUSHALL"
本-c是用來告訴bash從字串中讀取命令。
man頁面摘錄:
-c string If the -c option is present, then commands are read from
string. If there are arguments after the string, they
are assigned to the positional parameters, starting with
$0.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326490.html
