slimtoolkit / slim

Slim(toolkit): Don't change anything in your container image and minify it by up to 30x (and for compiled languages even more) making it secure too! (free and open source)
Apache License 2.0
19.34k stars 726 forks source link

Slimify based on multiple commands? #515

Open Pithikos opened 1 year ago

Pithikos commented 1 year ago

Scenario

I have a single image that is deployed in 3 different ways:

  1. Running migrations of the app
  2. Deploy app1 internally on port 5000
  3. Deploy app2 externally on port 3000

In each case there's some overlap between files used but not all are used. Running docker-slim against (2) ends up removing files used by case (1) and (3) for example.

How can I solve this? Alternatively is there a way to skip specific files?

I'd prefer to not split into 3 separate containers, since everything is tightly coupled and that will introduce a lot of unnecessary maintenace.


Specifications

kcq commented 1 year ago

This is a very interesting use case @Pithikos !

The first thing you can try is one/some of the --include-** flags (e.g., --include-path, which can point to a file or to a directory). Those --include-** flags allow you to keep extra things in the minified container image. There's a number of new (already committed, but not in a release) flags like --include-app-image-all (that gets used with --app-image-start-instruction-group) that allow you to keep everything that's in your application part of the image.

Another thing to try is the --exec flag to run extra shell commands during the runtime analysis phase that would allow you to ensure the unused parts of your images get used.

In some cases, wrapping all three (or how many) ways to invoke your container in a shell script (which could be mounted in the build command) and then making this wrapper shell the entrypoint (using the --entrypoint flag) when you execute the slim build command can accomplish what you are looking for.

There's also a new option on the todo list that should help too. There'll be a new slim command called merge where you can point to your minified images and it'll combine all of them into one. It's a relatively straight forward enhancement, so it should be available soon.

kcq commented 1 year ago

@Pithikos The new release has an experimental merge command that can help with your use case: https://github.com/slimtoolkit/slim/releases/tag/1.40.3

Once you have your 3 minified images that correspond to each of your execution profiles you can merge the first two and then merge the result image of the initial merge with the last minified image. Let me know how it goes and if you encounter any gotchas or if you have any comments or asks there.