0

I am trying to write a shell script that should start a docker container. I have a running example, but it uses /bin/bash:

#!/bin/bash

export NETWORK=jetty-svc-test
export IMAGE_TAG=jetty-test-app
export SVC_NAME=svc-test

clean_up() {
    docker stop $SVC_NAME
    docker network rm $NETWORK
}

docker network create $NETWORK
if [ $? -eq 1 ]; then
    exit 1
fi

docker build --tag $IMAGE_TAG .
if [ $? -eq 1 ]; then
    exit 1
fi

docker run -it --rm --name $SVC_NAME --network=$NETWORK -v $(pwd)/app:/app -d $IMAGE_TAG
if [ $? -eq 1 ]; then
    exit 1
fi
sleep 5

export RES=$(docker run --rm --network=$NETWORK curlimages/curl:7.71.1 http://$SVC_NAME:8080 | docker run --rm -i stedolan/jq .)
export HEALTH_CHECK=$(echo "$RES" | docker run --rm -i stedolan/jq -r .status)

if [ "$HEALTH_CHECK" != "I am healthy" ]; then
    clean_up
    exit 1
fi

clean_up

but I need it in #!/bin/sh. For instance, the statement export RES=$(.. does not work in #!/bin/sh.

I need to rewrite the script above, because https://hub.docker.com/layers/docker/library/docker/19.03.12/images/sha256-d208a88b6afa09430a2f4becbc8dbe10bfdaeb1703f9ff3707ca96e89358a8e4?context=explore only supports #!/bin/sh and I would like to run the script above inside the docker:dind container.

How to write docker command with /bin/sh and assign result to variable?

softshipper
  • 32,463
  • 51
  • 192
  • 400
  • 1
    Isn't `/bin/sh` usually symlinked to `bash` (or `dash` on debian-based systems, or `zsh` on macos) – jkr Jul 30 '20 at 14:53
  • I have rewritten my post. – softshipper Jul 30 '20 at 15:07
  • 1
    `export RES=$(.. does not work in #!/bin/sh`? What does it mean "not work"? Please first check your script with shellcheck.net `if [ $? -eq 1 ]; then` is an antiipattern, do `if ! command args args args; then`. – KamilCuk Jul 30 '20 at 15:14
  • 1
    You can also try with back-ticks: `FOO=\`docker run ...\``. – jkr Jul 30 '20 at 15:21
  • 1
    @jakub Aren't you answering your own question? `/bin/sh` is often symlinked to *something,* but that *something* is a moving target. `dash` is pretty close to a bare-bones POSIX `sh`. `zsh` is a completely separate can of worms (though it can be set up to emulate both `bash` and `sh`). – tripleee Jul 30 '20 at 16:35

2 Answers2

4

You seem to be asking how to rewrite

export variable=$(command)
export other="value"

and the answer for POSIX sh is simply

variable=$(command)
export variable

other="value"
export other

See also Where is “export var=value” not available? (on Unix / Linux Stack Exchange).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • You should probably also learn to quote your variables. See [When to wrap quotes around a shell variable](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Jul 30 '20 at 16:39
0

As mentioned in the comment, you can check the script again shellcheck, I am not going to address the issue in shell script, as a suggestion in comment should work, but I will just to create a custom image by extending docker:dind, which will have bash as well as other utility that you are trying pull docker image for jq etc,

By doing this approach you will save network bandwidth plus CPU time.

FROM docker:dind
RUN apk add --no-cache bash jq curl

Now run this docker:dind image instead of the offical image, your script will become something like

docker run -dit --rm --name $SVC_NAME -p 3000:3000 --network=$NETWORK  jetty-test-app:latest
if [ $? -eq 1 ]; then
    exit 1
fi
sleep 2

RES=$(curl localhost:3000/users | jq .)

export HEALTH_CHECK=$(echo "$RES" | jq .status)

if [ "$HEALTH_CHECK" != "I am healthy" ]; then
    clean_up
    exit 1
fi
clean_up
Adiii
  • 54,482
  • 7
  • 145
  • 148