1
0
mirror of https://github.com/openshift/source-to-image.git synced 2026-02-05 12:44:54 +01:00
Files
source-to-image/README.md

238 lines
13 KiB
Markdown
Raw Normal View History

2016-05-27 22:40:55 -04:00
# Source-To-Image (S2I)
## Overview
[![Go Report Card](https://goreportcard.com/badge/github.com/openshift/source-to-image)](https://goreportcard.com/report/github.com/openshift/source-to-image)
[![GoDoc](https://godoc.org/github.com/openshift/source-to-image?status.png)](https://godoc.org/github.com/openshift/source-to-image)
[![Travis](https://travis-ci.org/openshift/source-to-image.svg?branch=master)](https://travis-ci.org/openshift/source-to-image)
[![License](https://img.shields.io/github/license/openshift/source-to-image.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
2014-01-28 11:32:40 -08:00
Source-to-Image (S2I) is a toolkit and workflow for building reproducible Docker images from source code. S2I produces
ready-to-run images by injecting source code into a Docker container and letting the container prepare that source code for execution. By creating self-assembling **builder images**, you can version and control your build environments exactly like you use Docker images to version your runtime environments.
2016-05-27 22:40:55 -04:00
2016-07-19 18:18:06 -04:00
For a deep dive on S2I you can view [this presentation](https://www.youtube.com/watch?v=flI6zx9wH6M).
### How Source-to-Image works
2016-05-27 22:40:55 -04:00
For a dynamic language like Ruby, the build-time and run-time environments are the same. Starting with a **builder image** that describes this environment - with Ruby, Bundler, Rake, Apache, GCC, and other packages needed to set up and run a Ruby application installed - source-to-image performs the following steps:
1. Start a container from the builder image with the application source injected into a known directory
1. The container process transforms that source code into the appropriate runnable setup - in this case, by installing dependencies with Bundler and moving the source code into a directory where Apache has been preconfigured to look for the Ruby `config.ru` file.
2016-05-27 22:40:55 -04:00
1. Commit the new container and set the image entrypoint to be a script (provided by the builder image) that will start Apache to host the Ruby application.
2014-05-16 14:13:18 -04:00
For compiled languages like C, C++, Go, or Java, the dependencies necessary for compilation might dramatically outweigh the size of the actual runtime artifacts. To keep runtime images slim, S2I enables a multiple-step build processes, where a binary artifact such as an executable or Java WAR file is created in the first builder image, extracted, and injected into a second runtime image that simply places the executable in the correct location for execution.
2014-10-10 11:00:59 -04:00
For example, to create a reproducible build pipeline for Tomcat (the popular Java webserver) and Maven:
2014-10-10 11:00:59 -04:00
1. Create a builder image containing OpenJDK and Tomcat that expects to have a WAR file injected
2. Create a second image that layers on top of the first image Maven and any other standard dependencies, and expects to have a Maven project injected
3. Invoke source-to-image using the Java application source and the Maven image to create the desired application WAR
4. Invoke source-to-image a second time using the WAR file from the previous step and the initial Tomcat image to create the runtime image
2016-05-27 22:40:55 -04:00
By placing our build logic inside of images, and by combining the images into multiple steps, we can keep our runtime environment close to our build environment (same JDK, same Tomcat JARs) without requiring build tools to be deployed to production.
2016-06-29 15:32:05 +02:00
Learn more about [building your own images](#getting-started) and [how to use a non-builder image for the final application image](https://github.com/openshift/source-to-image/blob/master/docs/runtime_image.md).
2016-05-27 22:40:55 -04:00
## Goals
2016-05-27 22:40:55 -04:00
### Reproducibility
Allow build environments to be tightly versioned by encapsulating them within a Docker image and defining a simple interface (injected source code) for callers. Reproducible builds are a key requirement to enabling security updates and continuous integration in containerized infrastructure, and builder images help ensure repeatability as well as the ability to swap runtimes.
2016-05-27 22:40:55 -04:00
### Flexibility
Any existing build system that can run on Linux can be run inside of a container, and each individual builder can also be part of a larger pipeline. In addition, the scripts that process the application source code can be injected into the builder image, allowing authors to adapt existing images to enable source handling.
2016-05-27 22:40:55 -04:00
### Speed
Instead of building multiple layers in a single Dockerfile, S2I encourages authors to represent an application in a single image layer. This saves time during creation and deployment, and allows for better control over the output of the final image.
2016-05-27 22:40:55 -04:00
### Security
Dockerfiles are run without many of the normal operational controls of containers, usually running as root and having access to the container network. S2I can be used to control what permissions and privileges are available to the builder image since the build is launched in a single container. In concert with platforms like OpenShift, source-to-image can enable admins to tightly control what privileges developers have at build time.
2014-10-10 11:00:59 -04:00
2016-05-27 22:40:55 -04:00
## Anatomy of a builder image
2014-10-10 11:00:59 -04:00
2015-07-24 18:39:35 +02:00
Creating builder images is easy. `s2i` looks for you to supply the following scripts to use with an
image:
2014-10-10 11:00:59 -04:00
2015-02-12 17:03:31 -05:00
1. `assemble` - builds and/or deploys the source
1. `run`- runs the assembled artifacts
1. `save-artifacts` (optional) - captures the artifacts from a previous build into the next incremental build
1. `usage` (optional) - displays builder image usage information
2014-10-10 11:00:59 -04:00
2015-07-24 18:39:35 +02:00
Additionally for the best user experience and optimized `s2i` operation we suggest images
2015-06-26 17:00:30 -05:00
to have `/bin/sh` and `tar` commands available.
2014-10-10 11:00:59 -04:00
2016-04-25 12:46:48 -04:00
See a practical tutorial on how to create a builder image [here](examples/README.md) and read [this](https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md) for a detailed description of the requirements and scripts along with examples of builder images.
2016-05-27 22:40:55 -04:00
## Build workflow
2016-04-25 12:46:48 -04:00
The `s2i build` workflow is:
1. `s2i` creates a container based on the build image and passes it a tar file that contains:
1. The application source in `src`, excluding any files selected by `.s2iignore`
1. The build artifacts in `artifacts` (if applicable - see [incremental builds](#incremental-builds))
1. `s2i` sets the environment variables from `.s2i/environment` (optional)
1. `s2i` starts the container and runs its `assemble` script
1. `s2i` waits for the container to finish
1. `s2i` commits the container, setting the CMD for the output image to be the `run` script and tagging the image with the name provided.
Filtering the contents of the source tree is possible if the user supplies a
`.s2iignore` file in the root directory of the source repository, where `.s2iignore` contains regular
expressions that capture the set of files and directories you want filtered from the image s2i produces.
Specifically:
1. Specify one rule per line, with each line terminating in `\n`.
1. Filepaths are appended to the absolute path of the root of the source tree (either the local directory supplied, or the target destination of the clone of the remote source repository s2i creates).
1. Wildcards and globbing (file name expansion) leverage Go's `filepath.Match` and `filepath.Glob` functions.
1. Search is not recursive. Subdirectory paths must be specified (though wildcards and regular expressions can be used in the subdirectory specifications).
1. If the first character is the `#` character, the line is treated as a comment.
1. If the first character is the `!`, the rule is an exception rule, and can undo candidates selected for filtering by prior rules (but only prior rules).
Here are some examples to help illustrate:
With specifying subdirectories, the `*/temp*` rule prevents the filtering of any files starting with `temp` that are in any subdirectory that is immediately (or one level) below the root directory.
And the `*/*/temp*` rule prevents the filtering of any files starting with `temp` that are in any subdirectory that is two levels below the root directory.
Next, to illustrate exception rules, first consider the following example snippet of a `.s2iignore` file:
```
*.md
!README.md
```
With this exception rule example, README.md will not be filtered, and remain in the image s2i produces. However, with this snippet:
```
!README.md
*.md
```
`README.md`, if filtered by any prior rules, but then put back in by `!README.md`, would be filtered, and not part of the resulting image s2i produces. Since `*.md` follows `!README.md`, `*.md` takes precedence.
2015-07-24 18:39:35 +02:00
Users can also set extra environment variables in the application source code.
2015-06-26 17:00:30 -05:00
They are passed to the build, and the `assemble` script consumes them. All
2015-02-24 16:00:43 +01:00
environment variables are also present in the output application image. These
2016-02-03 09:25:25 +01:00
variables are defined in the `.s2i/environment` file inside the application sources.
2015-02-24 16:00:43 +01:00
The format of this file is a simple key-value, for example:
```
FOO=bar
```
In this case, the value of `FOO` environment variable will be set to `bar`.
2016-05-27 22:40:55 -04:00
## Using ONBUILD images
2015-02-19 15:13:45 +01:00
In case you want to use one of the official Docker language stack images for
2015-07-24 18:39:35 +02:00
your build you don't have do anything extra. S2I is capable of recognizing the
Docker image with [ONBUILD](https://docs.docker.com/reference/builder/#onbuild) instructions and choosing the OnBuild strategy. This
2015-02-19 15:13:45 +01:00
strategy will trigger all ONBUILD instructions and execute the assemble script
(if it exists) as the last instruction.
Since the ONBUILD images usually don't provide any entrypoint, in order to use
2015-06-26 17:00:30 -05:00
this build strategy you will have to provide one. You can either include the 'run',
2015-02-19 15:13:45 +01:00
'start' or 'execute' script in your application source root folder or you can
2015-07-24 18:39:35 +02:00
specify a valid S2I script URL and the 'run' script will be fetched and set as
2015-02-19 15:13:45 +01:00
an entrypoint in that case.
2016-05-27 22:40:55 -04:00
### Incremental builds
2014-10-10 11:00:59 -04:00
2015-07-24 18:39:35 +02:00
`s2i` automatically detects:
2014-10-10 11:00:59 -04:00
* Whether a builder image is compatible with incremental building
* Whether a previous image exists, with the same name as the output name for this build
2014-10-10 11:00:59 -04:00
2015-04-15 12:51:06 -04:00
If a `save-artifacts` script exists, a prior image already exists, and the `--incremental=true` option is used, the workflow is as follows:
2014-10-10 11:00:59 -04:00
2015-07-24 18:39:35 +02:00
1. `s2i` creates a new Docker container from the prior build image
1. `s2i` runs `save-artifacts` in this container - this script is responsible for streaming out
2014-10-10 11:00:59 -04:00
a tar of the artifacts to stdout
2015-07-24 18:39:35 +02:00
1. `s2i` builds the new output image:
2014-11-06 09:47:59 -05:00
1. The artifacts from the previous build will be in the `artifacts` directory of the tar
2014-10-10 11:00:59 -04:00
passed to the build
1. The build image's `assemble` script is responsible for detecting and using the build
artifacts
**NOTE**: The `save-artifacts` script is responsible for streaming out dependencies in a tar file.
2014-10-10 11:00:59 -04:00
2016-05-27 22:40:55 -04:00
## Dependencies
2014-10-10 11:00:59 -04:00
1. [Docker](http://www.docker.io) >= 1.6
1. [Go](http://golang.org/) >= 1.4
2015-12-07 11:28:44 -05:00
1. (optional) [Git](https://git-scm.com/)
2014-10-10 11:00:59 -04:00
2016-05-27 22:40:55 -04:00
## Installation
2014-10-10 11:00:59 -04:00
##### For Linux/Mac
Go to the [releases](https://github.com/openshift/source-to-image/releases/latest) page and download the correct distribution for your machine. If on a Mac, use the darwin-amd64 link. If on Linux, choose either the linux-386 or the linux-amd64 links for 32 and 64-bit, respectively.
Unpack the downloaded tar with
```$ tar -xvf release.tar.gz```.
You should now see an executable called s2i. Either add the location of s2i to your PATH environment variable, or move it to a pre-existing directory in your PATH.
For example,
```# cp /path/to/s2i /usr/local/bin```
will work with most setups.
##### For Windows
Download the latest [64-bit Windows release](https://github.com/openshift/source-to-image/releases/latest).
Extract the zip file through a file browser. Add the extracted directory to your PATH. You can now use
s2i from the command line.
##### From source
2015-06-26 17:00:30 -05:00
Assuming Go and Docker are installed and configured, execute the following commands:
2014-10-10 11:00:59 -04:00
```
$ go get github.com/openshift/source-to-image
$ cd ${GOPATH}/src/github.com/openshift/source-to-image
2015-11-11 23:05:49 -05:00
$ export PATH=$PATH:${GOPATH}/src/github.com/openshift/source-to-image/_output/local/bin/linux/amd64/
$ hack/build-go.sh
```
2014-10-10 11:00:59 -04:00
2016-05-27 22:40:55 -04:00
## Security
2015-05-15 11:14:05 +02:00
2015-07-24 18:39:35 +02:00
Since the `s2i` command uses the Docker client library, it has to run in the same
security context as the `docker` command. For some systems, it is enough to add
yourself into the 'docker' group to be able to work with Docker as 'non-root'.
In the latest versions of Fedora/RHEL, it is recommended to use the `sudo` command
2015-06-26 17:00:30 -05:00
as this way is more auditable and secure.
2015-05-15 11:14:05 +02:00
2015-06-26 17:00:30 -05:00
If you are using the `sudo docker` command already, then you will have to also use
2015-07-24 18:39:35 +02:00
`sudo s2i` to give S2I permission to work with Docker directly.
2014-10-10 11:00:59 -04:00
2016-02-02 16:30:01 -05:00
Be aware that being a member of the 'docker' group effectively grants root access,
as described [here](https://github.com/docker/docker/issues/9976).
2016-05-27 22:40:55 -04:00
## Getting Started
2014-10-10 11:00:59 -04:00
2015-07-24 18:39:35 +02:00
You can start using `s2i` right away (see [releases](https://github.com/openshift/source-to-image/releases))
with the following test sources and publicly available images:
2014-10-10 11:00:59 -04:00
```
2015-07-24 18:39:35 +02:00
$ s2i build git://github.com/pmorie/simple-ruby openshift/ruby-20-centos7 test-ruby-app
2015-11-11 23:05:49 -05:00
$ docker run --rm -i -p :8080 -t test-ruby-app
```
2014-10-10 11:00:59 -04:00
```
$ s2i build git://github.com/bparees/openshift-jee-sample openshift/wildfly-100-centos7 test-jee-app
2015-02-09 11:18:36 -05:00
$ docker run --rm -i -p :8080 -t test-jee-app
2014-10-10 11:00:59 -04:00
```
2015-07-24 18:39:35 +02:00
Interested in more advanced `s2i` usage? See [here](https://github.com/openshift/source-to-image/blob/master/docs/cli.md)
2015-06-26 17:00:30 -05:00
for detailed descriptions of the different CLI commands with examples.
2015-07-24 18:39:35 +02:00
Running into some issues and need some advice debugging? Peruse [here](https://github.com/openshift/source-to-image/blob/master/docs/debugging-s2i.md) for some tips.