1

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.

StevieD
  • 6,925
  • 2
  • 25
  • 45
  • In the general case, there isn't, and if the two images aren't as tightly related as you suggest here, it's likely impossible (you couldn't merge Alpine- and Debian-based images with different system C libraries). – David Maze Dec 18 '22 at 11:38

1 Answers1

1

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.

StevieD
  • 6,925
  • 2
  • 25
  • 45
  • 1
    This isn't likely to work reliably. Since you're copying literally everything, including the `/lib` and `/etc` trees, you're getting a kind of random mish-mash of the two images, and the conflicts in shared libraries or system files could cause hard-to-debug problems. – David Maze Dec 18 '22 at 11:36