Search code examples
dockerexpectalpine-linux

running expect spawn in docker alpine - no such file or directory


I'm new to bash testing and wanted to create a docker image to test a cli. I discovered 'expect' and wanted to try it against this example bash script. Only problem is its failing to spawn a file.

data/que.sh

#!/bin/bash
echo "Enter your name"
read $REPLY
echo "Enter your age"
read $REPLY
echo "Enter your salary"
read $REPLY

data/test.sh

#!/usr/bin/expect -f
 
set timeout 2
spawn /data/que.sh
expect "Enter your name\r"
send -- "I am Steve\r"
expect "Enter your age\r"
send -- "2\r"
expect "Enter your salary\r"
send -- "10k\r"
expect eof

docker-compose.yml

services:
  linux-expect:
    container_name: cont-alpine-linux-expect
    image: img-alpine-linux-expect
    build:
      context: .

Dockefile

FROM alpine:latest
RUN apk add --update nodejs-current npm

USER root
RUN apk update && apk add expect

RUN mkdir data
COPY data /data

CMD ["node", "-e","setTimeout(() => console.log(999999), 99999999)"]
# Just run temporary timeout so we can interact with script

terminal command

  • docker-compose up --build

new terminal

  • docker exec -it cont-alpine-linux-expect sh
  • expect data/test.sh

I am getting an error to find this file, but it does exist. I am the root user.

Error

"spawn /data/que.sh" (file "data/test.sh" line 4)


Solution

  • The problem is that it can't find bash. You have

    #!/bin/bash
    

    as the first line in your script, so the shell looks for bash to run it. But bash isn't installed in Alpine images.

    You can see if your script runs with /bin/sh. I believe that's the ash shell in Alpine images. If that doesn't work, you can install bash in the image using apk.