Docker: Combine Multiple Images

Is there a way to combine Docker images into 1 container?

You can, with the multi-stage builds feature introduced in Docker 1.17

Take a look at this:

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]

Then build the image normally:

docker build -t alexellis2/href-counter:latest

From : https://docs.docker.com/develop/develop-images/multistage-build/

The end result is the same tiny production image as before, with a significant reduction in complexity. You don’t need to create any intermediate images and you don’t need to extract any artifacts to your local system at all.

How does it work? The second FROM instruction starts a new build stage with the alpine:latest image as its base. The COPY --from=0 line copies just the built artifact from the previous stage into this new stage. The Go SDK and any intermediate artifacts are left behind, and not saved in the final image.

Is there a way to combine two Docker base images in a single container and build with kaniko?

Here's the minimum Dockerfile that bake JRE 11 to the mongo image.

FROM mongo:latest

# Replace the version if desired
RUN apt-get update -y && apt-get install openjdk-11-jre-headless -y

# Install your app and stuffs here...

# Override for your own command
CMD ["java","-version"]

Build the image docker build -t mongodb-java .

Test the image docker run -t --rm mongodb-java will output the JRE version.

Test the image docker run -t --rm mongodb-java mongo --version will output the MongoDB version.

You can then follow Kaniko steps to build the image.

Merge two docker images

You can't just merge the images. You have to recreate your own based on what was in each of the images you want. You can download both images and re-create the Docker files for each like this:

docker history --no-trunc=true image1 > image1-dockerfile
docker history --no-trunc=true image2 > image2-dockerfile

Substitute the image1 and image2 with the images you want to see the history for. After this you can use those dockerfiles to build your own image that is the combination of the two.

The fly in the ointment here is that any ADD or COPY commands will not reveal what was copied because you don't have access to the local file system from which the original images were created. With any luck that won't be necessary or you can get any missing bits from the images themselves.

Combining multiple docker images

With what you afforded as next, in fact you are using multi-stage-build, but only the last image will be act as the base image of final produced image. The final produced image won't have anything which in the former 3 images, unless you explicitly copy things to the last stage. So this definitely won't meet your requirement:

FROM prom/prometheus:latest AS prometheus
FROM bitnami/node-exporter:latest as node-exporter
FROM prom/pushgateway:latest AS pushgateway
FROM bitnami/alertmanager:latest AS alert-manager

In fact, what you are seeking is not the philosophy of docker. See Multi container apps:

Up to this point, we have been working with single container apps. But, we now want to add MySQL to the application stack. The following question often arises - “Where will MySQL run? Install it in the same container or run it separately?” In general, each container should do one thing and do it well. A few reasons:

  • There’s a good chance you’d have to scale APIs and front-ends differently than databases
  • Separate containers let you version and update versions in isolation
  • While you may use a container for the database locally, you may want to use a managed service for the database in production. You don’t want to ship your database engine with your app then.
  • Running multiple processes will require a process manager (the container only starts one process), which adds complexity to container startup/shutdown

And there are more reasons. So, we will update our application to work like this:

Sample Image

From above, docker suggest you to deploy different separate application to different containers. So, what you tried with your start.sh is OK with docker philosophy, you can't combine different images together with any straightforward way.

Additional, you could use docker-compose or k8s to improve the way to deploy these containers, of course this could be another topic.

How can I run TWO Docker images in a SINGLE Container?

Found the solution

Here is my Github URL: https://github.com/yogig/dockercron/

FROM php:7.2-fpm-alpine
LABEL maintainer="xxx@xxx.de" \ muz.customer="xxx" \ muz.product="xxx" \ container.mode="development"
#https://pkgs.alpinelinux.org/packagesRUN apk add --no-cache --virtual .deps autoconf tzdata build-base libzip-dev mysql-dev gmp-dev \ libxml2-dev libpng-dev zlib-dev freetype-dev jpeg-dev icu-dev openldap-dev libxslt-dev &&\ docker-php-ext-install zip xml mbstring json intl gd pdo pdo_mysql iconv soap \ dom gmp fileinfo sockets bcmath mysqli ldap xsl &&\ echo 'date.timezone="Europe/Berlin"' >> "${PHP_INI_DIR}"/php.ini &&\ cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime &&\ echo 'Europe/Berlin' > /etc/timezone &&\ curl -s https://www.phing.info/get/phing-latest.phar > /bin/phing &&\ chmod a+x /bin/phing &&\ ln -s /bin/phing /usr/local/bin/phing &&\ ln -s /bin/phing /usr/local/phing &&\ ln -s /bin/phing /usr/bin/phing &&\ apk del .deps &&\ apk add --no-cache libzip mysql libxml2 libpng zlib freetype jpeg icu gmp git subversion libxslt openldap \ apache2 apache2-ldap apache2-proxy libreoffice openjdk11-jre ghostscript msttcorefonts-installer \ terminus-font ghostscript-fonts &&\ ln -s /usr/lib/apache2 /usr/lib/apache2/modules &&\ ln -s /usr/sbin/httpd /etc/init.d/httpd &&\ update-ms-fonts
# copy crontabs for root userCOPY crontab.txt /etc/crontabs/root
#https://github.com/docker-library/httpd/blob/3ebff8dadf1e38dbe694ea0b8f379f6b8bcd993e/2.4/alpine/httpd-foreground#https://github.com/docker-library/php/blob/master/7.2/alpine3.10/fpm/DockerfileCMD ["/bin/sh", "-c", "rm -f /usr/local/apache2/logs/httpd.pid && httpd -DBACKGROUND && php-fpm"]


Related Topics



Leave a reply



Submit