I’m trying to create a simple script which would:
- Connect to docker container’s BASH shell
- Go into redis-cli
- Perform a
flushall
command in redis-cli
So far, I have this in my docker_script.sh
(this basically copies the manual procedure):
docker exec -it redis /bin/bash
redis-cli
flushall
However, when I run it, it only connects to the container’s BASH shell and doesn’t do anything else. Then, if I type exit
into the container’s BASH shell, it outputs this:
root@5ce358657ee4:/data# exit
exit
./docker_script.sh: line 2: redis-cli: command not found
./docker_script.sh: line 3: keys: command not found
Why is the command not found
if commands redis-cli
and flushall
exist and are working in the container when I perform the same procedure manually? How do I "automate" it by creating such a small BASH script?
Thank you
2
Answers
Seems like you’re trying to run
/bin/bash
inside the redis container, while theredis-cli
andflushall
commands are scheduled after in your current shell instance. Try passing in yourredis-cli
command to bash like this:The
-c
is used to tell bash to read a command from a string.Excerpt from the
man
page:To answer your further question in the comments, you want to run a single script,
redis_flushall.sh
to run that command. The contents of that file are:Breaking that down, you are calling
redis-cli auth MyRedisPass
as a bash command, andflushall
as another bash command. The issue is,flushall
is not a valid command, you’d want to callredis-cli flushall instead
. Command chaining is something that has to be implemented in a CLI application deliberately, not something that falls out of the cracks.If you replace the contents of your script with the following, it should work, i.e., after
;
add aredis-cli
call before specifying theflushall
command.The above proposed solution with auth still got me an error
(error) NOAUTH Authentication required
This worked for me: