Building Containers from Source Code

The source code for this section is contained in the 02_03 branch of the GitHub repository for this course.

Software developers have adopted Git as the de facto source code management system in the past 15 years. The good news is that OpenShift knows Git well, so you can deploy applications directly from your source code repositories without much effort.

Deploying

It is straightforward to deploy an application from your source code repository. First, open the OpenShift web console and log in as the "kubeadmin" user. Then navigate to the Developer perspective. Then create a new project to encapsulate and easily manage your work. Call this project "sourcecode."

You should now see the "+Add" menu entry on the left side of the screen. One of the options in that screen is the "Import from Git" entry. Click on it, and paste the URL of a project, for example, gitlab.com/akosma/simple-deno-api.git.

As soon as you paste the URL, OpenShift will immediately analyze the structure and programming language of the project and automatically recommend options for its build process. In our case, it’s a small application built with the Go programming language, and as such, it will advise the options shown on the screen.

import from git
Figure 1. Deploying a project directly from its Git repository

This particular example doesn’t require more configurations than the ones shown on the screen; click the Create button.

After a few seconds, you will see your application running on the "Topology" screen. OpenShift will download the source code and trigger your project’s build. Click on the Topology screen icon to see the "Build" section, indicating that a build is running. The compilation and deployment of your application can take some time, depending on the complexity of the source code and the programming language used.

Once the build has finished, on the same pane, you will see a route available under the "Routes" section. Click on it, and you will see your application in action.

Container Registry

OpenShift has built your application source code, and the product of this build process is a container. You can see the container that OpenShift made for you on the "Administrator" perspective, selecting the "Builds" menu and then the "ImageStreams" menu entry.

OpenShift includes a container registry; developers can use it as any other registry from outside the cluster. Let us use "podman" to access the container registry and run the container locally on your workstation.

Users must have the "registry-editor" and the "system:image-builder" roles to access the container registry. Since we’re connected to the Web Console using the "kubeadmin" user, we can provide those roles directly from the user interface without using the command line.

Navigate to the "User Management" section and select "RoleBindings." Click on the Create binding button, and fill the form using the following values:

  • Name: developer-sourcecode-registry-editor

  • Namespace: sourcecode

  • Role name: registry-editor

  • Subject: User

  • Subject name: developer

Do the same for the "system:image-builder" role, using a different "Name" field on the top of the form.

Now on the terminal, log in as "developer" and use the following commands:

PROJECT=sourcecode
REGISTRY="default-route-openshift-image-registry.apps-crc.testing/${PROJECT}"
podman login --tls-verify=false -u unused -p $(oc whoami -t) ${REGISTRY}
podman pull ${REGISTRY}/hello-git:latest
podman run --rm --publish 5000:8080 ${REGISTRY}/hello-git:latest

When logging into the container registry provided by CRC, we must specify the "--tls-verify=false" option to avoid errors caused by self-signed certificates. To prevent passing this entry every time we use the command, we can add the CRC registry to the configuration of our system as a trusted registry. On RHEL, you can do that by editing the file "/etc/containers/registries.conf" or your local equivalent, "$HOME/.config/containers/registries.conf" and adding the following snippet:

[[registry]]
location="default-route-openshift-image-registry.apps-crc.testing"
insecure=true

Updating the Application

The purpose of source code is to evolve continuously. Clone your Git project using your preferred method, and perform a minor modification in the source code, changing the message displayed on the screen. Then commit and push your changes to your repository.

On the OpenShift Web Console, return to the "Developer" perspective and select the "Topology" screen. You will see your application running. Click on the icon, and you will see, in the "Builds" section, a button labeled Start Build. Click on it and wait. This second build will re-fetch the code from the repository and will redeploy the application automatically for you.

Click on the application route to see your application displaying a new message. But it would be great if we didn’t manually trigger a build.