Source to Image (S2I)

One of the useful components of OpenShift is its source-to-image capability. S2I is a framework that makes it easy to turn your source code into runnable images. The main advantage of using S2I for building reproducible docker images is the ease of use for developers. You'll see just how simple it can be in this lab.

Let's build a node.js web app, using S2I

We can do this either via the command line or the web console. You decide which you'd rather do and follow the steps below.

  1. Goto the terminal and type the following:
    $ oc new-project metro-map
    $ oc new-app --name=dc-metro-map https://github.com/RedHatGov/openshift-workshops.git --context-dir=dc-metro-map
    $ oc expose service dc-metro-map
    

    💥 TECHNICAL NOTE

    When using the CLI, OpenShift automatically detects the source code type and select the nodejs builder image.

Check out the build details

We can see the details of what the S2I builder did. This can be helpful to diagnose issues if builds are failing.

💥 TECHNICAL NOTE

TIP: For a node.js app, running "npm shrinkwrap" is a good practice to perform on your branch before releasing changes that you plan to build/deploy as an image with S2I

Viewing build details

  1. Goto the terminal and type the following:
    $ oc get builds
    
  2. Note the name of your build from the above command output and use it to see the logs with:
    $ oc logs builds/[BUILD_NAME]
    

The console will print out the full log for your build. Note, you could pipe this to more or less for easier viewing in the CLI.

See the app in action

Let's see this app in action!

  1. Goto the terminal and type the following:
    $ oc get routes
    
  2. Copy the HOST/PORT and paste into your favorite web browser:
  3. The app should look like this in your web browser:image
  1. Switch to administrator mode, if you're not already thereimage
  2. Click on "Projects" then the "Create Project" button and give it a name of metro-mapimage
  3. Switch to developer mode and select the project metro-map if it did not take you there automaticallyimage
  4. Click "+Add"image
  5. Click "From Git"image
  6. Fill out the "Git Repo URL" field as follows:

    Git Repo URL

    https://github.com/RedHatGov/openshift-workshops.git

    image
  7. Ensure that the repository is validated (as shown above)
  8. Click on the "Show Advanced Git Options" expenderimage
  9. Fill out the "Context Dir" field as follows:

    Context Dir

    dc-metro-map

    #### 💥 **TECHNICAL NOTE** _Do NOT include a leading / or the build will fail!_
  10. Under "Builder", click click on the "Node.js" iconimage
  11. Select Node.js 12 UBI7 (12-ubi7) in the Builder Image versionimage
  12. Fill out the fields, under "General" as follows:

    Application

    Create Application

    Application Name

    dc-metro-map

    Name

    dc-metro-map

  13. Choose "Deployment Configuration"image
  14. Scroll to the bottom and click "Create"

Check out the build details

We can see the details of what the S2I builder did. This can be helpful to diagnose issues if builds are failing.

💥 TECHNICAL NOTE

For a node.js app, running "npm shrinkwrap" is a good practice to perform on your branch before releasing changes that you plan to build/deploy as an image with S2I

Viewing build details

  1. Click on "Topology", the "node"/"dc-metro-map" icon, then Resources, and then on "View logs"image
  2. You should see a log output similar to the one below:image

See the app in action

Let's see this app in action!

  1. Click on "Topology"image
  2. Click the arrow, at the top right corner of the "dc-metro-map" icon, to launch a new tab/window, with the running appimage