Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I build docker images from nothing?

Tags:

docker

image

I have some docker experience already, but all images I built were based on other images.

I still don't know where did the first docker image come from?
In another word, what if I need a perticular image, say ubuntu, which is quite different from official image?

Update on 20180928:
Based on present answer and comment, it seems like that scratch is the very original image. Every image did build from it. So here is the question: what is scratch? Could you guys explain it with common words please?

Update on 20180929:
According to the information linked in comments, indeed, the image(say, light/hello:latest) built FROM scratch does not have extra files but what have added. After starting a container from light/hello:latest, there are more files and directories except for the files we added.

And now I know, since every container must have a root filesystem for execution, these extra files and directories are created by container runtime implementation(runC/libcontainer) based on the runC libcontainer spec v1, which follows the OCI runtime specification.

Now back on my confusion: What's scratch? Is it some trick Docker reserved? Just like Java project, always starting with a main function: public static void main(String[] args)?

Update 20181008:
Now I think the very first way I should figure out is that what an docker image actually is. And how could it be done? If possible and necessary, you may put some source code here. Please show me light.

Thanks in advance.

like image 621
Light.G Avatar asked Sep 28 '18 06:09

Light.G


People also ask

Can you build a Docker image without Docker?

Buildah provides a CLI tool that allows users to build OCI or traditional Docker images. Buildah can be used to create and run images from a Dockerfile and without. In our case, we are going to use Buildah to build the image and Podman to run the image.

Can we create our own Docker image?

This is the recommended workflow for creating your own Docker image for your application: Write a Dockerfile for your application. Build the image with docker build command. Host your Docker image on a registry.

What are the ways to create Docker image?

First way: Package-only Build In a package-only build, we will let Maven (or your build tool of choice) control the build process. …and then build the Docker image. The following command tells Docker to fetch the Dockerfile in the current directory (the period at the end of the command).


2 Answers

A base image either has no FROM line in its Dockerfile, or has FROM scratch.

A parent image is the image that your image is based on. It refers to the contents of the FROM directive in the Dockerfile.

Create a simple parent image using scratch

You can use Docker’s reserved, minimal image, scratch, as a starting point for building containers. Using the scratch “image” signals to the build process that you want the next command in the Dockerfile to be the first filesystem layer in your image.

While scratch appears in Docker’s repository on the hub, you can’t pull it, run it, or tag any image with the name scratch. Instead, you can refer to it in your Dockerfile. For example, to create a minimal container using scratch:

FROM scratch
ADD hello /
CMD ["/hello"]

Assuming you built the “hello” executable example by following the instructions at https://github.com/docker-library/hello-world/, and you compiled it with the -static flag, you can build this Docker image using this docker build command:

docker build --tag hello .

FROM scratch an explicitly empty image, especially for building images "FROM scratch"

This image is most useful in the context of building base images (such as debian and busybox) or super minimal images (that contain only a single binary and whatever it requires, such as hello-world).

scratch does not add a layer to your docker image. but why scratch? it because of

A root filesystem must be provided to a container for execution. The container will use this root filesystem (rootfs) to jail and spawn processes inside where the binaries and system libraries are local to that directory. Any binaries to be executed must be contained within this rootfs.

develop-images baseimages

like image 177
Adiii Avatar answered Sep 28 '22 17:09

Adiii


Instead of being FROM a server or distribution image base, a Dockerfile can declare itself FROM scratch. "scratch" is a special image name that contains absolutely nothing – no files, no directories, I think it even has no image layers.

The most common use of FROM scratch is to create an extremely small image around a statically compiled binary (most often in Go, also possible in C/C++, not possible in languages like Java or Python that need an interpreter). This can look like

FROM scratch
COPY app /
ENTRYPOINT ["/app"]

If you were to somehow look at the image you'd see only the single file /app plus a couple of injected system bits (I think you always get a /dev and /proc for instance). This is tricky, because there's no shell you can run (there's no /bin directory unless you explicitly create one).

The other place "scratch" appears is in base images, like this debian:stable Dockerfile:

FROM scratch
ADD rootfs.tar.xz /
CMD ["bash"]

Again, starting from absolutely nothing, unpack a tar file of a Debian installation and use that as a starting point for things going forward.

like image 38
David Maze Avatar answered Sep 28 '22 16:09

David Maze