richard
richard

Reputation: 2053

docker run bash difference

I used docker container to backup the database, like script below

docker run --rm -i -e dbuser=root dbpass=xfdZdfd dbhost=abc.come dbport=32 docker.abc.com/tools/mysql-client bash -c "mysqldump -u$dbuser -p$dbpass -h$dbhost -P$dbport db1 > db1.sql"

Then I create a different image with env into the image, like add the script to docker file

ENV dbuser root
ENV dbpass xfdZdfd
ENV dbhost abc.come
ENV dbport 32

In the command line, I have to use ' instead of ", like command below

docker run --rm -i  docker.abc.com/tools/mysql-client bash -c 'mysqldump -u$dbuser -p$dbpass -h$dbhost -P$dbport db1 > db1.sql'

Since the difference is to add variables, why I have to use ' or "?

Upvotes: 1

Views: 1699

Answers (1)

etene
etene

Reputation: 728

You have to use single quotes (') in this kind of scenario to prevent shell substitution.

Shell example:

$ WHAT=sandwiches
$ echo "I like $WHAT"
I like sandwiches
$ echo 'I like $WHAT'
I like $WHAT

If you use double quotes when you call docker, the variables will be replaced by your own shell instead of the shell inside the container.

Since they probably haven't got any value in your shell, they will be replaced with empty strings before the command is executed in the container, which is why the single quotes that prevent that are necessary.

Upvotes: 1

Related Questions