redis / docker-library-redis

Docker Official Image packaging for Redis
http://redis.io
BSD 3-Clause "New" or "Revised" License
1.12k stars 563 forks source link

bash string issue #306

Closed dianshane closed 2 years ago

dianshane commented 2 years ago

Hello, while installing an app(Mayan edms specifically) i encountered issues connecting to Redis server.

After contacting the developers we concluded that it was a credentials/connection url issue.

Finally after debugging a lot, I found out there seems to be an issue with the way bash passes the strings to your container when used with "-c" or as "runuser -u --".

I used the alpine image, but it shouldn't be any different since the image doesn't have anything to do with the issue as far as i could find. (I also used the debian one, but i had also made other changes so i cant surely say that I know its the same issue, although its the most propable senario since i got the exact same exception)

I don't know if this is the right place for this, and i also don't know the exact nature of the issue or if its fixable or not. When i know more, after working a while with the new setup, i will definitely update you.

Please feel free to move this to the appropriate location, just make sure i can find it to post any updates.

tianon commented 2 years ago

I'm sorry, can you give a bit more detail about why this is related to the Redis image (or what you think we might be able to change in the image to fix it)? :sweat_smile:

dianshane commented 2 years ago

As of now I don't think there is anything that can be done to fix it!

It probably is just a bash issue with the strings and outside the grasp of the container itself.

If I do find a fix, I will post a merge request to fix it.

As of now I just wanted to inform you of this, since loads of people use bash scripts for different operations with containers, including Redis, and you might want to comment in your documentation to have this in mind.

As always have nice day and happy coding Sotiris

yosifkit commented 2 years ago

Without knowing what your docker run is and what the problem is, I'll drop a few shell string hints that might help out. I'll use echo in place of redis-server to make it easy to see the result.

$ docker run -it --rm -e var=12345 redis bash -c "echo $var"

$ # nothing but the newline from echo
$ # this is because the string is in double quotes so it is parsed and variable expansion is done by the host shell
$ # before the `docker` command gets anything and this is what it sees:
$ # docker run -it --rm -e var=12345 redis bash -c "echo "
$ docker run -it --rm -e var=12345 redis bash -c 'echo $var'
12345
$ # because it is in single quotes the host shell does not so variable expansion on the string
$ # this time the container shell parses and expands the variables and then runs: echo 12345

$ # if we try to prevent the host shell from evaluating it and don't use shell in the container to parse and run
$ docker run -it --rm -e var=12345 redis echo '$var'
$var
$ # echo gets the literal string and prints it out
$ docker run -it --rm -e var=12345 redis echo "$var"

$ # host shell again... random fact that echo does get a single argument of the empty string
dianshane commented 2 years ago

Without knowing what your docker run is and what the problem is, I'll drop a few shell string hints that might help out. I'll use echo in place of redis-server to make it easy to see the result.

$ docker run -it --rm -e var=12345 redis bash -c "echo $var"

$ # nothing but the newline from echo
$ # this is because the string is in double quotes so it is parsed and variable expansion is done by the host shell
$ # before the `docker` command gets anything and this is what it sees:
$ # docker run -it --rm -e var=12345 redis bash -c "echo "
$ docker run -it --rm -e var=12345 redis bash -c 'echo $var'
12345
$ # because it is in single quotes the host shell does not so variable expansion on the string
$ # this time the container shell parses and expands the variables and then runs: echo 12345
$ # if we try to prevent the host shell from evaluating it and don't use shell in the container to parse and run
$ docker run -it --rm -e var=12345 redis echo '$var'
$var
$ # echo gets the literal string and prints it out
$ docker run -it --rm -e var=12345 redis echo "$var"

$ # host shell again... random fact that echo does get a single argument of the empty string

thank you for your reply! The issue was in passing runtime variable parameters withing a docker command that was run using "runuser" (like the following code) and is now solved (I will also mark this as closed since it might be confusing)!

runuser -u someuser -- docker run (parameters)\
-e RUNTIME_VAR_PARAMETER="some value"  \
(other params);

I mostly opened this to inform you about this inconvenience since we all mostly use docker-compose and rarely get these types of errors and you might not have known about it!

It also obviously is a general docker thing, i just contacted you since i use your images everyday and felt like you deserved to know.

Still Very many thanks for your reply!