2015-07-24 18:39:35 +02:00
# s2i builder image requirements
2014-12-09 16:03:01 +01:00
2015-07-24 18:39:35 +02:00
The main advantage of using s2i for building reproducible docker images is ease
2015-06-05 21:28:34 +01:00
of use for developers. To meet that criteria you, as a builder image author,
2016-02-03 09:26:25 +01:00
should be aware of the two basic requirements for the best possible s2i
2015-06-05 21:28:34 +01:00
performance. These are:
2014-12-09 16:03:01 +01:00
* [required image contents ](#required-image-contents )
2015-07-24 18:39:35 +02:00
* [s2i scripts ](#s2i-scripts )
2014-12-09 16:03:01 +01:00
# Required image contents
2015-06-05 21:28:34 +01:00
The build process consists of three fundamental elements which are combined into the
2015-07-24 18:39:35 +02:00
final docker image. These are: source code, s2i scripts, and the builder image. During the
build process s2i must place sources and scripts inside that builder image. To do
so s2i creates a tar file containing the two and then streams that file into the
builder image. Before executing the `assemble` script, s2i untars that file and places
2015-07-06 14:09:30 +02:00
its contents into the destination specified with either the `--destination` flag or the value of
the `io.openshift.s2i.destination` label set in the builder image (the default destination is `/tmp` ).
2015-07-24 18:39:35 +02:00
If your image does not have either `tar` or `/bin/sh` the s2i build will perform an additional
2015-06-05 21:28:34 +01:00
docker build to place the source code and scripts into an appropriate image and then run
2015-07-24 18:39:35 +02:00
the normal s2i build.
2015-06-05 21:28:34 +01:00
The following diagram illustrates the build workflow:
2014-12-09 16:03:01 +01:00
2015-07-24 18:39:35 +02:00

2014-12-09 16:03:01 +01:00
2015-06-05 21:28:34 +01:00
\* Run build's responsibility is to untar the sources, scripts and (optionally) artifacts
and invoke the `assemble` script. If this is the second run after any previous runs with
`tar` /`/bin/sh` errors, it will only run the `assemble` script, since both the source and
scripts are already present.
2014-12-09 16:03:01 +01:00
2015-07-24 18:39:35 +02:00
# s2i scripts
2014-12-09 16:03:01 +01:00
2015-07-24 18:39:35 +02:00
`s2i` expects you (the builder image author) to supply the following scripts:
2014-12-09 16:03:01 +01:00
* required:
* [assemble ](#assemble )
* [run ](#run )
* optional:
* [save-artifacts ](#save-artifacts )
* [usage ](#usage )
2017-05-23 20:44:44 -07:00
* [test/run ](#testrun )
2014-12-09 16:03:01 +01:00
2015-06-02 17:36:30 +02:00
All of the scripts can be written in any programming language, as long as the scripts
2015-06-05 21:28:34 +01:00
are executable inside the builder image. The build searches the following locations for
these scripts in the following order:
2015-06-02 17:36:30 +02:00
1. A script found at the `--scripts-url` URL
2016-02-03 09:26:25 +01:00
1. A script found in the application source `.s2i/bin` directory
2015-06-26 16:00:57 +02:00
1. A script found at the default image URL (`io.openshift.s2i.scripts-url` label)
2015-06-02 17:36:30 +02:00
2015-06-26 16:00:57 +02:00
Both the `io.openshift.s2i.scripts-url` label specified in the image and `--scripts-url` flag
2015-06-26 09:44:52 -05:00
can be supplied in any of the following forms to indicate where the scripts are located:
2015-06-02 17:36:30 +02:00
2015-06-05 21:28:34 +01:00
* `image://path_to_scripts_dir` - absolute path inside the image
* `file://path_to_scripts_dir` - relative or absolute path on the host machine
* `http(s)://path_to_scripts_dir` - URL to a directory
2015-06-02 17:36:30 +02:00
2015-06-05 21:28:34 +01:00
**NOTE**: In the case where the scripts are already placed inside the image (ie when
using `--scripts-url` flag or the `io.openshift.s2i.scripts-url` with the format
2015-07-06 14:09:30 +02:00
`image:///path/in/image` ), then the `--destination` flag or the `io.openshift.s2i.destination`
2015-06-05 21:28:34 +01:00
label applies only to sources and artifacts.
2014-12-09 16:03:01 +01:00
## assemble
2015-06-25 16:39:56 -05:00
The `assemble` script is responsible for building the application artifacts from source
2015-06-05 21:28:34 +01:00
and placing them into the appropriate directories inside the image. The workflow for the
`assemble` script is:
2014-12-09 16:03:01 +01:00
2015-06-05 21:28:34 +01:00
1. Restore build artifacts (in case you want to support incremental builds (if using this,
make sure you define [save-artifacts ](#save-artifacts )) as well.
1. Place the application source code in the appropriate location.
1. Build any application artifacts.
2014-12-09 16:03:01 +01:00
1. Install the artifacts into locations appropriate for running.
2017-08-15 22:28:09 -03:00
In the case you need to assemble the Image using a different user than the runtime user defined
in ``USER` ` directive of Dockerfile, you can achive this by the following ways:
1. use the `--assemble-user` in cmd line
1. use the label `io.openshift.s2i.assemble-user`
2014-12-09 16:03:01 +01:00
#### Example `assemble` script:
**NOTE**: All the examples are written in [Bash ](http://www.gnu.org/software/bash/ )
2017-12-06 19:06:36 -05:00
and it is assumed that the tar contents unpack into the `/tmp` directory.
2014-12-09 16:03:01 +01:00
```
#!/bin/bash
# restore build artifacts
2017-12-06 19:06:36 -05:00
if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then
mv /tmp/artifacts/* $HOME/.
2014-12-09 16:03:01 +01:00
fi
# move the application source
2016-02-03 09:26:25 +01:00
mv /tmp/s2i/src $HOME/src
2014-12-09 16:03:01 +01:00
# build application artifacts
pushd ${HOME}
make all
# install the artifacts
make install
popd
```
## run
The `run` script is responsible for executing your application.
#### Example `run` script:
```
#!/bin/bash
# run the application
/opt/application/run.sh
```
## save-artifacts
2016-12-05 12:50:38 -05:00
The `save-artifacts` script is responsible for gathering all the dependencies into a tar file and streaming it to the standard output (eg. for Ruby - gems installed by Bundler, for Java - `.m2` contents, etc.). The existence of this can speed up the following build processes. Note: it is critical that the `save-artifacts` script output only include the tar stream output and nothing else. This is handled by redirecting output to /dev/null in the sample script below.
2014-12-09 16:03:01 +01:00
#### Example `save-artifacts` script:
```
#!/bin/bash
2016-11-15 12:37:57 -05:00
# Besides the tar command, all other output to standard out must
# be surpressed. Otherwise, the tar stream will be corrupted.
pushd ${HOME} >/dev/null
2014-12-09 16:03:01 +01:00
if [ -d deps ]; then
# all deps contents to tar stream
tar cf - deps
fi
2016-11-15 12:37:57 -05:00
popd >/dev/null
2014-12-09 16:03:01 +01:00
```
## usage
2015-06-05 21:28:34 +01:00
The `usage` script is for you (as the builder image author) to inform the user
how to use your image.
2014-12-09 16:03:01 +01:00
#### Example `usage` script:
```
#!/bin/bash
# inform the user how to use the image
cat <<EOF
2015-07-24 18:39:35 +02:00
This is a S2I sample builder image, to use it, install
2014-12-09 16:03:01 +01:00
https://github.com/openshift/source-to-image
EOF
```
2015-01-28 10:43:18 +01:00
## test/run
2015-06-05 21:28:34 +01:00
The `test/run` script is for you (as the builder image author) to create a simple
process to check whether the image is working correctly. The workflow of that process
should be the following:
2015-01-28 10:43:18 +01:00
1. Build the image.
2015-06-05 21:28:34 +01:00
1. Run the image to verify the `usage` script.
2015-07-24 18:39:35 +02:00
1. Run the `s2i build` to verify `assemble` script.
1. (Optional) Run the `s2i build` once more to verify `save-artifacts` script and
2015-01-28 10:43:18 +01:00
`assemble` 's restore artifacts functionality.
1. Run the image to verify the test application is working.
2015-06-05 21:28:34 +01:00
**NOTE** The suggested place to put your test application built by your
2015-01-28 10:43:18 +01:00
`test/run` script is `test/test-app` in your image repository, see
2015-07-24 18:39:35 +02:00
[s2i create ](https://github.com/openshift/source-to-image/blob/master/docs/cli.md#s2i-create ).
2016-04-25 12:46:48 -04:00
# Additional steps
## OpenShift support
2022-07-08 16:38:12 -04:00
If you are intending to use this image with [OpenShift ](https://github.com/openshift/origin ), review and follow the OpenShift [image creation guidelines ](https://docs.okd.io/latest/openshift_images/create-images.html ).