Chau Pham
Chau Pham

Reputation: 5080

What does "From image" do in dockerfiles

I see that dockerfiles usually have a line beginning with "from" keywork, for example:

FROM composer/composer:1.1-alpine AS composer

As far as I know, dockerfiles are a set of commands that help to build and run many containers in docker.

As the example above, docker uses a image named composer/composer:1.1-alpine from docker hub. The As composer just make an alias, so we can use it more convenient.

When I looked for the image, I found the link enter link description here and then enter link description here.

The thing I dont really understand is that:

I guess docker will use the image to build something, but how exactly does it use the image? Does docker run the image or just prepare to use it when in need. Sometimes I dont see the dockerfiles use the image in following line (like this example, there are no lines using the keyword "composer" except the first line). It makes me confused.

Any help would be appreciated. Thanks.

Upvotes: 1

Views: 1727

Answers (2)

Navin a.s
Navin a.s

Reputation: 416

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.

FROM can appear multiple times within a single Dockerfile to create multiple images or use one build stage as a dependency for another. Simply make a note of the last image ID output by the commit before each new FROM instruction. Each FROM instruction clears any state created by previous instructions.

Optionally a name can be given to a new build stage by adding AS name to the FROM instruction. The name can be used in subsequent FROM and COPY --from= instructions to refer to the image built in this stage.

The tag or digest values are optional. If you omit either of them, the builder assumes a latest tag by default. The builder returns an error if it cannot find the tag value.

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

Upvotes: 0

Christian Sauer
Christian Sauer

Reputation: 10909

DockerFiles describes layers: Each command creates it's own layer. For example: RUN touch test.txt RUN cp test.txt foo.txt would create two layers - the first one with the file test.txt, the second one without test.txt but with foo.txt

Each layer adds something to a container. When we walk the layers "up" we find that the very first layer is the empty layer, e.g. it contains only the linux (or windows) kernel itself - nothing else. But that's not really useful - we need a lot of tools (e.g. bash) to be able to run an app. So common base images like alpine add suc tools and core os functions.

It would be annoying as hell if we had to do this setup in every container so there a lots of base images, which do exactly this kind of setup. If you want to see what a base image does, just search the name on hub.docker.com - there you will find the Dockerfile, describing the build process.

Aditionally, containers can be extenend, e.g. you use the elasticsearch container as a base image, and add your own functionality - that's the second use case for base images.

For your second question: You have to decide if you have to replicate the steps in your base image or not. If you inherit from a general OS image like apline - probably not, since linux normally ships with these tools. If you inherit from a more specialized container, it depends - if your machine matches the environment in the container, you don't need to, but if not you will have to apply these steps to your machine, too. E.g. if you don't have elasticsearch installed, you have to install it.

As for multiple froms in one Dockerfile: Please look up the documentation for Multi Stage builds. Essentially, they encapsulate multiple containers in a single dockerfile. Which can be very useful if you need a different set to build an app and to run the app. The first Container is responsible to build your app, while the second one takes the compiled source code and just runs it. Watch for COPY --from= lines, these are copying files from one container to another.

Upvotes: 1

Related Questions