Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Combine multiple images

Is it possible with Docker to combine two images into one?

Like this here:

genericA --
            \
             ---> specificAB
            /
genericB --

For example there's an image for Java and an image for MySQL.

I'd like to have an image with Java and MySQL.

like image 671
Frizz Avatar asked Nov 30 '14 15:11

Frizz


People also ask

Can you combine 2 docker images?

Docker doesn't do merges of the images, but there isn't anything stopping you combining the dockerfiles if available, and rolling into them into a fat image which you'd need to build.

Can we use multiple images in Docker file?

One approach to keeping Docker images small is using multistage builds. A multistage build allows you to use multiple images to build a final product. In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image.

Can Dockerfile have 2 base images?

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


3 Answers

No, you can only inherit from one image.

You probably don't want Java and MySQL in the same image as it's more idiomatic to have a single component in a container i.e. create a separate MySQL container and link it to the Java container rather than put both into the same container.

However, if you really must have them in the same image, write a Dockerfile with Java as the base image (FROM statement) and install MySQL in the Dockerfile. You should be able to largely copy the statements from the official MySQL Dockerfile.

like image 155
Adrian Mouat Avatar answered Oct 02 '22 15:10

Adrian Mouat


Docker doesn't directly support this, but you can use DockerMake (full disclosure: I wrote it) to manage this sort of "inheritance". It uses a YAML file to set up the individual pieces of the image, then drives the build by generating the appropriate Dockerfiles.

Here's how you would build this slightly more complicated example:

                                     --> genericA --                                     /                \       debian:jessie -->  customBase                   ---> specificAB                                     \                /                                       --> genericB -- 

You would use this DockerMake.yml file:

specificAB:   requires:     - genericA     - genericB  genericA:   requires:      - customBase   build_directory: [some local directory]   build: |     #Dockerfile commands go here, such as     ADD installA.sh     RUN ./installA.sh  genericB:   requires:     - customBase   build: |     #Here are some other commands you could run     RUN apt-get install -y genericB     ENV PATH=$PATH:something  customBase:   FROM: debian:jessie   build: |     RUN apt-get update && apt-get install -y buildessentials 

After installing the docker-make CLI tool (pip install dockermake), you can then build the specificAB image just by running

docker-make specificAB 
like image 40
Aaron V Avatar answered Oct 02 '22 15:10

Aaron V


If you do docker commit, it is not handy to see what commands were used in order to build your container, you have to issue a docker history image

If you have a Dockerfile, just look at it and you see how it was built and what it contains.

Docker commit is 'by hand', so prone to errors, docker build using a Dockerfile that works is much better.

like image 44
user2915097 Avatar answered Oct 02 '22 16:10

user2915097