9.9.1 Build a container image

This setion covers building a container image using a Dockerfile.

Task 9.9.1.1: Build a container image

The build can be done using any container building tool that supports Dockerfile builds.

The sample application is an HTTP server written in the Go programming language.

The following files are needed inside your application repository:

Sample go application

This Go code defines an HTTP server listening on port 8080. It has to be placed in the main.go file.

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", HelloServer)
	http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}

source

Calling this app will return “Hello,” followed by the given path.

Examples:

urlresponse
localhost:8080/Hello, !
localhost:8080/worldHello, world!
localhost:8080/appuioHello, appuio!

Image build instruction

The Dockerfile defines the image build (Dockerfile reference).

FROM registry.access.redhat.com/ubi8/go-toolset:1.20.12-5.1712568462 AS build
COPY main.go /opt/app-root/src
ENV CGO_ENABLED=0\
    GOOS=linux\
    GOARCH=amd64\
    GO111MODULE=off
RUN go build -a -o go-hello-world-app .

FROM registry.access.redhat.com/ubi8/ubi:8.9-1160
RUN useradd -ms /bin/bash golang
RUN chgrp -R 0 /home/golang && \
    chmod -R g+rwX /home/golang
USER golang
COPY --from=build /opt/app-root/src/go-hello-world-app /home/golang/
EXPOSE 8080
CMD /home/golang/go-hello-world-app

source

It is a multi-stage build. The build is done in several stages using different containers.

  1. use a Go container to build the Go application
  2. copy the go binary from the build to a minimal ubi image Universal Base Image

Image build

The image build is shown using Buildah. Buildah - a tool that facilitates building OCI container images.

Find Docker instructions hint at the bottom of this page or here.

Buildah build command:

buildah bud -f Dockerfile -t go-hello-world .

The image is available locally:

buildah images
REPOSITORY                                    TAG      IMAGE ID       CREATED         SIZE
localhost/go-hello-world                      latest   7f3ed9de1e49   3 seconds ago   219 MB
registry.access.redhat.com/ubi8/ubi           8.2      7923da9ba983   6 days ago      212 MB
registry.access.redhat.com/ubi8/go-toolset    1.13.4   4bf10ac637aa   5 weeks ago     990 MB

Image test

Test the container image locally using Podman. Use this command to run the container:

podman run -p 8088:8080 -ti localhost/go-hello-world

This makes the Go application accessible by the port 8088 of your device.

It can be tested with a browser (http://localhost:8088/world) or using curl:

curl localhost:8088/world

Publish image to Docker Hub

To make the image accessible to OpenShift, it must be pushed to an image registry. We use Docker Hub as the registry.

podman login
podman push localhost/go-hello-world:latest docker://docker.io/appuio/go-hello-world:latest