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

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.3WORKDIR /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-certificatesWORKDIR /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.


You can't combine dockerfiles as conflicts may occur. What you want to do is to create a new dockerfile or build a custom image.

TL;DR;If your current development container contains all the tools you need and works, then save it as an image and upon it to a repo and create a dockerfile to pull from that image off that repo.

Details:Building a custom image is by far easier than creating a dockerfile using a public image as you can store whatever hacks and mods into the image. To do so, start a blank container with a basic Linux image (or broadinstitute/scala-baseimage), install whatever tools you need and configure them until everything works correctly, then save it (the container) as an image. Create a new container off this image and test to see if you can build your code on top of it via docker-compose (or however you want to do/build it). If it works, than you have a working base image that you can upload to a repo so others can pull it.

To build a dockerfile with a public image, you will need to put all hacks, mods and setup on the dockerfile itself. That is, you will need to place every command line that you used into a text file and reduce whatever hacks, mods and setup into command lines. At the end, your dockerfile will create an image automatically and you don't need to store this image into a repo and all you need to do is to give others the dockerfile and they can spin the image up at their own docker.

Note that once you have a working dockerfile, you can tweak it easily as it will create a new image every time you use the dockerfile. With a custom image, you may run into issues where you need to rebuild the image due to conflicts. For example, all of your tools work with openjdk until you install one that doesn't work. The fix may involve uninstalling openjdk and use the oracle one, but all configuration you did for all the tools that you have installed broke.


The following answer applies to docker 1.7 and above:

I would prefer to use --from=NAME and from image as NAMEWhy?You can use --from=0 and above but this might get little hard to manage when you have many docker stages in dockerfile.

sample example:

FROM golang:1.7.3 as backendWORKDIR /backendRUN go get -d -v golang.org/x/net/html  COPY app.go .RUN  #install some stuff, compile assets....FROM golang:1.7.3 as assetsWORKDIR /assetsRUN ./getassets.shFROM nodejs:latest as frontend RUN npm installWORKDIR /assetsCOPY --from=assets /asets .CMD ["./app"] FROM alpine:latest as mergedassetsWORKDIR /root/COPY --from=merge ./COPY --from=backend ./backend .CMD ["./app"]

Note: Managing dockerfile properly will help to build a docker image must faster. Internally docker usings docker layer caching to help with this process, incase the image have to be rebuilt.