commit 5c1d0f5e7786375e8c2e9624bb77ab301c2c89de Author: Nicolas Massé Date: Tue May 16 15:28:47 2017 +0100 initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6d284ce --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ + +# golang-centos7 +FROM openshift/base-centos7 + +# Yes, it's me +MAINTAINER Nicolas Masse + +# The Go version you would like to use +ENV GOLANG_VERSION 1.8.1 + +LABEL io.k8s.description="Platform for building golang applications. Based on GO ${GOLANG_VERSION}." \ + io.k8s.display-name="golang builder v${GOLANG_VERSION}" \ + io.openshift.expose-services="8080:http" \ + io.openshift.tags="builder,golang" + +# Install go +RUN curl https://storage.googleapis.com/golang/go${GOLANG_VERSION}.linux-amd64.tar.gz -o /tmp/go.tar.gz && tar -C /usr/local -zxf /tmp/go.tar.gz + +# Copy the S2I scripts to /usr/libexec/s2i, since openshift/base-centos7 image +# sets io.openshift.s2i.scripts-url label that way, or update that label +COPY ./s2i/bin/ /usr/libexec/s2i + +# Drop the root user and make the content of /opt/app-root owned by user 1001 +RUN chown -R 1001:1001 /opt/app-root + +# This default user is created in the openshift/base-centos7 image +USER 1001 + +# Set the default port for applications built using this image +EXPOSE 8080 + +# Set the default CMD for the image +CMD ["/usr/libexec/s2i/usage"] diff --git a/README.md b/README.md new file mode 100755 index 0000000..170f2bc --- /dev/null +++ b/README.md @@ -0,0 +1,82 @@ + +# Creating a basic S2I builder image + +## Getting started + +### Files and Directories +| File | Required? | Description | +|------------------------|-----------|--------------------------------------------------------------| +| Dockerfile | Yes | Defines the base builder image | +| s2i/bin/assemble | Yes | Script that builds the application | +| s2i/bin/usage | No | Script that prints the usage of the builder | +| s2i/bin/run | Yes | Script that runs the application | +| s2i/bin/save-artifacts | No | Script for incremental builds that saves the built artifacts | +| test/run | No | Test script for the builder image | +| test/test-app | Yes | Test application source code | + +#### Dockerfile +Create a *Dockerfile* that installs all of the necessary tools and libraries that are needed to build and run our application. This file will also handle copying the s2i scripts into the created image. + +#### S2I scripts + +##### assemble +Create an *assemble* script that will build our application, e.g.: +- build python modules +- bundle install ruby gems +- setup application specific configuration + +The script can also specify a way to restore any saved artifacts from the previous image. + +##### run +Create a *run* script that will start the application. + +##### save-artifacts (optional) +Create a *save-artifacts* script which allows a new build to reuse content from a previous version of the application image. + +##### usage (optional) +Create a *usage* script that will print out instructions on how to use the image. + +##### Make the scripts executable +Make sure that all of the scripts are executable by running *chmod +x s2i/bin/** + +#### Create the builder image +The following command will create a builder image named golang-centos7 based on the Dockerfile that was created previously. +``` +docker build -t golang-centos7 . +``` +The builder image can also be created by using the *make* command since a *Makefile* is included. + +Once the image has finished building, the command *s2i usage golang-centos7* will print out the help info that was defined in the *usage* script. + +#### Testing the builder image +The builder image can be tested using the following commands: +``` +docker build -t golang-centos7-candidate . +IMAGE_NAME=golang-centos7-candidate test/run +``` +The builder image can also be tested by using the *make test* command since a *Makefile* is included. + +#### Creating the application image +The application image combines the builder image with your applications source code, which is served using whatever application is installed via the *Dockerfile*, compiled using the *assemble* script, and run using the *run* script. +The following command will create the application image: +``` +s2i build test/test-app golang-centos7 golang-centos7-app +---> Building and installing application from source... +``` +Using the logic defined in the *assemble* script, s2i will now create an application image using the builder image as a base and including the source code from the test/test-app directory. + +#### Running the application image +Running the application image is as simple as invoking the docker run command: +``` +docker run -d -p 8080:8080 golang-centos7-app +``` +The application, which consists of a simple static web page, should now be accessible at [http://localhost:8080](http://localhost:8080). + +#### Using the saved artifacts script +Rebuilding the application using the saved artifacts can be accomplished using the following command: +``` +s2i build --incremental=true test/test-app nginx-centos7 nginx-app +---> Restoring build artifacts... +---> Building and installing application from source... +``` +This will run the *save-artifacts* script which includes the custom code to backup the currently running application source, rebuild the application image, and then re-deploy the previously saved source using the *assemble* script. diff --git a/s2i/bin/assemble b/s2i/bin/assemble new file mode 100755 index 0000000..f797c8e --- /dev/null +++ b/s2i/bin/assemble @@ -0,0 +1,26 @@ +#!/bin/bash -e +# +# S2I assemble script for the 'golang-centos7' image. +# The 'assemble' script builds your application source so that it is ready to run. +# +# For more information refer to the documentation: +# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md +# + +# If the 'golang-centos7' assemble script is executed with the '-h' flag, print the usage. +if [[ "$1" == "-h" ]]; then + exec /usr/libexec/s2i/usage +fi + +# Restore artifacts from the previous build (if they exist). +# +if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then + echo "---> Restoring build artifacts..." + mv /tmp/artifacts/. ./ +fi + +echo "---> Installing application source..." +cp -Rf /tmp/src/. ./ + +echo "---> Building application from source..." +go build -o main diff --git a/s2i/bin/run b/s2i/bin/run new file mode 100755 index 0000000..469f010 --- /dev/null +++ b/s2i/bin/run @@ -0,0 +1,10 @@ +#!/bin/bash -e +# +# S2I run script for the 'golang-centos7' image. +# The run script executes the server that runs your application. +# +# For more information see the documentation: +# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md +# + +exec main diff --git a/s2i/bin/usage b/s2i/bin/usage new file mode 100755 index 0000000..7dc5235 --- /dev/null +++ b/s2i/bin/usage @@ -0,0 +1,12 @@ +#!/bin/bash -e +cat < golang-centos7 + +You can then run the resulting image via: +docker run +EOF