Drone

How to run shell script in docker:dind

I have created a drone pipeline, that looks as follows:

kind: pipeline
type: docker
name: Build Jetty Keycloak image

services:
  - name: docker
    image: docker:dind
    privileged: true
    volumes:
      - name: dockersock
        path: /var/run

steps:
  - name: Test Jetty service
    image: docker:dind
    volumes:
      - name: dockersock
        path: /var/run
    commands:
      - ls -la 
      - pwd
      - ./service_test.sh

  - name: tag the commit
    image: hub.databaker.io/devops/git-auto-tagger:0.2.3
    environment:
      REPO: ${DRONE_GIT_HTTP_URL}
      USER:
        from_secret: gituser
      PW:
        from_secret: gitpw
      PRE: ""
    commands:
      - git_tagger
      - sleep 1

volumes:
  - name: dockersock
    temp: {}

trigger:
  branch:
    - master

image_pull_secrets:
  - dockerconfig

the build failed on ./service_test.sh with the following message:

+ ls -la
total 32
drwxr-xr-x    5 root     root          4096 Jul 28 20:13 .
drwxr-xr-x    3 root     root          4096 Jul 28 20:13 ..
-rw-r--r--    1 root     root          1245 Jul 28 20:13 .drone.yml
drwxr-xr-x    8 root     root          4096 Jul 28 20:13 .git
-rw-r--r--    1 root     root           225 Jul 28 20:13 Dockerfile
drwxr-xr-x    2 root     root          4096 Jul 28 20:13 app
drwxr-xr-x    9 root     root          4096 Jul 28 20:13 jetty
-rwxr-xr-x    1 root     root           737 Jul 28 20:13 service_test.sh
+ pwd
/drone/src
+ ./service_test.sh
/bin/sh: ./service_test.sh: not found

the content of the shell script:

#!/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 health" ]; then
    clean_up
    exit 1
fi

clean_up

Why it is not possible the run service_test.sh?

Thanks

The reason this fails is because your script references bash (using shebang #!/bin/bash) however /bin/bash does not exist in the docker:dind image. The only available shell is /bin/sh

Here are the commands I executed to verify:

$ docker run -t -i docker:dind /bin/sh
/ # which bash
/ # which sh
/bin/sh
1 Like