8

For all the Dockerfiles I've come across thus (which admittedly is not many), all of them have used a FROM clause to base off an existing image, even if it's FROM scratch.

Is this clause required? Is it possible to have a Dockerfile with no FROM clause? Will this container created thus be able to do anything?

EDIT
I read

A Dockerfile with no FROM directive has no parent image, and is called a base image.

https://docs.docker.com/glossary/?term=parent%20image

But I think this may be an error.

2
  • 1
    What does it do when you try it? Commented Oct 10, 2019 at 19:42
  • 2
    I've submitted a PR to help clarify the wording. Commented Oct 10, 2019 at 20:47

3 Answers 3

11

Based on the official documentation it's required:

The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile MUST start with a FROM instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.

https://docs.docker.com/engine/reference/builder/#from

Sign up to request clarification or add additional context in comments.

2 Comments

I found this page by Googling, but did not scroll down enough...
Found a link that seems to be worded erroneously (in the EDIT above) - would you agree?
7

Short answer is yes, the FROM clause is required. But it's easier to come to this conclusion if you think of the image building process a bit.

Dockerfile is just a way to describe a sequence of commands to be executed by Docker build subsystem to create an image. And an image is just a bunch of regular files, most notably, user land files of a particular Linux distribution, but possibly with some extra files on top of it. Every Docker image is based on the parent image and adds its own files to the parent's set. Every image has to start FROM something, i.e. specify its parent. And the parent of all parents is a scratch image defined as noop, i.e. an empty set of files.

Take a look at busybox image:

FROM scratch ADD busybox.tar.xz / CMD ["sh"] 

It starts from scratch, i.e. an empty set of files, and adds (i.e. copies) to this set a bunch of files from busybox.tar.xz archive.

Now, if you want to create your own image, you can start from busybox image and describe what files (and how) you are going to add:

FROM busybox:latest ADD myfile.txt / 

But every time a new image has to start FROM something.

1 Comment

A somewhat philosophical point: Conceptually, we can also imagine starting with nothing and building up - the Dockerfile parser could have written to take the absence of FROM to mean the same as FROM scratch. This behavior would be consistent with the usage of include and import statements in some languages, where the omission of these would simply mean nothing is "brought in" explicitly.
1

Yes, it is. It defines the layers on which the image you are building is based on. If you want to start an image from scratch docker offers an image called scratch

The documentation also says:

A parent image is the image that your image is based on

also

A base image has FROM scratch in its Dockerfile.

Refer to base images documentation

1 Comment

Ok, thanks - just saw link that seems to be in error (in the EDIT above) - would you agree?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.