0

I am running the below script and getting error.

#!/bin/bash
webproxy=$(sudo docker ps -a --format "{{.Names}}"|grep webproxy)
webproxycheck="curl -k -s https://localhost:\${nginx_https_port}/HealthCheckService"
if [ -n "$webproxy" ] ; then
  sudo docker exec $webproxy sh -c "$webproxycheck"
fi

Here is my docker ps -a output

$sudo docker ps -a --format "{{.Names}}"|grep webproxy
webproxy-dev-01
webproxy-dev2-01

when i run the command individually it works. For Example:

$sudo docker exec webproxy-dev-01 sh -c 'curl -k -s https://localhost:${nginx_https_port}/HealthCheckService'
HEALTHCHECK_OK

$sudo docker exec webproxy-dev2-01 sh -c 'curl -k -s https://localhost:${nginx_https_port}/HealthCheckService'
HEALTHCHECK_OK

Here is the error i get.

$ sh healthcheck.sh
OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"webproxy-dev-01\": executable file not found in $PATH": unknown

Could someone please help me with the error. Any help will be greatly appreciated.

2
  • You don't need sudo or docker exec for this at all; you should be able to just invoke that curl command against a published port for your container. Commented Aug 29, 2020 at 17:46
  • sudo docker is necessary on some platforms like Red Hat, but e.g. on Debian-based platforms you should simply add your user to the docker group and then run docker without sudo. Commented Oct 10, 2020 at 9:06

2 Answers 2

1

Because the variable contains two tokens (on two separate lines) that's what the variable expands to. You are running

sudo docker exec webproxy-dev-01 webproxy-dev2-01 ...

which of course is an error.

It's not clear what you actually expect to happen, but if you want to loop over those values, that's

for host in $webproxy; do
    sudo docker exec "$host" sh -c "$webproxycheck"
done

which will conveniently loop zero times if the variable is empty.

If you just want one value, maybe add head -n 1 to the pipe, or pass a more specific regular expression to grep so it only matches one container. (If you have control over these containers, probably run them with --name so you can unambiguously identify them.)

Sign up to request clarification or add additional context in comments.

Comments

0

Based on your given script, you are trying to "exec" the following

sudo docker exec webproxy-dev2-01
webproxy-dev-01 sh -c "curl -k -s https://localhost:${nginx_https_port}/HealthCheckService"

As you see, here is your error.

sudo docker exec webproxy-dev2-01
webproxy-dev-01 [...]

The problem is this line:

webproxy=$(sudo docker ps -a --format "{{.Names}}"|grep webproxy)

which results in the following (you also posted this):

webproxy-dev2-01 
webproxy-dev-01

Now, the issue is, that your docker exec command now takes both images names (coming from the variable assignment $webproxy), interpreting the second entry (which is webproxy-dev-01 and sepetrated by \n) as the exec command. This is now intperreted as the given command which is not valid and cannot been found: That's what the error tells you.


A workaround would be the following:

webproxy=$(sudo docker ps -a --format "{{.Names}}"| grep webproxy | head -n 1)

It only graps the first entry of your output. You can of course adapt this to do this in a loop.

A small snippet:

#!/bin/bash

webproxy=$(sudo docker ps -a --format "{{.Names}}"| grep webproxy )
echo ${webproxy}

webproxycheck="curl -k -s https://localhost:\${nginx_https_port}/HealthCheckService"

while IFS= read -r line; do
    if [ -n "$line" ] ; then
        echo "sudo docker exec ${line} sh -c \"${webproxycheck}\""
    fi   
done <<< "$webproxy"

1 Comment

In the OP's original code, the variable is not quoted, so the newline between the values is normalized to a single space by the shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.