Source-To-Image is a deployment tool created by OpenShift that enables developers to build and deploy Docker images with just one command. The tool pulls down source from a repository, compiles that source, puts it in a directory on the container of your choosing, and then starts the server. This tutorial takes you through setting up the Source-To-Image tool and using it to build and deploy WebSphere Liberty into your local Docker Registry.
Before you start
- Install Docker: On Windows and Mac this is most easily achieved by installing the Docker Toolbox.
- Install a compatible OS: Fedora 21, CentOS 7.1, or RHEL 7.1 or later with the
Minimalinstallation option, or RHEL Atomic Host 7.1.6 or later OpenShift prerequisites.
- Install the Go Language GoLang.
- Install OpenShift V3 with the correct configuration if auto deployment is required (Instructions here.).
- Have some source code on GitHub configured so that the code can be compiled using Maven.
Installing Source-To-Image is relatively simple. If you would like the Source-To-Image tool to automatically deploy to OpenShift then a running instance of the OpenShift server is required. Instructions to achieve that can be found here!
Now to get started:
- Run the following command to get the Source-To-Image binaries:
go get github.com/openshift/source-to-image
- Navigate to the Source-To-Image application:
- Set the environment variable:
- Finish the installation:
Building and deploying Liberty into your Docker Registry
- To start using the Source-To-Image tool, clone the ci.docker.openshift git repository.
This will provide all the required files, including the scripts described below, the Dockerfile, and a test script:
assemble – required
The assemble script contains information regarding how to build you application from source and places were the build application will be deployed.
run – required
The run script contains configuration information regarding what process to run to start your application.
usage – optional
The usage script is simply a description or a set of instruction on how to use the image and what it does.
Build a new version of Liberty to include the scripts we talked about above and set some LABELS in order for OpenShift to set certain things such as what port to expose. The Dockerfile is included in the GitHub repository with the scripts above but here it is with an explanation of what it does:
FROM websphere-liberty # Install Maven RUN sudo apt-get update && sudo apt-get -y install maven && mkdir -p /opt/s2i/destination && apt-get -y install openjdk-7-jdk LABEL io.k8s.description="Used for building and running JEE applications on WebSphere Liberty" io.k8s.display-name="WebSphere-Liberty" io.openshift.expose-services="9080:http" io.openshift.tags="builder,liberty,websphere-liberty" io.openshift.s2i.destination="/opt/s2i/destination" io.openshift.s2i.scripts-url="image:///opt/s2i/" # Copy Scripts to build s2i COPY assemble /opt/s2i/ COPY run /opt/s2i/ COPY usage /opt/s2i/ #CMD $STI_SCRIPTS_PATH/usage
- The RUN command installs maven, creates a directory for the source to go and installs openjdk as a jdk is required to use source to image.
- The LABEL command is needed for the Source to image scripts to do what they do. It sets stuff such as description, version of the image, port numbers and the destination of the scripts/source code.
- The COPY command copies over the scripts to the image.
Navigate to the Dockerfile/script location and build the image with the following command:
docker build –t liberty .
Once the image is built simply run the following command to get the source from your repo, compile it, and deploy it to your WebSphere Liberty server. You will need to navigate to where you downloaded the source2image binaries first or put the s2i command on the class path:
./s2i build https://github.com/WASdev/sample.ferret.git liberty tests2i
If you now run the command
docker imagesyou should see your image which is ready for deployment. To run your image simply type:
docker run –d –P tests2i
If you then do a
docker psyou can get the port number and put that into your browser to view the Liberty home page. If you then add
/ferreton to the end ( providing you used the example above), your web application should be displayed.
Congrats! You have successfully built and deployed your WebSphere Liberty image and Java application with just one command (and a few scripts). Once your base image is set up you can just re-use it over and over and just provide it with different source code.