Create and Build an Image Using the CLI

Overview

This getting started experience walks you through the simplest way to get a sample project up and running on OKD. There are a few different ways to launch images within a project, but this topic focuses on the quickest and easiest method.

If this is the first part of the documentation you have read, and you are unfamiliar with the core concepts of OKD version 3 (v3), you might want to start by reading about what’s new. This version of OKD is significantly different from version 2 (v2).

OKD 3 provides out of the box a set of languages and databases for developers with corresponding implementations and tutorials that allow you to kickstart your application development. Language support centers around the Quickstart templates, which in turn leverage builder images.

LanguageImplementations and Tutorials

Ruby

Rails

Python

Django

Node.js

Node.js

PHP

CakePHP

Perl

Dancer

Java

Other images provided by OKD include:

To help illustrate constructing such applications, the following sections guide you through creating a project that contains a sample Node.js application that will serve a welcome page and the current hit count (stored in a database).

This topic discusses both Quickstart and Instant App templates and applications. Quickstarts provide a starting point for application development, but they rely on your development to create a useful application. In contrast, Instant Apps like Jenkins are instantly usable.

Before You Begin

Before you can get started:

Forking the Sample Repository

  1. Visit the Ruby example page while you are logged in to GitHub.

    This topic follows the Ruby example, but you can follow along using any of the language examples provided in the OKD GitHub project.

  2. Fork the repository.

    You are redirected to your new fork.

  3. Copy the clone URL for your fork.

  4. Clone the repository to your local machine.

Creating a Project

To create an application, you must create a new project and specify the location of the source. From there, OKD begins the build process and creates a new deployment.

  1. Log into OKD from the CLI:

    • With username and password:

      1. $ oc login -u=<username> -p=<password> --server=<your-openshift-server> --insecure-skip-tls-verify
    • With oauth token:

      1. $ oc login <https://api.your-openshift-server.com> --token=<tokenID>
  1. To create a new project:

    1. $ oc new-project <projectname> --description="<description>" --display-name="<display_name>"

After creating the new project, you will be automatically switched to the new project namespace.

Creating an Application

To create a new application from the code in your forked repository:

  1. Create the application by specifying the source of the code:

    1. $ oc new-app openshift/ruby-20-centos7~https://github.com/<your_github_username>/ruby-ex

    OKD finds the matching builder image (which in this case is ruby-20-centos7) and then creates resources for the application (image stream, build configuration, deployment configuration, service). It also schedules the build.

  2. Track the progress of the build:

    1. $ oc logs -f bc/ruby-ex
  3. Once the build is complete and the resulting image has successfully pushed to your registry, check the status of your application:

    1. $ oc status

    Or you can view the build from the web console.

Creating your application might take some time. You can follow along on the Overview page of the web console to see the new resources being created, and watch the progress of the build and deployment. You can also use the oc get pods command to check when the pod is up and running, or the oc get builds command to see build statistics.

While the Ruby pod is being created, its status is shown as pending. The Ruby pod then starts up and displays its newly-assigned IP address. When the Ruby pod is running, the build is complete.

The oc status command tells you what IP address the service is running; the default port it deploys to is 8080.

Create a Route

An OKD route exposes a service at a host name, so that external clients can reach it by name. To create a route to your new application:

  1. Expose a service for ruby-ex:

    1. $ oc expose service ruby-ex
  2. View your new route:

    1. $ oc get route

Verify the Application is Running

To view your new application, paste the route location that you copied (in the previous section) into the address bar of your web browser and hit enter.

The example ruby-ex application is a simple welcome screen, and contains details on how to deploy code changes, manage your application, and other development resources.

Next, configure automated builds via a GitHub webhook trigger, so that code changes in your forked repository cause your application to rebuild.

Configuring Automated Builds

You forked the source code for this application from the OKD GitHub repository. Therefore, you can use a webhook to automatically trigger a rebuild of your application whenever you push code changes to your forked repository.

To set up a webhook for your application:

  1. View the triggers section of the **BuildConfig** to verify that a GitHub webhook trigger exists:

    1. $ oc edit bc/ruby-ex

    You should see something similar to this:

    1. triggers
    2. - github:
    3. secret: Q1tGY0i9f1ZFihQbX07S
    4. type: GitHub

    The secret ensures that only you and your repository can trigger the build.

  2. Run the following command to display the webhook URLs associated with your **BuildConfig**:

    1. $ oc describe bc ruby-ex
  3. Copy the GitHub webhook payload URL output by the above command.

  4. Navigate to your forked repository on GitHub, then click Settings.

  5. Click Webhooks & Services.

  6. Click Add webhook.

  7. Paste your webhook URL into the Payload URL field.

  8. Set the Content Type to application/json.

  9. Click Add webhook to save.

GitHub now attempts to send a ping payload to your OKD server to ensure that communication is successful. If you see a green check mark appear next to your webhook URL, then it is correctly configured. Hover your mouse over the check mark to see the status of the last delivery.

The next time you push a code change to your forked repository, your application will automatically rebuild.

Writing a Code Change

To work locally and then push changes to your application:

  1. On your local machine, use a text editor to change the sample application’s source for the file ruby-ex/config.ru

  2. Make a code change that will be visible from within your application. For example: on line 229, change the title from Welcome to your Ruby application on OpenShift to This is my Awesome OpenShift Application, then save your changes.

  3. Commit the change in git, and push the change to your fork.

    If your webhook is correctly configured, your application will immediately rebuild itself based on your changes. Once the rebuild is successful, view your updated application using the route that was created earlier.

Now going forward, all you need to do is push code updates and OKD handles the rest.

Manually Rebuilding Images

You may find it useful to manually rebuild an image if your webhook is not working, or if a build fails and you do not want to change the code before restarting the build. To manually rebuild the image based on your latest committed change to your forked repository:

  1. $ oc start-build ruby-ex

Troubleshooting

Changing Projects

Although the oc new-project command automatically sets your current project to the one you’ve just created, you can always change projects by running:

  1. $ oc project <project-name>

To view a list of projects:

  1. $ oc get projects

Manually Triggering Builds

If the build does not start automatically, start a build and stream the logs:

  1. $ oc start-build ruby-ex --follow

Alternatively, do not include --follow in the above command, and instead issue the following command after triggering the build, where n is the number of the build to track:

  1. $ oc logs -f build/ruby-ex-n