OpenShift Source-to-Image (S2I) is a framework that makes it easy to write images that take application source code as an input and produce a new image that runs the assembled application as output. The main advantage of using S2I for building reproducible Docker images is the ease of use for developers. A Dockerfile is a recipe (or blueprint) for building Docker images.
In this tutorial, you will learn how to use OpenShift S2I feature to build a docker image from a Dockerfile hosted in github and deploy a Pod using that docker image. You will also learn how to setup the github webhook to notify OpenShift of new code push/commit events in github, such that OpenShift will auto rebuild and redeploy Pods using the latest code/Dockerfile changes in your github.
For this tutorial you will need:
- Red Hat OpenShift Cluster 4.3 or above on IBM Cloud.
It will take you around 30 minutes to complete this tutorial.
- Create a GitHub repo and host a Dockerfile
- Create Project
- Create a pod deployment using the Dockerfile from your GitHub repo
- Verify that the container process matches the command specified in the Dockerfile
- Setup GitHub Webhook
- Make changes on your GitHub repository
- Go to this GitHub repository and fork it by clicking the Fork button at the top right of the repo. You will be making changes in the Dockerfile to trigger new pod deployments at a later step in this tutorial.
The repository has a Dockerfile that contains the following lines of code.
# simple dockerfile to test OCP s2i using Dockerfile
FROM ubuntu:18.04
CMD ["/bin/bash", "-c", "sleep infinity"]
# CMD ["/bin/bash", "-c", "--", "while true; do sleep 30; done;"]
- From the Administrator perspective, go to Projects and click 'Create Project' from the top right of the page. You can name the project 's2i-project'.
- Once created you will be redirected to the Overview page of the project
- In the OpenShift web console, switch from the Administrator perspective to the Developer perspective and select to create an application 'From Dockerfile'.
- Copy the URL of your GitHub repo which should look like this
https://github.com/<GITHUB-USERNAME>/oc-docker-s2i
, make sure it includes your GitHub username. - Paste the URL of your GitHub repository in the 'Git Repo URL' text field.
- Add to context dir field
/ubuntu
which is where the Dockerfile is located. - Scroll down to Resources section and select
Deployment Config
and keep the 'create a route to the application' checked then click 'Create'. - You will be redirected to the topology view which shows the pods created. Once it successfully builds, you will notice that the circle around your application turns dark blue.
- If you click on your application, it will show the Deployment Config view where you can view details, resources and logs. What happens here is that OpenShift builds the docker image using Dockerfile from your github repo, creates a docker image, uploads the image into OpenShift’s internal image registry and creates a Pod using that docker image.
- From the Topology view, click on the
DeploymentConfig
. - Click on
Pods
to list the running Pod - Select the running pod to enter the pod details view.
- Click on
Terminal
, which brings up a Terminal (Shell) inside your running Ubuntu Container. - Verify that the pod is running
sleep infinity
process as specified in the Dockerfile by typing in the terminal the following command as shown in the screenshot.
ps aux | grep sleep
Github Webhooks allow external services to be notified when certain events happen.
For auto-deploy (updates to the github Dockerfile auto deploys new Pods) to work, you will need to configure our github repo with the webhook that OpenShift provides as part of the BuildConfig
. This is best achieved using the GUI.
- From menu to the left, go to 'Builds' then select the build config of your application.
- You will be redirected to the details view of the build config. Scroll down till you get to the webhooks section and copy the URL with secret for generic webhooks.
- Go to your GitHub repo, under Settings, click on Webhook option then click 'add Webhook' button.
- Paste the copied URL into
Payload URL
field and select application/json option inContent type
field, leave everything rest to defaults and click onAdd webhook
- Click on your newly added webhook to see details. Scroll down to see the
Recent Deliveries
section with an entry for PING test prefixed with a tick mark , which indicates the ping test was successful. Click on the entry to get more details about the REST API call and the associated response for the ping test. A successful PING test would mean Github is able to connect with your OpenShift cluster.
In this section, you will make a small change in Dockerfile, you will change the CMD used to keep the Container alive and commit the change. This should trigger a push event from Github to the OpenShift which will cause OpenShift to re-build the Docker image and re-deploy the pod using the newly built Docker image
- Go to the Dockerfile in github, edit the file, comment the first command and uncomment the second command as shown in the screenshot below. Then commit your changes
- Go back to the web console, go to Administrator view and click on
Builds
. You will notice a new Build is initiated automatically as shown in the following image. Once this build is done, a new Pod will be created using the new docker image that was built. - Click on
Pods
view to see that the old Pod is being Terminated and new Pod is being created - Click on the new Pod and go to the Terminal to verify that the new Container indeed is running the new process specified in the Dockerfile. Use the following command in the terminal.
ps aux | grep sleep
- You will get output similar to the following.
- If you go to your Github webhooks view, you will see a new entry under
Recent Deliveries
which maps to the recent notification delivered by Github to OpenShift in response to the new commit in the repo.
In this tutorial, you have successfully demonstrated source-to-image (S2I) capability using Dockerfiles. This use case shows how to deploy a pod from GitHub hosted Dockerfile, setup the connection between OpenShift and GitHub using webhooks and ensure that new code changes to the repository results in new pod being deployed on the OpenShift cluster.