Search code examples
dockershellshalpine-linux

Alpine 3.18 Docker image not respecting `sleep` shell command after a background command run with &


I've been using a simple script with a while loop inside it as the entry point to a Docker container to run a command every minute. A sort of simple cronjob so to speak. The while loop looks like this:

#!/bin/sh

while [ true ]
do
  php script.php &
  sleep 60
done

This has been working fine until I updated the base image to be built off of Alpine 3.18, and it now no longer respects the sleep 60 command and just reruns the php script immediately.

You can test this by running the following simple docker locally with versions 3.17 and 3.18 of Alpine respectively:

docker run --rm -it alpine:3.17 sh -c "while [ true ]; do echo 1 & sleep 3; done;"

Correctly runs and waits 3 seconds in between echoing 1.

docker run --rm -it alpine:3.18 sh -c "while [ true ]; do echo 1 & sleep 3; done;"

Just starts streaming 1's to the screen without sleeping for 3 seconds.

Has something changed in the syntax for a shell script in 3.18 and/or BusyBox? Just as a note, the script has the same issue be it a one liner like the docker run example, or in it's own .sh file formatted like the top example.


Solution

  • This is a bug in busybox ash that has already been fixed upstream but not included in a release (as of this writing, at which point v1.36.1 is the latest available).

    The fix is included in commit 7362d2979434c565ae70b0ccf9d4b09d7597fb48; you can thus expect this behavior to no longer take place when a new busybox release is next available.

    As suggested by Grobu, you might switch to dash in the interim: this bug is related to busybox trying to call its own sleep.