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.
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.
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
- 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
MYNAMESPACEas your docker hub username.
where oshi36 is a docker hub username that we will be using in the further lab, you can change it according to your username.
s2i buildcommand 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
Dockerfile of the cloned git repo
Now, run the
s2i build command and specify
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.
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
docker run -p 30001:3000 -it $MYNAMESPACE/webapplocal
Now access the app from
app-port-30001 under the Lab URLs section
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:
After this, you can learn how to create an S2I builder image.
In this hands-on lab, we saw how to build an image with S2I and push it to a registry.