How to create an empty file in a scratch container in a Dockerfile?
Obviously, touch
is not available:
FROM scratch
RUN ["touch", ".emptyfile"]
Result:
container_linux.go:265: starting container process caused "exec: \"touch\": executable file not found in $PATH"
Unfortunately, /dev/null
is not available either:
FROM scratch
COPY /dev/null .emptyfile
Result:
COPY failed: stat /var/lib/docker/tmp/docker-builder872453691/dev/null: no such file or directory
I could create an empty file in the Docker host build context and then COPY
it, but you know, that would be too easy.
Any ideas?
There are no commands in scratch to run. The only options you have from scratch are COPY
and ADD
. And you can only copy or add files from the context (unless you want to ADD
from a remote url which I wouldn't recommend). So you are left to create an empty file in your context and copy that.
And then docker introduced multi stage builds, which let's you use another build as your context. So you can make an empty file in one stage and copy it to the other.
FROM busybox AS build-env
RUN touch /empty
FROM scratch
COPY --from=build-env /empty /.emptyfile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With