Linux Format

Building your own pods

Assembling your own pod is as easy as a pull request…

-

t’s easy to think that you’ll mostly use Podman

to pull ready-made containeri­sed apps from the registries, but the problems we outlined in the intro are real. There will come a time when you need a specific app working in a specific environmen­t with particular libraries and dependenci­es that, because you’re now on Ubuntu 45 (Dangerous Duck), you simply can’t install on your local system.

In these cases, you can use Podman to create a container that will hold everything you need to get your precious software running.

One of our favourite things to do to pass time when we’re trying to avoid doing any work is play solitaire in the terminal. It’s a classic game, but the version we use relies on Google’s Go language. There’s no guarantee the developers will continue to update the game, and given that Go is a Google project, there’s little reason to believe it will exist in any form at all in a decade or so. To begin with, you need a base OS image. We know that Solitaire-tui works with Ubuntu 22.04, so that seems a sensible place to start. Open a terminal and pull it on to your local machine with:

podman pull ubuntu:jammy

To deploy a container with your newly pulled image, you need to use Podman’s run

command, specify a name for your container, and point it at your newly downloaded image:

$ podman run -ti --name your_project_name

Iubuntu:jammy

Hit Enter and you find yourself on the root command line of your minimal distro. While it should be up to date, it’s always a good idea to make sure, so:

$ apt-get update && apt-get upgrade

It’s a frequent complaint of Ubuntu users that the distro is full of bloat and tools you’ll never use. This isn’t the case in your bare-bones base image. You need to install almost everything yourself. Start by installing

Wget, then use it to fetch the latest Go tarball.

$ apt install wget

$ wget https://go.dev/dl/go1.21.3.linux-amd64.tar.gz

Unpack it and add the new directorie­s to your PATH:

$ tar -C /usr/local -xvf go1.21.3.linux-amd64.tar.gz $ export PATH=$PATH:/usr/local/go/bin

$ export PATH=$PATH:/root/go/bin

You can now use Go to install Solitaire-tui:

$ go install github.com/brianstrau­ch/solitaire-tui@

latest

Installati­on is quick, and when it completes, you can play your favourite card game in the terminal with:

$ solitaire-tui

With Solitaire-tui safely installed in a Podman

container, it’s yours for ever, and will run on any Linux system into the far future. If Ubuntu, Golang or even Google itself vanish from the face of the Earth – or more likely, if there’s a breaking change to the Go language – you’ll still have your container and be able to play virtual cards in your terminal.

Build and share web apps

So far, our pod creation journey hasn’t been that far removed from installing software inside a VM. You can enter the container, and you can run the software you installed. But using Podman means you can also easily ship it elsewhere for others to use.

To begin with, we’re going to build a simple applicatio­n using Go on our host system (see above for instructio­ns). The web app will be a simple program, with an HTTP server listening on port 8888, and will return the message: ‘Subscribe to Linux Format Today!!!’ It’s not a hugely complex app, and besides its evergreen and ever pertinent message, it isn’t overly useful (I think you’ll find we’re very useful!–Ed) either.

Initialise your Go app, then use Nano to create the main.go file:

$ go mod init test_app

$ nano main.go

In the new file, copy the following:

package main import (

“fmt”;

“net/http”;

)

The fmt library provides formatted I/O, enabling you to print messages, while net/http exposes the web server.

The following main function registers a handler function using the http.HandleFunc function, http. ResponseWr­iter writes a response to the client, while http.Request contains informatio­n about the request.

The handler uses fmt.println to write our urgent message, and http.ListenAndS­erve starts the HTTP server on port 8888. func main() {

http.HandleFunc(“/”, func(w http.ResponseWr­iter,

r *http.Request) { fmt.Fprintln(w, “Subscribe to Linux Format

Today!!!”)

}) http.ListenAndS­erve(“:8888”, nil) }

Save and exit Nano with Ctrl+O then Ctrl+X, and check your basic applicatio­n works with:

$ go run main.go && curl localhost:8888

Because there’s no sense in reinventin­g the wheel, we’re going to create a Dockerfile – a script that contains instructio­ns for building a container image – to containeri­se our web app.

$ nano Dockerfile

In here, add your dependenci­es:

FROM golang:1.21.3-alpine

Set your working directory:

WORKDIR /testapp

Next, copy go.mod from the host machine into the container:

COPY go.mod ./

Install any dependenci­es:

RUN go mod download

Copy all Go files into the container:

COPY *.go ./

Build an executable named test-app:

RUN go build -o /test-app

Expose port 8888

EXPOSE 8888

Finally, you want your applicatio­n to run when the container starts:

CMD [“/test-app”]

Save and exit Nano, and use Podman to build the container image:

$ podman build -t test_image .

Podman asks you to choose where to pull golang

from. Use the arrow keys to navigate the list, and hit Enter to make your choice. Sit back and relax as

Podman pulls the necessary blobs.

The entire process should take mere seconds, and comprises eight steps, after which you should see this message:

Successful­ly tagged localhost/test_image:latest

Run the following and you should see your shiny new container listed alongside all the other ones on your system:

$ podman images

You already know how your Go app should work when you run it directly; now you can test it out using Podman, too. Invoke Podman with run , give it the name you want to use, and specify the port to listen on, along with the name and version of your image:

$ podman run --name test -p 8888:8888 localhost/test_

image:latest

Your app starts up in the background. To see your inspiratio­nal message, use curl :

$ curl localhost:8888

If everything went well, you should see the message: ‘Subscribe to Linux Format Today!!!’

alternativ­ely, type the address into your browser.

Ship your containers

Naturally, you want to share your containers with the world, so everybody can hear the good news about subscribin­g to your favourite periodical. If you want the world to be able to use your new app, you first need to push it to a registry. The biggest and most commonly used registry is still Docker Hub, so we’ll use that for the next part of this guide.

In your browser, visit http://docker.io, and click the Sign In button. You don’t yet have an account, so locate the Don’t Have An Account? Sign Up link. You need a valid email address, username and password. Make sure you don’t accidental­ly sign up for junk mail during the process. Check your email for the activation email, then use your new credential­s to sign in.

Back in the terminal, log into Docker Hub through

Podman (you’re asked for username and password):

podman login docker.io

It’s all very well having random containers on your local system with names like test_app, but there are thousands on Docker Hub. How will anyone know which one is yours, and how will you find your image if you want to download it again?

You need to rebuild your container again using your username, so it can be pinned to you and your account:

$ podman build -t your_username/your_image_name .

Now when you run the following, you should see another container in the format localhost/your_ username/your_image_name:

$ podman images

If you’re sure you want to share your container image with the rest of humanity, enter:

$ podman push your_username/your_image_name

Finally, you should be able to find your container image again using:

$ podman search your_image_name

 ?? ?? Playing solitaire in your terminal doesn’t really highlight Linux’s competence as a gaming platform, but at least it’s portable!
Playing solitaire in your terminal doesn’t really highlight Linux’s competence as a gaming platform, but at least it’s portable!
 ?? ??
 ?? ?? Building a container with Podman isn’t as frightenin­g as we first thought it would be; however, we’d be reluctant to submit our project to the registries.
Building a container with Podman isn’t as frightenin­g as we first thought it would be; however, we’d be reluctant to submit our project to the registries.
 ?? ??

Newspapers in English

Newspapers from Australia