0

I would like to run curl inside a docker image. It is working in docker container like this:

curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"INFO"}'  localhost:8091/mobile-svc/actuator/loggers/something

when I want to run it from a bash script out from docker container like this:

args="sh -c \"curl -i -X POST -H 'Content-Type: application/json' -d'{\"configuredLevel\":\"${LEVEL}\"}' localhost:${PORT}/mobile-svc/actuator/loggers/${PACKAGE}\""
echo $args
cmd="docker exec -it $CID $args"
eval $cmd

it said:

sh -c "curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"DEBUG"}' 
localhost:8091/tcp/mobile-svc/actuator/loggers/something
HTTP/1.1 404 
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 08 Aug 2020 12:49:58 GMT
{"timestamp":"2020-08-08T12:49:58.949+0000","status":404,"error":"Not Found","message":"No message available","path":"/tcp/mobile-svc/actuator/loggers/something"}

How can I pass compound parameters to curl?

thx Zamek

zamek 42
  • 793
  • 1
  • 9
  • 15
  • Does this answer your question? [How to store a command in a variable in a shell script?](https://stackoverflow.com/a/44055875/10248678) – oguz ismail Aug 08 '20 at 13:50
  • In general, you shouldn't need to script `docker exec` calls. That's doubly true for something that's making a network call: run `curl` from your host connecting to the published port of the container. – David Maze Aug 08 '20 at 14:33
  • Unfortunately not. – zamek 42 Aug 08 '20 at 18:17
  • declare curl_args() curl_args=("curl" "-i" "-X" "POST" "-H" "Content-Type: application/json" "-d" "{\"configuredLevel\":\"${LEVEL}\"}" "localhost:${PORT}/mobile-svc/actuator/loggers/${PACKAGE}") declare cmd=("exec" "-it" "$CID" "sh" "-c" "\"${curl_args[@]}\"") echo $cmd eval $(docker ${cmd[@]}) the seems good, but eval result is: curl: try 'curl --help' or 'curl --manual' for more information – zamek 42 Aug 08 '20 at 18:28
  • your `${PORT}` variable seems to resolve to `8091/tcp` which messes up your querystring. I guess you should use a variable that resolves to `8091` only to make it work. – lab9 Aug 08 '20 at 21:01
  • I previously changed port with ${PORT%\/tcp}. The echoed command like is that: docker exec -it 649f6e6af264 sh -c curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"DEBUG"}' localhost:8091/mobile-svc/actuator/loggers/something – zamek 42 Aug 09 '20 at 07:09

1 Answers1

0

Finally I've found the solving, but it is a little bit ugly:

cmd="docker exec -it $CID sh -c \"curl -i -X POST -H 'Content-Type: application/json' -d '{\\\"configuredLevel\\\":\\\"$LEVEL\\\"}' localhost:${PORT}/mobile-svc/actuator/loggers/something\""
zamek 42
  • 793
  • 1
  • 9
  • 15