This is a cache of https://docs.okd.io/4.9/openshift_images/using_images/customizing-s2i-images.html. It is a snapshot of the page at 2024-11-24T23:31:14.919+0000.
Customizing source-to-image images - Using images | Images | OKD 4.9
×

Source-to-image (s2i) builder images include assemble and run scripts, but the default behavior of those scripts is not suitable for all users. You can customize the behavior of an s2i builder that includes default scripts.

Invoking scripts embedded in an image

Builder images provide their own version of the source-to-image (s2i) scripts that cover the most common use-cases. If these scripts do not fulfill your needs, s2i provides a way of overriding them by adding custom ones in the .s2i/bin directory. However, by doing this, you are completely replacing the standard scripts. In some cases, replacing the scripts is acceptable, but, in other scenarios, you can run a few commands before or after the scripts while retaining the logic of the script provided in the image. To reuse the standard scripts, you can create a wrapper script that runs custom logic and delegates further work to the default scripts in the image.

Procedure
  1. Look at the value of the io.openshift.s2i.scripts-url label to determine the location of the scripts inside of the builder image:

    $ podman inspect --format='{{ index .Config.Labels "io.openshift.s2i.scripts-url" }}' wildfly/wildfly-centos7
    Example output
    image:///usr/libexec/s2i

    You inspected the wildfly/wildfly-centos7 builder image and found out that the scripts are in the /usr/libexec/s2i directory.

  2. Create a script that includes an invocation of one of the standard scripts wrapped in other commands:

    .s2i/bin/assemble script
    #!/bin/bash
    echo "Before assembling"
    
    /usr/libexec/s2i/assemble
    rc=$?
    
    if [ $rc -eq 0 ]; then
        echo "After successful assembling"
    else
        echo "After failed assembling"
    fi
    
    exit $rc

    This example shows a custom assemble script that prints the message, runs the standard assemble script from the image, and prints another message depending on the exit code of the assemble script.

    When wrapping the run script, you must use exec for invoking it to ensure signals are handled properly. The use of exec also precludes the ability to run additional commands after invoking the default image run script.

    .s2i/bin/run script
    #!/bin/bash
    echo "Before running application"
    exec /usr/libexec/s2i/run