Kurt Peek
Kurt Peek

Reputation: 57471

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?

Upvotes: 3

Views: 7997

Answers (2)

DShook
DShook

Reputation: 15664

If you got here as I did looking for solutions to the "No such file or directory" error running scripts in Alpine, another thing to check is that your script has unix line endings.

In my case, I was saving the script in windows and adding it into the docker image and it was giving that same unhelpful error message.

Upvotes: 2

Rawkode
Rawkode

Reputation: 22592

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

Upvotes: 4

Related Questions