How to run a Bash script in an Alpine Docker container?

I have a directory containing only two files, Dockerfileand sayhello.sh:

.
├── Dockerfile
└── sayhello.sh

Dockerfile is reading

FROM alpine
COPY sayhello.sh sayhello.sh
CMD ["sayhello.sh"]

and sayhello.shcontains just

echo hello

Dockerfile builds successfully:

kurtpeek@Sophiemaries-MacBook-Pro ~/d/s/trybash> docker build --tag trybash .
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM alpine
 ---> 665ffb03bfae
Step 2/3 : COPY sayhello.sh sayhello.sh
 ---> Using cache
 ---> fe41f2497715
Step 3/3 : CMD sayhello.sh
 ---> Using cache
 ---> dfcc26c78541
Successfully built dfcc26c78541

However, if I try runto do this, I get an error executable file not found in $PATH:

kurtpeek@Sophiemaries-MacBook-Pro ~/d/s/trybash> docker run trybash
container_linux.go:247: starting container process caused "exec: \"sayhello.sh\": executable file not found in $PATH"
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"sayhello.sh\": executable file not found in $PATH".
ERRO[0001] error getting events from daemon: net/http: request canceled

What causes this? (I remember running scripts on debian:jessie-based images in a similar way, so maybe this applies to Alpine)?

+32
source share
4 answers

Alpine comes with ash as the default shell instead bash.

So you can

  1. sayhello.sh /bin/bash, sayhello.sh .

    #!/bin/bash
    
  2. Bash Alpine, , , , Bash, Dockerfile:

    RUN apk add --no-cache --upgrade bash
    
+49

.

. Bash Docker Alpine.

CMD, :

CMD ["sh", "sayhello.sh"]

.

+16

.

FROM alpine
COPY sayhello.sh /sayhello.sh
RUN chmod +x /sayhello.sh
CMD ["/sayhello.sh"]
+7

CMD, Docker sayhello.sh PATH, /, PATH.

, :

CMD ["/sayhello.sh"]

By the way, as @ user2915097 said, be careful, since Alpine does not have a default Bash if your script uses it in shebang.

+3
source

All Articles