Moving then executing script in Dockerfile causes "Text file busy" error

I have a Dockerfile that moves a script, then executes the destination script. The move command seems to be uncompleted by the time of the execution command, causing a “Text file busy” error.

This is unexpected behaviour because mv script.sh /dir1 && /dir1/script.sh seems like a very simple sequence of commands. I have this in another Dockerfile and it was failing on 9 out of 10 builds.

I’ve created a simple Dockerfile to reproduce this issue: https://github.com/eugenesia/circleci-debug/blob/70f021/Dockerfile .

The build log showing this error is below. sleep.sh is just a dummy script that sleeps for 30s. The full log is on https://circleci.com/gh/eugenesia/circleci-debug/67 .

...
Step 3/3 : RUN mkdir /dir1 && mv /sleep.sh /dir1 && /dir1/sleep.sh
 ---> Running in f8c19e91fed4
/bin/sh: 1: /dir1/sleep.sh: Text file busy
The command '/bin/sh -c mkdir /dir1 && mv /sleep.sh /dir1 && 
  /dir1/sleep.sh' returned a non-zero code: 2
Exited with code 2

Feel free to keep rebuilding to reproduce the error.

Thanks,
Eugene

If I make it sleep and wait for the mv command to complete before executing the script, the error is fixed. E.g. mkdir /dir1 && mv /script.sh /dir1 && sleep 5s && /dir1/script.sh.

The modified Dockerfile with additional sleep command: https://github.com/eugenesia/circleci-debug/blob/1048fb/Dockerfile

After making this modification, the build succeeds on https://circleci.com/gh/eugenesia/circleci-debug/68 .

This fix is weird though and shouldn’t be necessary to fix a simple command like this.