Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a bash script in Alpine Linux?

I'm interested in using wait-for-it to make one service in Docker-Compose wait for another, using images based on the Alpine Linux distribution.

However, if I clone the wait-for-it repository and add to it the following Dockerfile,

FROM alpine
RUN apk --update add bash
COPY wait-for-it.sh wait-for-it.sh
CMD ["./wait-for-it.sh", "www.google.com:80"]

Then I build it using docker build --tag waitforit . followed by docker run waitforit, but I get this error message:

timeout: can't execute '15': No such file or directory
wait-for-it.sh: timeout occurred after waiting 15 seconds for www.google.com:80

By contrast, this is what I see when running this command on my (Ubuntu 16.04 LTS) computer:

wait-for-it.sh: waiting 15 seconds for www.google.com:80
wait-for-it.sh: www.google.com:80 is available after 0 seconds

It seems like the bash script is not working in the Alpine container as it is on my Ubuntu local machine. How can I fix this?

like image 351
Kurt Peek Avatar asked Jul 25 '26 16:07

Kurt Peek


1 Answers

That wait-for-it script isn't sh compliant, so you'd need to install bash into your alpine image.

However, may I suggest avoiding "hacks" like this and utilising HEALTHCHECK instead?

For example, having one service wait for another to be healthy would look like this:

services:
  my_service:
    image: something
    healthcheck:
      test: nc -z 3306

  other_service
  depends_on:
    my_service:
      condition: service_healthy

Please note, this will only work with v2 compose file, not v3

like image 113
Rawkode Avatar answered Jul 27 '26 08:07

Rawkode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!