jayunit100
jayunit100

Reputation: 17648

What are the different ways of implementing Docker FROM?

Inheritance of an image is normally done using docker's from command, for example.

from centos7:centos7 

In my case, I have a Dockerfile which I want to use as a base image builder, and I have two sub dockerfiles which customize that file.

I do not want to commit the original dockerfile as a container to dockerhub, so for example, I would like to do:

Dockerfile
slave/Dockerfile
master/Dockerfile

Where slave/Dockerfile looks something like this:

from ../Dockerfile

Is this (or anything similar) possible ? Or do I have to actually convert the top level Dockerfile to a container, and commit it as a dockerhub image, before I can leverage it using the docker FROM directive.

Upvotes: 0

Views: 498

Answers (1)

wassgren
wassgren

Reputation: 19221

You don't have to push your images to dockerhub to be able to use them as base images. But you need to build them locally so that they are stored in your local docker repository. You can not use an image as a base image based on a relative path such as ../Dockerfile- you must base your own images on other image files that exists in your local repository.

Let's say that your base image uses the following (in the Dockerfile):

FROM        centos7:centos7
// More stuff...

And when you build it you use the following:

docker build -t my/base .

What happens here is that the image centos7:centos7 is downloaded from dockerhub. Then your image my/base is built and stored (without versioning) in your local repository. You can also provide versioning to your docker container by simply providing version information like this:

docker build -t my/base:2.0 .

When an image has been built as in the example above it can then be used as a FROM-image to build other sub-images, at least on the same machine (the same local repository). So, in your sub-image you can use the following:

FROM        my/base

Basically you don't have to push anything anywhere. All images lives locally on your machine. However, if you attempt to build your sub-image without a previously built base image you get an error.

For more information about building and tagging, check out the docs:

Upvotes: 3

Related Questions