Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of FROM command - Docker file

Main purpose of Docker container is to avoid carrying guest OS in every container, as shown below.

enter image description here As mentioned here, 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.

My understanding is, FROM <image> allow a container to run on its own OS.


Why a valid Docker file must have FROM instruction?

like image 597
overexchange Avatar asked Jan 02 '19 00:01

overexchange


3 Answers

Containers don't run a full OS, they share the kernel of the host OS (typically, the Linux kernel). That's the "Host Operating System" box in your right image.

They do provide what's called "user space isolation" though - roughly speaking, this means that every container manages its own copy of the part of the OS which runs in user mode- typically, that's a Linux distribution such as Ubuntu. In your right image, that would be contained in the "Bins/Libs" box.

You can leave out the FROM line in your Dockerfile, or use FROM scratch, to create a blank base image, then add all the user mode pieces on top of a blank kernel yourself.

like image 82
Max Avatar answered Sep 27 '22 21:09

Max


Another common use of FROM is to chain builds together to form a multi-stage build of smaller images.

This would be useful for instance to limit redundant rebuilding during failed auto-builds.

like image 41
Richard Barber Avatar answered Sep 27 '22 20:09

Richard Barber


FROM instruction specifies the underlying OS architecture that you are gonna use to build the image. You have to use some form of base image for you to get started with building an image. It can be ubuntu, centos or any minimal linux image like ALPINE which is only 5MB!. The idea is to install only the packages you need rather than having everything bundled and packaged as a distribution. This makes the size of the docker images very small as compared to the full blown OS distribution. I hope this answers your question. Let me know if you have any questions.

like image 23
Varun Babu Pozhath Avatar answered Sep 27 '22 22:09

Varun Babu Pozhath