In this HOWTO, we will create an s2i builder image for spring boot.
curl -L https://github.com/openshift/source-to-image/releases/download/v1.1.10/source-to-image-v1.1.10-27f0729d-linux-amd64.tar.gz -o s2i.tar.gz
cd $HOME
git clone https://github.com/openshift/source-to-image.git
mkdir $HOME/tmp
cp -r $HOME/source-to-image/examples/nginx-centos7/ $HOME/tmp/my-spring-boot-s2i
Change directory to $HOME/tmp/my-spring-boot-s2i
Dockerfile
Makefile
README.md
s2i
test
# nginx-centos7
# Here you can use whatever base image is relevant for your application.
FROM centos:centos7
# Here you can specify the maintainer for the image that you're building
LABEL maintainer="Your Name <your.name@example.com>"
# Export an environment variable that provides information about the application version.
# Replace this with the version for your application.
ENV NGINX_VERSION=1.6.3
# Set the labels that are used for OpenShift to describe the builder image.
LABEL io.k8s.description="Nginx Webserver" \
io.k8s.display-name="Nginx 1.6.3" \
io.openshift.expose-services="8080:http" \
io.openshift.tags="builder,webserver,html,nginx" \
# this label tells s2i where to find its mandatory scripts
# (run, assemble, save-artifacts)
io.openshift.s2i.scripts-url="image:///usr/libexec/s2i"
# Install the nginx web server package and clean the yum cache
#RUN yum install -y epel-release && \
# yum install -y --setopt=tsflags=nodocs nginx && \
# yum clean all
RUN yum install -y java-1.8.0-openjdk-devel maven && yum clean all
# Copy the S2I scripts to /usr/libexec/s2i since we set the label that way
COPY ./s2i/bin/ /usr/libexec/s2i
#RUN chown -R 1001:1001 /usr/share/nginx
#RUN chown -R 1001:1001 /var/log/nginx
#RUN chown -R 1001:1001 /var/lib/nginx
#RUN touch /run/nginx.pid
#RUN chown -R 1001:1001 /run/nginx.pid
#RUN chown -R 1001:1001 /etc/nginx
USER 1001
# Set the default port for applications built using this image
EXPOSE 8080
# Modify the usage script in your application dir to inform the user how to run
# this image.
CMD ["/usr/libexec/s2i/usage"]
Edit the Makefile. Change the value of IMAGE_NAME to my-spring-boot-s2i
Inside the s2i directory are the following files
s2i/bin
s2i/bin/save-artifacts
s2i/bin/assemble
s2i/bin/run
s2i/bin/usage
#!/bin/bash -e
#
# S2I assemble script for the 'nginx-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 [[ "$1" == "-h" ]]; then
# If the 'nginx-centos7' assemble script is executed with '-h' flag,
# print the usage.
exec /usr/libexec/s2i/usage
fi
mvn -Dmaven.repo.local=/tmp/.m2 package
#!/bin/bash -e
# 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
#
# We will turn off daemonizing for the nginx process so that the container
# doesn't exit after the process runs.
#exec /usr/sbin/nginx -g "daemon off;"
cd /tmp/src/target
exec java -jar gs-serving-web-content-0.1.0.jar
make
my-spring-boot-s2i latest 6f671258dc9d About an hour ago 429 MB
13. Tag the new image with your docker hub repository name:
docker tag my-spring-boot-s2i
In my case, I tag is using:
docker tag my-spring-boot-s2i bcorpusjr/my-spring-boot-s2i
14. Login to docker hub using your username and password
docker login
15. Push the image to docke hub
docker push
In my case I execute the command:
docker push bcorpusjr/my-spring-boot-s2i
## Testing the image
1. Create a new project in openshift
oc new-project spring
2. Create a new spring boot app. We will use the following project https://github.com/corpbob/hello-spring-boot.git
oc new-app
In my case, I do
oc new-app bcorpusjr/my-spring-boot-s2i~https://github.com/corpbob/hello-spring-boot.git
3. Login to the OpenShift web console->Projects->spring->Builds->Builds
You should see something like:
<img src="/assets/img/s2i_howto_build.png" />
4. Click on the Logs to see the following:
<img src="/assets/img/s2i_howto_build_logs.png"/>
5. After the build, create a route for the application
oc expose svc hello-spring-boot ```