Dockerfile
.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:
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:])
}
Calling this app will return “Hello,” followed by the given path.
Examples:
url | response |
---|---|
localhost:8080/ | Hello, ! |
localhost:8080/world | Hello, world! |
localhost:8080/appuio | Hello, appuio! |
The Dockerfile
defines the image build (Dockerfile reference
).
FROM registry.access.redhat.com/ubi8/go-toolset:1.24.6-1759234265 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.10-1756195303
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
It is a multi-stage build . The build is done in several stages using different containers.
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 under Docker Instructions .
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
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
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