簡體   English   中英

Bash - 連接到 Docker 容器並在 redis-cli 中執行命令

[英]Bash - Connect to Docker container and execute commands in redis-cli

我正在嘗試創建一個簡單的腳本:

  1. 連接到 docker 容器的 BASH shell
  2. Go 進入redis-cli
  3. 在 redis-cli 中執行flushall命令

到目前為止,我的docker_script.sh中有這個(這基本上復制了手動過程):

docker exec -it redis /bin/bash
redis-cli
flushall

但是,當我運行它時,它只連接到容器的 BASH shell 並且不做任何其他事情。 然后,如果我在容器的 BASH shell 中鍵入exit ,它會輸出:

root@5ce358657ee4:/data# exit
exit
./docker_script.sh: line 2: redis-cli: command not found
./docker_script.sh: line 3: keys: command not found

當我手動執行相同的過程時,如果命令redis-cliflushall存在並且正在容器中工作,為什么command not found命令? 我如何通過創建這么小的 BASH 腳本來“自動化”它?

謝謝

上面提出的帶有 auth 的解決方案仍然給我一個錯誤

(error) NOAUTH Authentication required

這對我有用:

docker exec -it redis /bin/sh -c 'redis-cli -a MYPASSWORD FLUSHALL'

似乎您正在嘗試在 redis 容器內運行/bin/bash ,而redis-cliflushall命令則在您當前的 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM