Is `FROM` clause required in Dockerfile?

后端 未结 3 1131
清酒与你
清酒与你 2021-01-15 01:38

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

3条回答
  •  梦毁少年i
    2021-01-15 02:39

    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.

提交回复
热议问题