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

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, so your file will begin with 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

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", ""]

And this works too.

Remember to grant execution permission for all scripts.

FROM alpine
RUN chmod +x /
CMD ["/"]