33

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.

Prashant Prabhakar Singh
  • 1,120
  • 4
  • 15
  • 33
  • 3
    Many images out there have their Dockerfile published as well. There is seldom the need to reverse engineer. – Henry May 02 '17 at 04:18
  • 1
    Possible duplicate of [Is there a way to combine Docker images into 1 container?](http://stackoverflow.com/questions/39626579/is-there-a-way-to-combine-docker-images-into-1-container) – François Maturel May 02 '17 at 07:04

3 Answers3

21

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.

user2019864
  • 265
  • 2
  • 5
  • 8
    This is a borderline [link-only answer](//meta.stackexchange.com/q/8231). You should expand your answer to include as much information here, and use the link only for reference. – Filnor Nov 07 '17 at 10:12
  • 2
    This is borderline use of the word "borderline" – Nebulosar Jun 22 '23 at 11:50
7

The answer is NO. See the detail discussions in Moby issue, How do I combine several images into one via Dockerfile.

The idea behind containers is that the smallest unit of real composition is the container. In general, merging multiple images to one image could introduce some issues. For example, different images have conflict at library, or library version.

CloudStax
  • 649
  • 5
  • 6
6

Easily? No. Docker does not support this as it would be effectively combining multiple Linux root filesystems.

The easiest way to do this would be to start from Ubuntu and then install openjdk, or find an image on docker hub which does so already.

tcnj
  • 495
  • 4
  • 10