0

I have a bash script that is custom tool for running docker containers. I can't show entire script. In the following scenario:

    local dockerArgs=(
            "--restart=on-failure:5"
            "--memory 512m"
            "--cpu-shares 1024"
            "--pids-limit 100"
            "--health-cmd='stat /etc/passwd'"
            "--security-opt=no-new-privileges"
            "-v /containers/logs/:/containers/logs/"
            "-v /etc/conf/prometheus/:/etc/conf/prometheus/"
    )

, all the params are processed correctly except the health-cmd param. It the only one with the single-quoted value. The script fails due to the above syntax for health-cmd. Grateful for any ideas to fix. Thanks

Timothy Clotworthy
  • 1,960
  • 2
  • 19
  • 42
  • 2
    Remove the single quotes and make sure you quote your expansion `"${dockerArgs[@]}"` – jordanm Feb 08 '22 at 16:08
  • @jordanm I am already doing that: sudo docker run ${cloudArgs[@]} ${dockerArgs[@]} -e ... – Timothy Clotworthy Feb 08 '22 at 16:15
  • You are missing the quotes in your example. `sudo docker run "${cloudArgs[@]}" "${dockerArgs[@]}" -e` – jordanm Feb 08 '22 at 16:16
  • You should also have `-v` be a separate array element from `/containers/logs:/containers/logs`; similarly, `--pids-limit` should be a separate array element from `100`. – Charles Duffy Feb 08 '22 at 16:18
  • Also, use `local -a` when defining an array. – Charles Duffy Feb 08 '22 at 16:19
  • @jordanm I now have: "--health-cmd=stat /etc/passwd" , and sudo docker run "${cloudArgs[@]}" "${dockerArgs[@]}" -e JAVA_OPTS="${JAVA_OPTS}" --name "${applicationName}" -d -t ${dockerRegistry}/${containerName}:${version} ${opts[@]} and it still is failing. – Timothy Clotworthy Feb 08 '22 at 16:33
  • they are making me create a new question. claiming its a duplicate of https://stackoverflow.com/questions/45325659/expand-arguments-from-array-containing-double-quotes – Timothy Clotworthy Feb 08 '22 at 16:37

1 Answers1

1

--health-cmd='stat /etc/passwd' and "--health-cmd=stat /etc/passwd" are equivalent words, each of which is equivalent to the pair of words --health-cmd and stat /etc/passwd to your program. You can write either

local dockerArgs=(
   ...
   --health-cmd 'stat /etc/passwd'
   ...
)

or

local dockerArgs=(
   ...
   "--health-cmd=stat /etc/passwd"  # or --health-cmd='stat /etc/passwd'
   ...
)

and

docker ... "${dockerArgs[@]}"

will work the same same either way.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    Very much correct as far as it goes, but the `health-cmd` argument isn't the only one where the OP needs to fix their quoting. f/e, `"--cpu-shares 100"` should be `"--cpu-shares" "100"` or just `--cpu-shares 100`. – Charles Duffy Feb 08 '22 at 16:20
  • Hi. I tried both of the above and neither worked. Here is how the actual run command vars are expanded in: # Start the container sudo docker run ${cloudArgs[@]} ${dockerArgs[@]} -e JAVA_OPTS="${JAVA_OPTS}" --name "${applicationName}" -d -t ${dockerRegistry}/${containerName}:${version} ${opts[@]} – Timothy Clotworthy Feb 08 '22 at 16:22
  • You aren't quoting the array expansions as instructed. – chepner Feb 08 '22 at 16:34
  • @chepner , I now have: "--health-cmd=stat /etc/passwd" , and sudo docker run "${cloudArgs[@]}" "${dockerArgs[@]}" -e JAVA_OPTS="${JAVA_OPTS}" --name "${applicationName}" -d -t ${dockerRegistry}/${containerName}:${version} ${opts[@]} and it still is failing. – Timothy Clotworthy Feb 08 '22 at 16:51
  • See Charles Duffy's comment: that's not the only option (or rather, pair of options) you have misquoted in the definition of the array. – chepner Feb 08 '22 at 16:51
  • E.g., either `(... "--memory" "512m" ...)` or `(... "--memory=512m")`, but not `(... "--memory 512m" ...)`. – chepner Feb 08 '22 at 16:53
  • @chepner the memory param is not an issue. All params are processed correctly other than health-cmd. Charles addressed the params that are NOT causing me issues but not the one the isn't working. – Timothy Clotworthy Feb 08 '22 at 16:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241828/discussion-between-timothy-clotworthy-and-chepner). – Timothy Clotworthy Feb 08 '22 at 17:05