1- # © Copyright IBM Corporation 2019, 2020
1+ # © Copyright IBM Corporation 2019, 2021
22#
33# Licensed under the Apache License, Version 2.0 (the "License");
44# you may not use this file except in compliance with the License.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- ARG BASE_IMAGE=ubuntu:18.04
16- FROM $BASE_IMAGE
1715
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+ # Just for fun, I've used two different base images, trying to get the runtime image as small
24+ # as possible while still using a "regular" libc-based container.
25+
26+ # Start by setting some global variables that can still be overridden on the build command line.
27+ ARG BASE_IMAGE=ubuntu:18.04
1828ARG GOPATH_ARG="/go"
1929ARG GOVERSION=1.13.15
2030
31+ # ##########################################################
32+ # This starts the BUILD phase
33+ # ##########################################################
34+ FROM $BASE_IMAGE AS builder
35+
36+ ARG GOVERSION
37+ ARG GOPATH_ARG
2138ENV GOVERSION=${GOVERSION} \
2239 GOPATH=$GOPATH_ARG \
2340 GOTAR=go${GOVERSION}.linux-amd64.tar.gz \
@@ -43,7 +60,7 @@ RUN export DEBIAN_FRONTEND=noninteractive \
4360 build-essential \
4461 && rm -rf /var/lib/apt/lists/*
4562
46- # Create location for the go programs and the MQ installation
63+ # Create a location for the go programs and the MQ installation
4764RUN mkdir -p $GOPATH/src $GOPATH/bin $GOPATH/pkg \
4865 && chmod -R 777 $GOPATH \
4966 && cd /tmp \
@@ -57,7 +74,7 @@ RUN mkdir -p $GOPATH/src $GOPATH/bin $GOPATH/pkg \
5774# Location of the downloadable MQ client package \
5875ENV RDURL="https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/messaging/mqdev/redist" \
5976 RDTAR="IBM-MQC-Redist-LinuxX64.tar.gz" \
60- VRMF=9.2.2 .0
77+ VRMF=9.2.3 .0
6178
6279# Install the MQ client from the Redistributable package. This also contains the
6380# header files we need to compile against. Setup the subset of the package
@@ -67,23 +84,48 @@ ENV genmqpkg_incnls=1 \
6784 genmqpkg_inctls=1
6885
6986RUN cd /opt/mqm \
70- && curl -LO "$RDURL/$VRMF-$RDTAR" \
71- && tar -zxf ./*.tar.gz \
72- && rm -f ./*.tar.gz \
73- && bin/genmqpkg.sh -b /opt/mqm
87+ && curl -LO "$RDURL/$VRMF-$RDTAR" \
88+ && tar -zxf ./*.tar.gz \
89+ && rm -f ./*.tar.gz \
90+ && bin/genmqpkg.sh -b /opt/mqm
7491
7592# We need the Go compiler in our PATH
7693ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/go-$GOVERSION/bin
7794
7895# Copy the source file over. We also need a go.mod file
7996# The source for that has a different name in the repo so it doesn't accidentally get
80- # used and we rename it during this copy.
81- COPY amqsput.go $GOPATH_ARG/src
97+ # used. We rename it during this copy.
98+ COPY amqsput.go $GOPATH_ARG/src
8299COPY runSample.gomod $GOPATH_ARG/src/go.mod
83100
84101# Do the actual compile. This will automatically download the ibmmq package
85102RUN cd $GOPATH_ARG/src && go build -o $GOPATH_ARG/bin/amqsput amqsput.go
86103
87- # The startup script will set MQSERVER and optionally set
88- # more environment variables that will be passed to amqsput through this entrypoint.
89- ENTRYPOINT /go/bin/amqsput $QUEUE $QMGR
104+ # ##########################################################
105+ # This starts the RUNTIME phase
106+ # ##########################################################
107+ # Now that there is a container with the compiled program we can build a smaller
108+ # runtime image. Start from one of the smaller base container images.
109+ FROM debian:stretch-slim
110+ ARG GOPATH_ARG
111+ ARG GOVERSION
112+
113+ # Copy over the MQ runtime client code. This does preserve the .h files used during compile
114+ # but those are tiny so there's no real space-saving from deleting them here.
115+ COPY --from=builder /opt/mqm /opt/mqm
116+
117+ # Create some directories that may be needed at runtime, depending on the container's
118+ # security environment.
119+ RUN mkdir -p /IBM/MQ/data/errors \
120+ && mkdir -p /.mqm \
121+ && chmod -R 777 /IBM \
122+ && chmod -R 777 /.mqm \
123+ && mkdir -p /go/bin
124+
125+ # The actual program has all of the Go runtime embedded; we only need the single
126+ # binary along with the MQ client libraries, for it to run.
127+ COPY --from=builder $GOPATH_ARG/bin/amqsput /go/bin/amqsput
128+
129+ # The startup script will set MQSERVER and optionally set more
130+ # environment variables that will be passed to amqsput through this entrypoint.
131+ ENTRYPOINT [ "/go/bin/amqsput" ]
0 commit comments