Home > other >  Merging/combining pre-existing docker images possible?
Merging/combining pre-existing docker images possible?

Time:12-18

Here's a hypothetical scenario: I have Image A, with the task command installed on it and Image B with fzf. Both images are built from alpine. I understand that I can do a multi-stage build like this to produce a single image with both commands on it:

FROM alpine as stage1
RUN apk add task 

FROM stage1 as final
RUN apk add fzf
CMD ash

But if Image A and Image B are already built, is there a way to merge the two?

I found this cool project called docker-merge but it throws errors.

CodePudding user response:

It looks like this can be accomplished using something like COPY from=ImageA / /.

Here's an example Dockerfile that worked for me, combining an image with the task on it named tw and another, separate image with ripgrep and fzf on it name ripgres-fzf:

FROM tw

COPY --from=ripgrep-fzf / /

After building this Dockerfile and then running the container, the image contains all three commands (task, fzf, and ripgrep). The / / was a brute force copy to ensure configuration files get carried over. Probably a better way to do that. See https://levelup.gitconnected.com/docker-multi-stage-builds-and-copy-from-other-images-3bf1a2b095e0 for a tutorial.

CodePudding user response:

I've used the pattern where you build a base image that then becomes the FROM statement in your layer 01 image. I've heard this called "inheritance" or "extension" of the base image. It's common when you don't have access to the base Dockerfile

for example

I've built GCP Dataflow pipelines where you want to have shared resources across multiple images, so you will bundle common functions into the base image that is then inherited by your deployed images

  • Related