Mike Conrad ed0611a954 Fixed typo
2024-10-23 11:18:49 -04:00
2024-10-23 10:42:55 -04:00
2024-10-23 10:42:55 -04:00
2024-10-23 10:42:55 -04:00
2024-10-23 10:42:55 -04:00
2024-10-23 10:42:55 -04:00
2024-10-23 11:18:49 -04:00

Overview

The purpose of this repo is to demonstrate some of the functionality of slimtoolkit for optimizing container images. This is a simple example using the carbone library to convert an .odt file to a pdf and replace some templatized variables with user input.

Running in Docker

The first step is to build the base Docker image and test it out.

docker build -t slimtoolkit-example .
docker run -p 8000:8000 slimtoolkit-example .

# In another terminal

curl -o test.pdf -X POST 'http://localhost:8000/api/convert' -H 'content-type: application/json' --data '{"firstname":"John","lastname":"Doe"}'

If everything works as expected you should now have a pdf named test.pdf in your current working directory. Open it and ensure that it has the text Hello John Doe!.

Now lets take a closer look at the image:

REPOSITORY            TAG                             IMAGE ID       CREATED          SIZE
slimtoolkit-example   latest                          5e0bdba7b286   47 seconds ago   1.53GB

Now that's a rather large image! Slimtoolkit to the rescue! The tricky part with this image is that we are relying on LibreOffice and some underlying dependencies so we need to ensure that Slimtoolkit is calling our API and trying to generate the pdf when it is optimizing the image. This ensures that it doesn't remove any libraries that are needed for the rendering process.

For this slimtoolkit has the concept of http-probes that it will run in the container during the optimization process. Fortunately, we can easily pass our own probes as well as payloads to go with it. In our case the custom probe looks like this:

{
    "commands": [
        {
            "protocol": "http",
            "method": "POST",
            "resource": "/api/convert",
            "body": "{\"firstname\":\"John\",\"lastname\":\"Doe\"}",
            "headers": ["content-type: application/json"]
        }
    ]
}

We can save that to a file, in this case http-probe.json and then call slim like this:

slim build --target slimtoolkit-example --http-probe-cmd-file http-probe.json

After a few minutes slim should finish and now we can check our optimized image. By default slim will just append .slim to the end of our image name.

docker images slimtoolkit-example.slim

REPOSITORY                 TAG       IMAGE ID       CREATED         SIZE
slimtoolkit-example.slim   latest    871735699565   4 minutes ago   459MB

Nice! That is about 1/3 of the size of our original image. Still fairly large, but mostly because it is relying on LibreOffice and it's dependencies. You could have slim execute multiple api calls if you needed to test functionality across multiple endpoints simply by adding to the commands array in http-probe.json.

Description
A simple example of using slimtoolkit to optimize container images
Readme 36 KiB
Languages
Dockerfile 52.7%
JavaScript 47.3%