Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple base images to build a docker image

Tags:

I have used a base docker image (openjdk) to create an image having a simple Java Programme.

But I was wondering if I use 2 base images in a docker file. How can we do that? I don't think docker supports anything like:

FROM dockerImaage1 FROM dockerImage2 

One of the scenarios, why this is required is I want an image of ubuntu having openjdk, so one way is I use the base image of ubuntu and the in docker file write instruction to install openjdk, set JAVA_HOME variable etc, which is undoubtedly cumbersome.

Another alternative, I like is using the base image of openjdk inside base of ubuntu (if possible).

There may be more typical cases, where we may need is feature damn badly.

SO any ideas on how to use 2 base images in a docker file? Has anyone done that yet?

I found a link of reverse engineering here, but it has some limitations like if the docker file of ubuntu uses commands like ADD or COPY, the reverse engineering fails.

like image 316
Prashant Prabhakar Singh Avatar asked May 02 '17 04:05

Prashant Prabhakar Singh


People also ask

Can you use multiple base images docker?

Using multi-stage dockerfiles, you can use several base images as well as previous intermediate image layers to build a new image layer.

What is multistage docker build?

A multistage Docker build is a process to create a Docker image through a series of steps. Each stage accomplishes a task -- such as to load or remove build tools -- specific to the base Docker image, as part of the compilation process.

Can you have multiple froms in docker?

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.

Can we have multiple images in one container?

Run one service per container. Use the official images on docker hub for mongodb, nodejs, rabbitmq, nginx etc. Extend them if needed. If you want to run everything in a fat container you might as well just use a VM.


1 Answers

The latest version of docker has the concept of multi stage builds. Refer: (https://docs.docker.com/engine/userguide/eng-image/multistage-build/)

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

like image 120
user2019864 Avatar answered Sep 23 '22 17:09

user2019864