Build a Container Image from Source-Code using S2I and Push It to a PrivateRegistry
Tags: docker, openshift, ibmcloud, registry
Published: 2020-04-14 02:42:13

---
title: Build a Container Image from Source-Code using S2I and Push It to a PrivateRegistry
published: true
tags: docker,openshift,ibm-cloud,registry
canonical_url: 
---

### Build a Container Image from Source-Code using S2I and Push It to a Private Registry

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

Recently, while drafting an [**OpenShift solution tutorial**](https://cloud.ibm.com/docs/tutorials?topic=solution-tutorials-scalable-webapp-openshift), 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](https://docs.openshift.com/container-platform/3.5/creating_images/s2i.html#creating-images-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](https://opensource.com/article/19/5/creating-source-image-build-pipeline-okd) on other operating systems.

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

Follow the instructions mentioned in the [link here](https://cloud.ibm.com/docs/services/Registry?topic=registry-registry_setup_cli_namespace) 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:

### Let’s build

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

Build a Docker image from a remote Git repository:

```
$ s2i build [https://github.com/IBM-Cloud/get-started-node](https://github.com/IBM-Cloud/get-started-node) nodeshift/centos7-s2i-nodejs:latest us.icr.io/$MYNAMESPACE/webapp
```

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:

To understand the S2I requirements and the artifacts of the generated Dockerfile, [see the documentation](https://docs.openshift.com/container-platform/3.11/creating_images/s2i.html#s2i-scripts).

### 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](http://localhost3000) to see the app running locally:

![](https://cdn-images-1.medium.com/max/1024/0*9sxifUtYWMNkMyaq.png)

Push the container image to the private [IBM Cloud Container Registry](https://www.ibm.com/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?

- Follow the steps mentioned in the [solution tutorial](https://cloud.ibm.com/docs/tutorials?topic=solution-tutorials-scalable-webapp-openshift) to deploy the generated container image as a new app on OpenShift. Also, learn to create a scalable web application on OpenShift [in this solution tutorial.](https://cloud.ibm.com/docs/tutorials?topic=solution-tutorials-scalable-webapp-openshift)

![](https://cdn-images-1.medium.com/max/1024/1*Yk7Nzgc0_8sVegvPccH7-g.png)

- [Learn how to create an S2I builder image](https://blog.openshift.com/create-s2i-builder-image/).

_Originally published at_ [_https://www.ibm.com_](https://www.ibm.com/cloud/blog/build-a-container-image-from-source-code-using-s2i) _on September 12, 2019._