Explore the capabilities of a tool called S2I (Source-to-Image).

Recently, while drafting an OpenShift solution tutorial, I explored an interesting tool called S2I (Source-to-Image). In this post, you will learn how to create a container image directly from your source code and push the generated container image to a private IBM Cloud Container registry.

What is S2I (Source-to-Image)?

S2I is a tool for building reproducible, Docker-formatted container images. It produces ready-to-run images by injecting application source into a container image and assembling a new image. The new image incorporates the base image (the builder) and built source and is ready to use with the docker runcommand. S2I supports incremental builds, which re-use previously downloaded dependencies, previously built artifacts, etc.

Installation and setup

Let’s start by installing S2I on your machine. 

You can install the s2i binary using go get, which will download the source-to-image code into your $GOPATH, build the s2i binary, and install it into your $GOPATH/bin:

$ go get github.com/openshift/source-to-image/cmd/s2i

For Mac, You can either follow the installation instructions for Linux (and use the darwin-amd64 link) or you can just install source-to-image with Homebrew:

$ brew install source-to-image

Follow the instructions here to install on other operating systems.

To confirm the installation, run the below command on a terminal or command prompt:

$ s2i 

Follow the instructions mentioned in the link here to set up the IBM Cloud Container Registry CLI and your registry namespace.

Once created, open the terminal and export the MYNAMESPACE environment variable pointing to your Registry namespace:

$ export MYNAMESPACE=<YOUR_REGISTRY_NAMESPACE>

Let’s build

The s2i build command provides two options to generate a new container image: 

  1. Build a Docker image from a remote Git repository:
    • Usage: s2i build <source> <image> [<tag>] [flags]
    • $ s2i build https://github.com/IBM-Cloud/get-started-node nodeshift/centos7-s2i-nodejs:latest us.icr.io/$MYNAMESPACE/webapp
  2. Build from a local directory. If this directory is a git repo, the current commit will be built:
    • Clone the get-started-node Git repo and run the below command
    • $ s2i build . nodeshift/centos7-s2i-nodejs:latest us.icr.io/$MYNAMESPACE/webapplocal

Even before building an image, you can see the generated Dockerfile by appending --as-dockerfile Dockerfile to your build commands.

Once generated, check the contents of the generated Dockerfile:

$ cat Dockerfile

To understand the S2I requirements and the artifacts of the generated Dockerfile, see the documentation

Run the app locally and push it to private registry

Check the generated Docker image by running it locally with the following command:

$ docker run -p 3000:3000 -it us.icr.io/$MYNAMESPACE/webapp

Launch a browser and point to http://localhost3000 to see the app running locally:

Push the container image to the private IBM Cloud Container Registry. Don’t forget to log into the container registry with ibmcloud cr login command:

$ docker push us.icr.io/$MYNAMESPACE/webapp

You can check the container image by running the following command:

$ ibmcloud cr images

What’s next?

Categories

More from Cloud

IBM Cloud inactive identities: Ideas for automated processing

4 min read - Regular cleanup is part of all account administration and security best practices, not just for cloud environments. In our blog post on identifying inactive identities, we looked at the APIs offered by IBM Cloud Identity and Access Management (IAM) and how to utilize them to obtain details on IAM identities and API keys. Some readers provided feedback and asked on how to proceed and act on identified inactive identities. In response, we are going lay out possible steps to take.…

IBM Cloud VMware as a Service introduces multitenant as a new, cost-efficient consumption model

4 min read - Businesses often struggle with ongoing operational needs like monitoring, patching and maintenance of their VMware infrastructure or the added concerns over capacity management. At the same time, cost efficiency and control are very important. Not all workloads have identical needs and different business applications have variable requirements. For example, production applications and regulated workloads may require strong isolation, but development/testing, training environments, disaster recovery sites or other applications may have lower availability requirements or they can be ephemeral in nature,…

IBM accelerates enterprise AI for clients with new capabilities on IBM Z

5 min read - Today, we are excited to unveil a new suite of AI offerings for IBM Z that are designed to help clients improve business outcomes by speeding the implementation of enterprise AI on IBM Z across a wide variety of use cases and industries. We are bringing artificial intelligence (AI) to emerging use cases that our clients (like Swiss insurance provider La Mobilière) have begun exploring, such as enhancing the accuracy of insurance policy recommendations, increasing the accuracy and timeliness of…

IBM NS1 Connect: How IBM is delivering network connectivity with premium DNS offerings

4 min read - For most enterprises, how their users access applications and data is an essential part of doing business, and how they service those application and data responses has a direct correlation to revenue generation.    According to We Are Social’s Digital 2023 Global Overview Report, there are 5.19 billion people around the world using the internet in 2023. There’s an imperative need for businesses to trust their networks to deliver meaningful content to address customer needs.  So how responsive is the…