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

25 April 2022
S2I
kubernetes
Openshift

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

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 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 the 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 run command. S2I supports incremental builds, which re-use previously downloaded dependencies, previously built artifacts, etc.

Figure 1: S2I workflow and Image Source:https://cloud.redhat.com/blog/source-image-s2i-deep-dive-ben-parees-openshift-commons-briefing-43
Figure 1: S2I workflow and Image Source:https://cloud.redhat.com/blog/source-image-s2i-deep-dive-ben-parees-openshift-commons-briefing-43

Lab Setup

You can start the lab setup by clicking on the Lab Setup button on the right side of the screen. Please note that there are app-specific URLs exposed specifically for the hands-on lab purpose and a terminal and IDE will also get set up with this.

Our lab has been set up with all necessary tools like base OS (Ubuntu), and developer tools like Git, Vim, wget, and others. 

S2I and Docker Installation

  • Let’s start by installing S2I on the machine. There are various ways to install S2I on the machine but for now, we will be installing it using the Linux distribution guide.
wget https://github.com/openshift/source-to-image/releases/download/v1.3.1/source-to-image-v1.3.1-a5a77147-linux-amd64.tar.gz
tar -xvzf source-to-image-v1.3.1-a5a77147-linux-amd64.tar.gz
mv s2i /usr/local/bin

For Mac with help of brew source-to-image can be installed. It can also be installed with the go get or go install command.

  • To confirm the installation, run the below command on a terminal
s2i
  • To push the images to a registry, we will be using the docker hub for that, and for this let's install docker on the machine
sudo apt update
sudo apt install docker.io -y
  • Next, Setup the environment variable MYNAMESPACE as your docker hub username.
export MYNAMESPACE=oshi36

where oshi36 is a docker hub username that we will be using in the further lab, you can change it according to your username.

Let’s build

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

First, to build a Docker image from a remote Git repository:

s2i build https://github.com/IBM-Cloud/get-started-node nodeshift/centos7-s2i-nodejs:latest $MYNAMESPACE/webapp

Second, to build from a local directory. If this directory is a git repo, the current commit will be built and for this clone the get-started-node git repo

git clone https://github.com/IBM-Cloud/get-started-node

Check the Dockerfile of the cloned git repo

cat ~/get-started-node/Dockerfile

Now, run the s2i build command and specify Dockerfile with --as-dockerfile

cd ~/get-started-node &&
s2i build . nodeshift/centos7-s2i-nodejs:latest $MYNAMESPACE/webapplocal --as-dockerfile Dockerfile

Now, check the contents of the Dockerfile again and will see changes in it.

cat ~/get-started-node/Dockerfile

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

Now, build the docker image from the newly generated Dockerfile

cd ~/get-started-node &&
docker build -t $MYNAMESPACE/webapplocal .

Run the app locally and push it to the registry

  • Check the both generated Docker image by running it locally with the following command:
docker run -p 30000:3000 -it $MYNAMESPACE/webapp

Now access the app from app-port-30000 under the Lab URLs section

Figure 1: Web App while accessing it from 30000 port
Figure 1: Web App while accessing it from 30000 port
docker run -p 30001:3000 -it $MYNAMESPACE/webapplocal

Now access the app from app-port-30001 under the Lab URLs section 

Figure 2: Web App while accessing it from 30001 port
Figure 2: Web App while accessing it from 30001 port

Now, push both of the docker images that are generated in the build steps to the docker hub. And for this do not forget to log in on the terminal with your docker credentials by using the docker login command.

Now, push the docker images

docker push $MYNAMESPACE/webapp
docker push $MYNAMESPACE/webapplocal
  • You can check the container images by running the following command:
docker images

What’s next?

After this, you can learn how to create an S2I builder image.

Conclusion

In this hands-on lab, we saw how to build an image with S2I and push it to a registry.

About the Author

Vidyasagar Chandra

Vidyasagar Chandra

Solutions Architect, Product Manager & Developer Advocate, IBM.

Vidyasagar is a polyglot and pragmatic programmer, who loves technologies changing lives. He is an AI Enthusiast mastering Machine learning and Data Science. He has more than 14 years of experience and currently working as Solutions Architect, Product Manager & Developer Advocate at IBM.