Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a Bash script in an Alpine Docker container?

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

. ├── Dockerfile └── sayhello.sh 

The Dockerfile reads

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

and sayhello.sh contains simply

echo hello 

The 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 to run it I get an executable file not found in $PATH error:

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 is causing this? I recall running scripts in debian:jessie-based images in a similar manner. So perhaps it is Alpine-specific?

like image 252
Kurt Peek Avatar asked Jun 28 '17 13:06

Kurt Peek


People also ask

Does alpine support bash?

The first step is to keep the core system as small as possible. Alpine never installs a lot of stuff that users will never use but might be handy. One example is the Bash shell. There is no Bash installed by default; Alpine uses BusyBox Bash as the default shell.

Does alpine have bin bash?

Because docker run command with --entrypoint=/bin/bash, but Alpine Linux have not /bin/bash. Alpine Linux is a next standard based image. I think there is a need to support it.

How do I run bash inside a container?

In order to start a Bash shell in a Docker container, execute the “docker exec” command with the “-it” option and specify the container ID as well as the path to the bash shell. If the Bash is part of your PATH, you can simply type “bash” and have a Bash terminal in your container.


2 Answers

Alpine comes with ash as the default shell instead of bash.

So you can

  1. Have a shebang defining /bin/bash as the first line of your sayhello.sh, so your file sayhello.sh will begin with bin/sh

    #!/bin/sh 
  2. Install Bash in your Alpine image, as you seem to expect Bash is present, with such a line in your Dockerfile:

    RUN apk add --no-cache --upgrade bash 
like image 196
user2915097 Avatar answered Sep 21 '22 13:09

user2915097


This answer is completely right and works fine.

There is another way. You can run a Bash script in an Alpine-based Docker container.

You need to change CMD like below:

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

And this works too.

like image 32
Shahriar Avatar answered Sep 19 '22 13:09

Shahriar