Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an empty file in a scratch container?

How to create an empty file in a scratch container in a Dockerfile?

Try 1

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"

Try 2

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?

like image 346
Pierre Prinetti Avatar asked Jan 05 '18 22:01

Pierre Prinetti


1 Answers

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
like image 166
BMitch Avatar answered Sep 30 '22 01:09

BMitch