Working around "Text file busy" when a shell script fails to run during docker build
Overview
I had written a shell execution inside a Dockerfile like the following.
1 | RUN chmod +x hoge.sh \ |
When I ran docker build with the above in place, I hit an error like this.
1 | /bin/sh: hoge.sh: Text file busy |
What is Text file busy ?
It occurs when you try to execute a file (a shared-text file) that is currently open only for writing, or when you try to open for writing or delete a file that is a running procedure.
Given the above, my guess was:
maybe it happens because hoge.sh is being executed while chmod +x hoge.sh is still running??
Environment
- Ubuntu 14.04.5 LTS \n \l
- Docker version 17.05.0-ce, build 89658be
- Base Image: ruby:2.5-alpine
Solution
Adding the sync step below resolved the problem cleanly.
1 | RUN chmod +x hoge.sh \ |
What is sync command ?
sync - システム管理コマンドの説明 - Linux コマンド集 一覧表
sync - システム管理コマンドの説明。sync - ディスク上のデータをメモリと同期させる。
References
Running chmod on file results in 'text file busy' when running straight after. · Issue #9547 · moby/moby
Dockerfile contents are: FROM debian WORKDIR /app COPY . /app CMD [ "/app/a.sh" ] Contents of 'a.sh' are: #!/bin/sh echo 'A' sh /app/b.sh 1 …

cpとmvとinodeの話 - Qiita
実行中のファイルに対して、 cpで上書きする場合だと、Text file busyで置き換えられず、 mvだと何も聞かれず置き換えられます。 この挙動の違いについて、まとめと実験。 inodeを見ると、それとなく分かります。 以下引用 リンクを全く持たない inode...
Working around "Text file busy" when a shell script fails to run during docker build
https://kenzo0107.github.io/en/2018/04/18/fix-text-file-busy-on-docker-build/
