Ben Wex
Ben Wex

Reputation: 53

Docker containers with multiple bases?

I don't really understand something basic about Docker, specifically if I wanted to build from multiple bases within the same Dockerfile. For example, I know that these two lines wouldn't work:

FROM X
FROM Y

(well it would compile but then it seems to only include the image from X in the final build). Or perhaps I'm wrong and this is correct but I still haven't seen any other Dockerfiles like this.

Why would I want to do this? For instance, if X and Y are images I found on DockerHub that I would like to build from. For a concrete example if I wanted ubuntu and I also wanted python:

FROM python:2 
FROM ubuntu:latest 

What is the best way to go about it? Am I just limited to one base? If I want the functionality from both am I supposed to go into the docker files until I find something in common to both of them and build the image myself by copying the one of the dockerfile's code manually all the way through sub images until I reach the common base and add those lines to the other Dockerfile? I imagine this is not the correct way to do this as it seems quite involved and not in line with the simplicity that Docker aims to provide.

Upvotes: 2

Views: 271

Answers (2)

BMitch
BMitch

Reputation: 263906

Multiple FROM lines in a Dockerfile are used to create a multi-stage build. The result of a build will still be a single image, but you can run commands in one stage, and copy files to the final stage from earlier stages. This is useful if you have a full compile environment with all of the build tools to compile your application, but only want to ship the runtime environment with the image you deploy. Your first stage would be something like a full JDK with Maven or similar tools, while your final stage would be just your JRE and the JAR file copied from the first stage.

Upvotes: 2

Adiii
Adiii

Reputation: 59966

For a concrete example if I wanted ubuntu and I also wanted python:

FROM python:2  
FROM ubuntu:latest

Ubuntu is Os, not a python. so what you need a Ubuntu base image which has python installed.

you can check offical python docker hub are based on ubuntu, so at one image you will get ubuntu + python, then why bother with two FROM? which is not also not working.

Some of these tags may have names like buster or stretch in them. These are the suite code names for releases of Debian and indicate which release the image is based on. If your image needs to install any additional packages beyond what comes with the image, you'll likely want to specify one of these explicitly to minimize breakage when there are new releases of Debian.

So for you below question

What is the best way to go about it? Am I just limited to one base? If I want the functionality from both am I supposed to go into the docker files

yes, limit it one base image suppose your base image

python:3.7-stretch

So with this base image, you have python and ubuntu both. you do not need to make Dockerfile that have two FROM.

Also, you do need to maintain and build the image from scratch, use the offical one and extend as per your need.

For example

FROM python:3.7-stretch
RUN apt-get update && apt-get install -y  vim
RUN pip install mathutils

Upvotes: 2

Related Questions