Skip to main content

Local container development with Podman and odo

ยท 5 min read
odo and Podman

So far, odo has been mainly focusing on container development on Kubernetes and OpenShift clusters.

In this post, we will showcase the experimental support we have recently added for Podman. We will see how odo can leverage Podman for local development in containers with no requirement whatsoever on any cluster โ€” making it easier to iterate on the application locally and transition to Kubernetes or OpenShift later on.

Prerequisitesโ€‹

Working locally with Podmanโ€‹

Let's revisit one of our quickstart guides, say the Golang one, to make it work with Podman.

Step 0. Creating the initial source code (optional)โ€‹

We will create the example source code by using some popular frameworks.

Before we begin, we will create a new directory and cd into it.

mkdir quickstart-demo && cd quickstart-demo

This is optional and you may use an existing project instead (make sure you cd into the project directory before running any odo commands) or a starter project from odo init.

For Go, we will create our own application using the standard library:

  1. Create the following main.go file:
package main

import (
"fmt"
"log"
"net/http"
)

func main() {
const addr = "0.0.0.0:8080"
http.HandleFunc("/", HelloServer)
log.Println("Up and running on", addr)
http.ListenAndServe(addr, nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
log.Println("New request:", *r)
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}
  1. Initialize a go.mod file:
go mod init my.example.go.project
Example
$ go mod init my.example.go.project
go: creating new go.mod: module my.example.go.project
go: to add module requirements and sums:
go mod tidy

Your source code has now been generated and created in the directory.

Step 1. Initializing your application (odo init)โ€‹

Now we'll initialize the application by creating a devfile.yaml to be deployed.

odo handles this automatically with the odo init command by auto-detecting the source code and downloading the appropriate Devfile.

Note: If you skipped Step 0, select a "starter project" when running odo init.

Let's run odo init and select Go:

odo init
Sample Output
$ odo init
__
/ \__ Initializing a new component
\__/ \ Files: Source code detected, a Devfile will be determined based upon source code autodetection
/ \__/ odo version: v3.6.0
\__/

Interactive mode enabled, please answer the following questions:
Based on the files in the current directory odo detected
Language: Go
Project type: Go
The devfile "go:1.0.2" from the registry "Staging" will be downloaded.
? Is this correct? Yes
โœ“ Downloading devfile "go:1.0.2" from registry "Staging" [1s]

โ†ช Container Configuration "runtime":
OPEN PORTS:
- 8080
ENVIRONMENT VARIABLES:

? Select container for which you want to change configuration? NONE - configuration is correct
? Enter component name: quickstart-demo

You can automate this command by executing:
odo init --name quickstart-demo --devfile go --devfile-registry Staging --devfile-version 1.0.2

Your new component 'quickstart-demo' is ready in the current directory.
To start editing your component, use 'odo dev' and open this folder in your favorite IDE.
Changes will be directly reflected on the cluster.
note

If you skipped Step 0 and selected "starter project", your output will be slightly different.

Step 2. Enabling the experimental modeโ€‹

Because the support for Podman is still experimental at the time of writing, we first need to explicitly opt-in.

Enabling the experimental mode can be done by setting the ODO_EXPERIMENTAL_MODE environment variable to true in the terminal session, like so:

export ODO_EXPERIMENTAL_MODE=true

Step 3. Iterating on your application locally on containers (odo dev)โ€‹

Now that we've generated our code as well as our Devfile, let's start iterating on our application locally by starting a Development session with odo dev, but targeting our local Podman.

odo dev on Podman will use the same inner loop development as for the cluster mode, allowing you to code, build, run and test the application in a continuous workflow.

Once you run odo dev --platform=podman, you can freely edit the application code in your favorite IDE and watch as odo rebuilds and redeploys it.

Let's run odo dev --platform=podman to start development on your Go application:

odo dev --platform=podman
Sample Output
$ odo dev --platform=podman
============================================================================
โš  Experimental mode enabled. Use at your own risk.
More details on https://odo.dev/docs/user-guides/advanced/experimental-mode
============================================================================

__
/ \__ Developing using the "quickstart-demo" Devfile
\__/ \ Platform: podman
/ \__/ odo version: v3.6.0
\__/

โ†ช Running on podman in Dev mode
โœ“ Deploying pod [5s]
โœ“ Building your application in container (command: build) [693ms]
โ€ข Executing the application (command: run) ...
- Forwarding from 127.0.0.1:20001 -> 8080

โ†ช Dev mode
Status:
Watching for changes in the current directory /tmp/test-go-podman/quickstart-demo

Keyboard Commands:
[Ctrl+c] - Exit and delete resources from podman
[p] - Manually apply local changes to the application on podman

You can now access the application at 127.0.0.1:20001 in your local browser and start your development loop. odo will watch for changes and push the code for real-time updates.

Example
$ curl http://127.0.0.1:20001/world

Hello, world!

We can optionally open the Podman Desktop application to take a look at the resources odo has created for our application on Podman:

Wrapping Upโ€‹

odo is now able to work with Podman to accelerate local development in containers, without requiring you to have access to any Kubernetes cluster.

Note that our support for Podman is still experimental, but we are working on improving the feature parity (as much as possible) with the cluster mode.

As such, any feedback is highly appreciated.