Go on z/OS

Go on z/OS

Go on z/OS

This group is a space for open discussions and sharing technical information, and also provides a connection to the wider Go open source community for anyone who is interested in Go on z/OS

 View Only

Go on z/OS and the Open Enterprise Foundation (OEF) Tools Container Image

By Joon Lee posted Tue September 03, 2024 03:40 PM

  

With the release of Go 1.23 for z/OS and Open Enterprise Foundation (OEF), a new Go container image has been made available for use with some exciting changes. In this blog we will review the new additions and explore how developers can take advantage of multi-stage builds.

Prerequisites

There are a few things you need to have before continuing:

  • A z/OS 3.1 system with z/OS UNIX System Services
    • A z/OS 2.5 system with the latest APARs will also work
  • The z/OS Containers Platform (zOSCP) suite of container tools - specifically Podman
  • An SMP/E license for the IBM Open Enterprise SDK for Go and zOSCP that come with their respective entitlement keys
    • Entitlement keys should be found in the attached entitlement memos

The new Go image no longer requires that you pull the z/OS base image. All you need to get started with a usable Go container is the Go image. You can get the image with the following command:

podman pull icr.io/zoscp/golang:latest --creds iamapikey:<go image entitlement key>

Using the New Go Image

In a previous blog, we went over how you could set up a container image for building and running your Go application. This process involved using the base z/OS image, copying the Go compiler from the Go image, and setting various environment variables. We also covererd how to create a container geared towards developing Go applications. This involved installing some quality of life tools like bash and git via z/OS Open Tools (Zopen Tools) and even more additions to your environment to enable these tools. With the new Go image, all of this has been simplified and streamlined so that you can spend more time focusing on developing and containerizing your applications! Below is an exmaple container file that outlines how one could make use of the new image to build and run your Go application.

# choose the version of Go  - "latest" is recommended
FROM golang:latest

# Base work directory within the container
WORKDIR /app

# Copy dependencies independently from building the application.
# This allows podman to cache this step for every build where
# the dependencies don't change.
COPY go.mod go.sum ./
RUN go mod download

# Build the Go application.
COPY *.go ./
RUN go build -o /<name of executable>

# Command to run on container initialization
CMD ["/<name of executable>"]

The biggest change is the use of the Open Enterprise Foundation Tools container image. It uses the z/OS base image and bundles together eight essential tools: Git, Curl, GNU Make, GNU Bash, Less, Vim, Ncurses, and Perl. The Go image uses the Open Enterprise Foundation Tools image as the base layer upon which we include the Go compiler as well as the Open Enterprise Languages on z/OS (OEL) C/C++ Compiler. This will enable the use of CGO in your code or any dependencies you have that require it. With all of these improvements, there is no longer a need to manually set up a development image like we did previously, you just need to use the Go image as your base layer and you are all set!

Multi-Stage Builds

When containerizing applications, you may wish to optimize your final image and leave out parts that will not be used to run your program. For example, Go builds it's programs into binaries. Once you have this binary, the compiler is not necessary to run the program. This is where multi-stage builds can come in handy. They are essentially container files that have multiple FROM statements, where each FROM can use a different base if you choose to. This can allow you to copy artifacts from one stage to another. The container file below builds the third CGO callback example from this blog and copies the resulting binary to the z/OS base image.

FROM golang:latest AS extract

WORKDIR /tmp

COPY ./cgodemo /tmp/cgodemo

RUN cd /tmp/cgodemo &&\
 GO111MODULE=off go build -o /cgo-callback-sort

FROM zos:latest
COPY --from=extract /cgo-callback-sort /cgo-callback-sort

CMD ["/cgo-callback-sort"]

Notice that in the first FROM statement, we have named this stage "extract" by including an AS <name>. We later reference this named stage in the COPY statement so that we can copy the Go program we just built. Since we do not require the Go compiler to run the program, we can use a different image as our base in the second FROM statement. In this case, we have chosen the z/OS base image for it's small size.

With that, you should be all set to get started with the new Go on z/OS image!

0 comments
43 views

Permalink