Davide
Davide

Reputation: 73

How to run parametrized bash scripts in Docker CLI

I have an issue that I'm not able to solve. I want to run a bash script that is inside my Docker CLI container, and I want to execute it, passing parameters. Usually, I run scripts using a notation like this:

docker exec -i $CLI_ID bash "./script.sh"

But I don't know how to pass parameters to the script. I tried to execute it with:

docker exec -i $CLI_ID bash "./script.sh PARAM" 

But it doesn't work. How can I do it?

Thanks

Upvotes: 0

Views: 147

Answers (3)

that other guy
that other guy

Reputation: 123490

Add your parameter as a separate argument rather than as part of the filename:

docker exec -i "$CLI_ID" bash "./script.sh" PARAM

This way, you don't have to add another level of escaping to the parameter.

Upvotes: 0

Ijaz Ahmad
Ijaz Ahmad

Reputation: 12110

Make sure the script is executable , then you dont need the bash and double quotes etc. Just run it striaght away by path/name and provide the options.

[root@ap-p1m-ff ~]# docker exec -i 0c cat /tmp/test.sh
#!bin/bash

ls $1


[root@ap-p1m-ff ~]# docker exec -i 0c /tmp/test.sh -l
total 4
-rw-r--r--   1 root root 159 Jun  4 18:32 RELEASE
drwxr-xr-x   2 root root 120 Jun  4 18:33 assets
drwxr-xr-x   1 root root  31 Jun  4 18:33 bin
drwxr-xr-x   2 root root   6 Apr 12  2016 boot
drwxr-xr-x   5 root root 340 Jun  7 19:50 dev
drwxr-xr-x   1 root root  22 Jun  7 19:50 etc

but if the script is not already executble the bash should work as well:

[root@ap-p1m-ff ~]# docker exec -i 0c bash /tmp/test.sh -l
total 4
-rw-r--r--   1 root root 159 Jun  4 18:32 RELEASE
drwxr-xr-x   2 root root 120 Jun  4 18:33 assets
drwxr-xr-x   1 root root  31 Jun  4 18:33 bin
drwxr-xr-x   2 root root   6 Apr 12  2016 boot
drwxr-xr-x   5 root root 340 Jun  7 19:50 dev
drwxr-xr-x   1 root root  22 Jun  7 19:50 etc
drwxr-xr-x   2 root root   6 Apr 12  2016 home
drwxr-xr-x   1 root root  45 Sep 13  2015 lib
drwxr-xr-x   2 root root  34 May 15 14:22 lib64
drwxr-xr-x   2 root root   6 May 15 14:22 media

Upvotes: 0

mchawre
mchawre

Reputation: 12268

Try with bash -c option

docker exec -i $CLI_ID bash -c "./script.sh PARAM"

Hope this helps.

Upvotes: 3

Related Questions