Digital Developer Conference: Hybrid Cloud 2021. On Sep 21, gain free hybrid cloud skills from experts and partners. Register now

Use Red Hat OpenShift Source-to-Image to deploy applications from Dockerfiles

The Red Hat OpenShift Source-to-Image (S2I) framework eases your ability to create reproducible Docker container images from application source code. A Dockerfile is a recipe (or blueprint) for building Docker images. In this tutorial, you learn how to use OpenShift S2I to build a Docker image from a Dockerfile hosted in GitHub and deploy a container pod by using the image. You also learn how to set up a GitHub webhook to notify OpenShift of new code push and commit events so it automatically rebuilds and redeploys pods by using the latest changes to your code and the Dockerfile within your GitHub repo.

Prerequisites

For this tutorial, you must have a Red Hat OpenShift on IBM Cloud cluster (4.3 or higher) and a GitHub account.

Estimated time

This tutorial takes 30 minutes to complete.

Steps

  1. Fork the GitHub repo and host a Dockerfile
  2. Create a project
  3. Create a pod deployment using the Dockerfile from your GitHub repo
  4. Verify the container process matches the command specified in the Dockerfile
  5. Set up a GitHub Webhook
  6. Make changes to your GitHub repository

1. Fork the GitHub repo and host a Dockerfile

To fork the GitHub repository for this tutorial, click the Fork button that is located on the main repo page. Within the repo is 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;"]

During a later step of this tutorial, you make changes in the Dockerfile to trigger new pod deployments.

2. Create a project

  • From the Administrator perspective of OpenShift, go to Projects and click Create Project.
  • Name the project s2i-project and click Create.
  • After the project is created, you are redirected to the Overview tab of the Project Details page.

    Screen capture of an example Project Details page

3. Create a pod deployment using the Dockerfile from your GitHub repo

  • In the OpenShift web console, switch from the Administrator perspective to the Developer perspective.

  • Select Topology from the navigation panel.

  • To create an application, select the From Dockerfile option.

    Screen capture of the Topology page with the From Dockerfile option highlighted

  • Copy the URL of your GitHub repo, which should be https://github.com/<GITHUB-USERNAME>/oc-docker-s2i. Ensure that the URL includes your GitHub username and not the <GITHUB-USERNAME> placeholder.

  • Paste the URL of your GitHub repository in the Git Repo URL field.

  • In the Context Dir field, type /ubuntu, which is where the Dockerfile is located.

    Screen capture of the Import from Dockerfile area of the page

  • In the Resources section, select Deployment Config and keep the Create a route to the application option selected.

    Screen capture of the Resources area of the page

  • Click Create.

  • You are redirected to the Topology view, which lists the created pods.

  • If you click on your application, you open the Deployment Config (DC) view, where you can review the Details, Resources, and Monitoring logs. What happens is that OpenShift builds the Docker image by using the Dockerfile from your GitHub repo, creates a Docker image, uploads the image into the OpenShift internal image registry, and creates a pod by using that Docker image.

    Screen capture of the DeploymentConfig view with the Resources tab highlighted

4. Verify the container process matches the command specified in the Dockerfile

  • From the Topology view, click the DeploymentConfig icon for your application to open the Deployment Config Details page.

    Screen capture of the Topology view with `DC oc-docker-s-2-i` highlighted

  • Select the Pods tab on the Deployment Config Details page to list the running pod.

    Screen capture of the Deployment Config Details page with the Pods tab highlighted

  • Select the running pod to open the Pod Details view.

    Screen capture of the Deployment Config Details page with the running pod name highlighted

  • Select Terminal, which brings up a terminal (shell) inside your running Ubuntu container.

    Screen capture of the terminal

  • Verify that the pod is running the sleep infinity process, as specified in the Dockerfile, by typing the following command in the terminal:

    ps aux | grep sleep
    

    Screen capture of the command results with the sleep infinity location highlighted

5. Set up a GitHub webhook

GitHub webhooks allow external services to be notified when certain events happen. To automatically deploy a new pod when updates to the GitHub Dockerfile occur, you must configure your GitHub repo with the webhook that OpenShift provides as part of the BuildConfig. This is best achieved by using the OpenShift web console.

  • From the menu, select Builds.

  • On the Build Configs page, select the build configuration of your application.

    Screen capture of the Build Configs view

  • You are redirected to the detailed view of the build config. Go to the Webhooks section and click Copy URL with Secret for the Generic webhook type.

    Screen capture of the Webhooks section with the Generic webhook row highlighted

  • Open your GitHub repo and navigate to Settings.

  • Select Webhooks from the Options menu and click Add Webhook.

    Screen capture of the Webhooks page within a sample GitHub repo

  • On the Add webhook page, paste the URL that you copied earlier into the Payload URL field.

  • In the Content type field, select the application/json option.

  • Click Add webhook.

    Screen capture of the Add webhook page

  • Select your newly added webhook to see its details. The Recent Deliveries section displays an entry for a ping test that is prefixed with a tick mark, which indicates that the ping test was successful.

    Screen capture of the Recent Deliveries section

  • Click the entry to view more details about the REST API call and the associated response for the ping test. A successful ping test indicates that GitHub is able to connect with your OpenShift cluster.

6. Make changes to your GitHub repository

In this step, you make a small change to the Dockerfile, change the CMD that is used to keep the container alive, and commit the change. This triggers a push event from GitHub to the OpenShift cluster, which causes OpenShift to rebuild the Docker image and redeploy the pod by using the newly built Docker image.

  • Go to the Dockerfile in GitHub, edit the file by adding a comment to the first command and removing the comment from the second command, as shown in the following screen capture image.

    Screen capture of the Dockerfile

  • Commit your changes.

  • Go back to the OpenShift web console and open the Administrator view.

  • Click Builds. Notice that a new build is automatically initiated, as shown in the following screen capture image. After this build is done, a new pod is created by using the newly built Docker image.

    Screen capture of the status of the new build

  • Select the Pods view to see that the old pod is being terminated and new pod is being created.

    Screen capture of the status of the pods

  • Select the new pod and go to the terminal to verify that the new container is running the new process that is specified in the Dockerfile. Use the following command in the terminal:

    ps aux | grep sleep
    
  • You receive output similar to the one shown in the following screen capture image.

    Screen capture of the command output

  • If you go to your GitHub webhooks view, you can see a new entry within the Recent Deliveries section. The entry maps to the recent notification delivered by GitHub to OpenShift in response to the new commit in the repo.

    Screen capture of the Recent Deliveries section

Summary

In this tutorial, you successfully demonstrated the OpenShift S2I capability by using a Dockerfile. You deployed a pod from a Dockerfile hosted in GitHub, set up the connection between OpenShift and GitHub by using a webhook, and tested that new code changes within the repository resulted in a new pod deployment within your OpenShift cluster.

Learn more about S2I by watching the OpenShift Source-to-Image video and trying the Create your own source-to-image entry in an OpenShift cluster tutorial.