How do I run a Bash script in an Alpine Docker container?
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.
Remember to grant execution permission for all scripts.
FROM alpine
COPY sayhello.sh /sayhello.sh
RUN chmod +x /sayhello.sh
CMD ["/sayhello.sh"]
Alpine comes with ash as the default shell instead of bash
.
So you can
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
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
By using the CMD
, Docker is searching the sayhello.sh
file in the PATH
, BUT you copied it in /
which is not in the PATH
.
So use an absolute path to the script you want to execute:
CMD ["/sayhello.sh"]
BTW, as @user2915097 said, be careful that Alpine doesn't have Bash by default in case of your script using it in the shebang.