Skip to content

Commit 0a39c03

Browse files
committed
Alternative container construction samples, using buildah/podman + RH UBI base images
1 parent fa8d9eb commit 0a39c03

File tree

5 files changed

+181
-8
lines changed

5 files changed

+181
-8
lines changed

samples/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ Some trivial scripts run the sample programs in matching pairs:
3939
* pubsub.sh : Start amqssub and then run the amqspub program immediately
4040

4141
Building a container:
42-
* runSample.sh : Drives the process to get `amqsput` into the container
43-
* runSample.Dockerfile : Instructions to create the container with its dependencies
44-
* runSample.gomod : Copied into the container as `go.mod`
42+
* runSample.sh : Drives the process to get `amqsput` into the container
43+
* runSample.bud.sh : Drives the process to get `amqsput` into the container using podman/buildah as an alternative approach
44+
* runSample.*.Dockerfile : Instructions to create containers with runtime dependencies
45+
* runSample.gomod : Copied into the container as `go.mod`
46+
Two variants of the Dockerfile are provided. Set the `FROM` environment variable to "UBI"
47+
to use Red Hat Universal Base Images as the starting points for building and runtime;
48+
otherwise an Ubuntu/Debian combination is used.
4549

4650
The `mqitest` sample program in its own subdirectory is a more general demonstration
4751
of many of the features available from the MQI rather than focussed on a specific
@@ -64,7 +68,7 @@ For example
6468
go run amqsput.go DEV.QUEUE.1 QM1
6569
```
6670

67-
The amqsput.go program also allows the queue and queue manager names to
71+
The amqsput.go program also allows the queue and queue manager names to
6872
be provided by environment variables, to show another configuration
6973
mechanism. That approach will often be used in container deployments,
7074
and is demonstrated in the runSample set of files.
@@ -76,7 +80,7 @@ there is something waiting to receive the publications when they are made. The
7680

7781
## Building a container for running samples
7882
There is an set of files in here that will show how to create a container that runs
79-
the `amqsput` program. The `runSample.sh` script drives the process. It will try to
83+
the `amqsput` program. The `runSample*.sh` scripts drive the process. It will try to
8084
connect to a queue manager running on the host machine.
8185

8286
The process is split into two pieces - the first is used to compile the program, and

samples/runSample.bud.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# This is an example of running one of the sample programs in a container.
2+
# It used buildah/podman as an alternative to 'docker'.
3+
4+
function latestSemVer {
5+
(for x in $*
6+
do
7+
echo $x | sed "s/^v//g"
8+
done) | sort -n | tail -1
9+
}
10+
11+
# Assume repo tags have been created in a sensible order. Find the mq-golang
12+
# version in the root go.mod file and the current Git tag for this repo.
13+
# Then pick the latest version to create the Docker tag
14+
VERDEP=`cat ../go.mod | awk '/mq-golang/ {print $2}' `
15+
VERREPO=`git tag -l 2>/dev/null| sort | tail -1 `
16+
17+
VER=`latestSemVer $VERDEP $VERREPO`
18+
if [ -z "$VER" ]
19+
then
20+
VER="latest"
21+
fi
22+
23+
# Basic name for the container
24+
TAG=mq-golang-sample-amqsput
25+
echo Building container with tag $TAG:$VER
26+
27+
# Build the container which includes compilation of the program.
28+
# The UBI variant is built on the Red Hat Universal Base Image set of containers.
29+
dfile="runSample.ubi.Dockerfile"
30+
buildah bud -t $TAG:$VER -f $dfile .
31+
32+
if [ $? -eq 0 ]
33+
then
34+
# This line grabs a currently active IPv4 address for this machine. It's probably
35+
# not what you want to use for a real system but it's useful for testing. "localhost"
36+
# does not necessarily work inside the container so we need a real address.
37+
addr=`ip -4 addr | grep "state UP" -A2 | grep inet | tail -n1 | awk '{print $2}' | cut -f1 -d'/'`
38+
echo "Local address is $addr"
39+
port="1414"
40+
41+
if [ ! -z "addr" ]
42+
then
43+
# Run the container.
44+
# We override default command line values in amqsput via env vars.
45+
podman run -e MQSERVER="SYSTEM.DEF.SVRCONN/TCP/$addr($port)" \
46+
-e QUEUE=DEV.QUEUE.1 \
47+
-e QMGR=QM1 \
48+
$TAG:$VER
49+
else
50+
echo "Cannot find a working address for this system"
51+
exit 1
52+
fi
53+
fi
File renamed without changes.

samples/runSample.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function latestSemVer {
1010
# Assume repo tags have been created in a sensible order. Find the mq-golang
1111
# version in the root go.mod file and the current Git tag for this repo.
1212
# Then pick the latest version to create the Docker tag
13-
VERDEP=`cat ../../go.mod | awk '/mq-golang/ {print $2}' `
13+
VERDEP=`cat ../go.mod | awk '/mq-golang/ {print $2}' `
1414
VERREPO=`git tag -l 2>/dev/null| sort | tail -1 `
1515

1616
VER=`latestSemVer $VERDEP $VERREPO`
@@ -23,8 +23,17 @@ fi
2323
TAG=mq-golang-sample-amqsput
2424
echo Building container with tag $TAG:$VER
2525

26-
# Build the container which includes compilation of the program
27-
docker build -t $TAG:$VER -f runSample.Dockerfile .
26+
# Build the container which includes compilation of the program. Can set the FROM
27+
# environment variable outside this script to choose which base image to work from.
28+
# The UBI variant is built on the Red Hat Universal Base Image set of containers; the
29+
# default is built on Ubuntu/Debian containers.
30+
if [ "$FROM" = "UBI" ]
31+
then
32+
dfile="runSample.ubi.Dockerfile"
33+
else
34+
dfile="runSample.deb.Dockerfile"
35+
fi
36+
docker build -t $TAG:$VER -f $dfile .
2837

2938
if [ $? -eq 0 ]
3039
then

samples/runSample.ubi.Dockerfile

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# © Copyright IBM Corporation 2019, 2021
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# This Dockerfile has two separate stages.
17+
#
18+
# The first stage is used to compile the Go program, where we need tools like the Go and C compilers.
19+
# The second stage is a runtime-only container that holds just the things we need to
20+
# execute the compiled program.
21+
#
22+
# Files and directories are copied from the builder container to the runtime container as needed.
23+
#
24+
# The base images are taken from the Red Hat Universal Base Images repository
25+
26+
# Start by setting some global variables that can still be overridden on the build command line.
27+
ARG BASE_IMAGE=registry.access.redhat.com/ubi8/ubi
28+
ARG GOPATH_ARG="/go"
29+
30+
###########################################################
31+
# This starts the BUILD phase
32+
###########################################################
33+
FROM $BASE_IMAGE AS builder
34+
35+
ARG GOPATH_ARG
36+
ENV GOPATH=$GOPATH_ARG \
37+
ORG="github.com/ibm-messaging"
38+
39+
# Install the Go compiler and some other tools. The version of Go that
40+
# is available from the repository is new enough that we don't need to
41+
# explicitly pull it from Google. Installing Go also gives prereqs like the C compiler.
42+
RUN yum --disableplugin=subscription-manager -y install wget curl tar golang \
43+
&& yum --disableplugin=subscription-manager clean all
44+
45+
# Create a location for the go programs and the MQ installation
46+
RUN mkdir -p $GOPATH/src $GOPATH/bin $GOPATH/pkg \
47+
&& chmod -R 777 $GOPATH \
48+
&& cd /tmp \
49+
&& mkdir -p /opt/mqm \
50+
&& chmod a+rx /opt/mqm
51+
52+
# Location of the downloadable MQ client package \
53+
ENV RDURL="https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/messaging/mqdev/redist" \
54+
RDTAR="IBM-MQC-Redist-LinuxX64.tar.gz" \
55+
VRMF=9.2.3.0
56+
57+
# Install the MQ client from the Redistributable package. This also contains the
58+
# header files we need to compile against. Setup the subset of the package
59+
# we are going to keep - the genmqpkg.sh script removes unneeded parts
60+
ENV genmqpkg_incnls=1 \
61+
genmqpkg_incsdk=1 \
62+
genmqpkg_inctls=1
63+
64+
RUN cd /opt/mqm \
65+
&& curl -LO "$RDURL/$VRMF-$RDTAR" \
66+
&& tar -zxf ./*.tar.gz \
67+
&& rm -f ./*.tar.gz \
68+
&& bin/genmqpkg.sh -b /opt/mqm
69+
70+
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
71+
72+
# Copy the source file over. We also need a go.mod file
73+
# The source for that has a different name in the repo so it doesn't accidentally get
74+
# used. We rename it during this copy.
75+
COPY amqsput.go $GOPATH_ARG/src
76+
COPY runSample.gomod $GOPATH_ARG/src/go.mod
77+
78+
# Do the actual compile. This will automatically download the ibmmq package
79+
RUN cd $GOPATH_ARG/src && go build -o $GOPATH_ARG/bin/amqsput amqsput.go
80+
81+
###########################################################
82+
# This starts the RUNTIME phase
83+
###########################################################
84+
# Now that there is a container with the compiled program we can build a smaller
85+
# runtime image. Start from one of the smaller base container images.
86+
FROM registry.access.redhat.com/ubi8/ubi-minimal
87+
ARG GOPATH_ARG
88+
89+
# Copy over the MQ runtime client code. This does preserve the .h files used during compile
90+
# but those are tiny so there's no real space-saving from deleting them here.
91+
COPY --from=builder /opt/mqm /opt/mqm
92+
93+
# Create some directories that may be needed at runtime, depending on the container's
94+
# security environment.
95+
RUN mkdir -p /IBM/MQ/data/errors \
96+
&& mkdir -p /.mqm \
97+
&& chmod -R 777 /IBM \
98+
&& chmod -R 777 /.mqm \
99+
&& mkdir -p /go/bin
100+
101+
# The actual program has all of the Go runtime embedded; we only need the single
102+
# binary along with the MQ client libraries, for it to run.
103+
COPY --from=builder $GOPATH_ARG/bin/amqsput /go/bin/amqsput
104+
105+
# The startup script will set MQSERVER and optionally set more
106+
# environment variables that will be passed to amqsput through this entrypoint.
107+
ENTRYPOINT [ "/go/bin/amqsput" ]

0 commit comments

Comments
 (0)