Docker: Combine multiple images

10,677

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
10,677
Frizz
Author by

Frizz

Updated on July 07, 2022

Comments

  • Frizz
    Frizz almost 2 years

    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.

  • Frizz
    Frizz over 9 years
    Thank your for your reply. Since I am very new to Docker (I installed it 3 hours ago ;-) a quick follow up question: Why do I need Dockerfiles? Can't I just create a, not sure how it's called in "Docker-terms", snapshot(?) of the Java container and then manually install MySQL (with apt-get, etc.) in it?
  • Adrian Mouat
    Adrian Mouat over 9 years
    You can do that with "docker commit". The problem is that it's not repeatable, so if you want to change something you have to start from scratch again. Dockerfiles are definitely the way to go.
  • luxas
    luxas almost 9 years
    'FROM can appear multiple times within a single Dockerfile in order to create multiple images.' - from your link. So, it doesn't combine the images, but instead create multiple images
  • TZubiri
    TZubiri over 5 years
    It's a good point that Java and mysql should be in separate dockers, but this is not always the case and the problem still stands, for example, I might want to install nginx + python, both of which have 2 images. Your solution is just a workaround for the diamond inheritance pattern.