Skip to content

Commit

Permalink
Improved building of the Docker container
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Maier <[email protected]>
  • Loading branch information
andy-maier committed Sep 29, 2024
1 parent a703e2b commit 5025f45
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Note: /dist/ is needed in the docker build to access the distribution archive

# User specific configuration files
/myconfig/

Expand All @@ -10,7 +12,6 @@

# Make build
/build/
/dist/
/*.egg-info/
/AUTHORS
/ChangeLog
Expand Down
79 changes: 65 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,77 @@
# Dockerfile for zhmc-os-forwarder project
#
# This image runs the zhmc_os_forwarder command.
# The standard metric definition file is provided in its default location, so
# the -m option does not need to be specified.
#
# The HMC credentials file still needs to be made available to the container
# The os forwarder config file needs to be made available to the container
# using some mount option and specified with -c.
#
# Example docker command to run the forwarder using a locally built version of this image:
#
# docker run -it --rm -v $(pwd)/myconfig:/root/myconfig -p 514:514 zhmcosforwarder -c /root/myconfig/config.yaml -v
# docker run --rm -v $(pwd)/myconfig:/root/myconfig -p 514:514 zhmc_os_forwarder -c /root/myconfig/config.yaml -v

FROM python:3.12-alpine as builder

# Path name of binary distribution archive of zhmc-os-forwarder package
ARG bdist_file
RUN : "${bdist_file:?Build argument bdist_file is required}"

# Install some packages onto this minimal Alpine image:
# - git - in case the Python requirements use git+https links
# - gcc musl-dev - in case Python wheels based on C need to be built (e.g. for rpds)
RUN apk add git gcc musl-dev

# Make sure the installed Python commands are found
ENV PATH=/root/.local/bin:$PATH

# Install the Python package of this project
COPY ${bdist_file} /tmp/${bdist_file}
RUN pip install --user /tmp/${bdist_file}

# Show the installed Linux packages
RUN echo "Installed Linux packages:" \
&& apk info -v

# Show the installed Python packages
RUN echo "Installed Python packages:" \
&& pip list

# Display files in 'rpds' Python package (verifying that it can be imported)
RUN echo "Files in rpds Python package:" \
&& python -c "import rpds, os, sys; rpds_dir=os.path.dirname(rpds.__file__); print(rpds_dir); sys.stdout.flush(); os.system(f'ls -al {rpds_dir}')"

# The Python 'rpds' package (used by 'jsonschema') has a shared library that is
# built during its installation, and thus depends on APIs of the system and
# the C library of the builder OS used in the first stage of this Dockerfile.
# Therefore, the OS used in the final stage needs to be compatible with the
# builder OS. We use the same OS image to make sure.
FROM python:3.12-alpine

# Version of the zhmc-os-forwarder package
ARG package_version
RUN : "${package_version:?Build argument package_version is required}"

# Image build date in ISO-8601 format
ARG build_date
RUN : "${build_date:?Build argument build_date is required}"

# Git commit ID of the zhmc-os-forwarder repo used to build the image
ARG git_commit
RUN : "${git_commit:?Build argument git_commit is required}"

FROM python:3.9-slim
# Set image metadata
LABEL org.opencontainers.image.title="IBM Z HMC OS Message Forwarder"
LABEL org.opencontainers.image.version="${package_version}"
LABEL org.opencontainers.image.authors="Andreas Maier"
LABEL org.opencontainers.image.created="${build_date}"
LABEL org.opencontainers.image.url="https://github.com/zhmcclient/zhmc-os-forwarder"
LABEL org.opencontainers.image.documentation="https://zhmc-os-forwarder.readthedocs.io"
LABEL org.opencontainers.image.source="https://github.com/zhmcclient/zhmc-os-forwarder"
LABEL org.opencontainers.image.licenses="Apache Software License 2.0"
LABEL org.opencontainers.image.revision="${git_commit}"

# Install this package
ENV TMP_DIR=/tmp/zhmc-os-forwarder
WORKDIR $TMP_DIR
COPY . $TMP_DIR
RUN pip install . && rm -rf $TMP_DIR
# Copy the installed Python packages from the builder image
COPY --from=builder /root/.local /root/.local

# Set the current directory when running this image
WORKDIR /root
# Make sure the installed Python commands are found
ENV PATH=/root/.local/bin:$PATH

EXPOSE 9291
ENTRYPOINT ["zhmc_os_forwarder"]
Expand Down
20 changes: 12 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ package_name := zhmc_os_forwarder
# version indicator by 1.
package_version := $(shell $(PYTHON_CMD) -m setuptools_scm)

docker_registry := zhmcosforwarder
# Docker image
docker_image_name := zhmc_os_forwarder
docker_image_tag := latest

python_mn_version := $(shell $(PYTHON_CMD) -c "import sys; sys.stdout.write('{}.{}'.format(sys.version_info[0], sys.version_info[1]))")
pymn := $(shell $(PYTHON_CMD) -c "import sys; sys.stdout.write('py{}{}'.format(sys.version_info[0], sys.version_info[1]))")
Expand All @@ -125,7 +127,7 @@ test_py_files := \
$(wildcard $(test_dir)/*/*.py) \

dist_dir := dist
bdist_file := $(dist_dir)/$(package_name)-$(package_version)-py2.py3-none-any.whl
bdist_file := $(dist_dir)/$(package_name)-$(package_version)-py3-none-any.whl
sdist_file := $(dist_dir)/$(package_name)-$(package_version).tar.gz

# Dependencies of the distribution archives. Since the $(version_file) is
Expand All @@ -136,7 +138,7 @@ dist_dependent_files := \
LICENSE \
README.md \
requirements.txt \
$(package_py_files) \
$(filter-out $(version_file), $(package_py_files)) \

doc_dir := docs
doc_build_dir := build_docs
Expand Down Expand Up @@ -205,7 +207,7 @@ help:
@echo " build - Build the distribution files in $(dist_dir)"
@echo " builddoc - Build the documentation in $(doc_build_dir)"
@echo " all - Do all of the above"
@echo " docker - Build local Docker image in registry $(docker_registry)"
@echo " docker - Build local Docker image $(docker_image_name):$(docker_image_tag)"
@echo " authors - Generate AUTHORS.md file from git log"
@echo " upload - Upload the package to Pypi"
@echo " clean - Remove any temporary files"
Expand Down Expand Up @@ -381,7 +383,7 @@ $(doc_build_file): $(done_dir)/develop_$(pymn)_$(PACKAGE_LEVEL).done $(doc_depen
sphinx-build -b html -v $(doc_dir) $(doc_build_dir)
@echo "Makefile: Done generating HTML documentation"

$(sdist_file): $(done_dir)/develop_$(pymn)_$(PACKAGE_LEVEL).done Makefile $(dist_dependent_files)
$(sdist_file): $(done_dir)/develop_$(pymn)_$(PACKAGE_LEVEL).done Makefile $(dist_dependent_files) $(version_file)
@echo "Makefile: Building the source distribution archive: $(sdist_file)"
$(PYTHON_CMD) -m build --sdist --outdir $(dist_dir) .
@echo "Makefile: Done building the source distribution archive: $(sdist_file)"
Expand Down Expand Up @@ -431,9 +433,11 @@ $(done_dir)/bandit_$(pymn)_$(PACKAGE_LEVEL).done: $(done_dir)/develop_$(pymn)_$(
echo "done" >$@
@echo "Makefile: Done running Bandit"

$(done_dir)/docker_$(pymn)_$(PACKAGE_LEVEL).done: $(done_dir)/develop_$(pymn)_$(PACKAGE_LEVEL).done Dockerfile .dockerignore Makefile $(dist_dependent_files)
@echo "Makefile: Building Docker image $(docker_registry):latest"
$(done_dir)/docker_$(pymn)_$(PACKAGE_LEVEL).done: $(done_dir)/develop_$(pymn)_$(PACKAGE_LEVEL).done Dockerfile .dockerignore $(bdist_file)
@echo "Makefile: Building Docker image $(docker_image_name):$(docker_image_tag)"
-$(call RM_FUNC,$@)
docker build -t $(docker_registry):latest .
docker build --tag $(docker_image_name):$(docker_image_tag) --build-arg bdist_file=$(bdist_file) --build-arg package_version=$(subst +,.,$(package_version)) --build-arg build_date="$(shell date -Iseconds)" --build-arg git_commit="$(shell git rev-parse HEAD)" .
docker run --rm $(docker_image_name):$(docker_image_tag) --version
docker image list --filter reference=$(docker_image_name)
@echo "Makefile: Done building Docker image"
echo "done" >$@
1 change: 1 addition & 0 deletions changes/100.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved building of the Docker container to reduce its size.
2 changes: 2 additions & 0 deletions changes/100.incompatible.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Changed mage name of Docker container image from 'zhmcosforwarder' to
'zhmc_os_forwarder' to match the command name.
15 changes: 6 additions & 9 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ Running in a Docker container
-----------------------------

If you want to run the forwarder in a Docker container you can create the
container as follows, using the ``Dockerfile`` provided in the Git repository.

Note that the provided ``Dockerfile`` can also be used as a starting point if
you want to embed the forwarder in your own Docker/OCI image.
container as follows, using the Dockerfile provided in the Git repository.

* Clone the Git repository of the forwarder and switch to the clone's root
directory:
Expand All @@ -53,8 +50,8 @@ you want to embed the forwarder in your own Docker/OCI image.
$ make docker
This builds a container image named 'zhmcforwarder:latest' in your local Docker
environment.
This builds a container image named ``zhmc_os_forwarder:latest`` in your local
Docker environment.

The image does not contain the forwarder config file, so that needs to be
specified when starting the container.
Expand All @@ -63,7 +60,7 @@ you want to embed the forwarder in your own Docker/OCI image.

.. code-block:: bash
$ docker run -it --rm -v $(pwd)/myconfig:/root/myconfig zhmcforwarder -p 514:514 -c /root/myconfig/config.yaml -v
$ docker run --rm -v $(pwd)/myconfig:/root/myconfig zhmc_os_forwarder -p 514:514 -c /root/myconfig/config.yaml -v
In this command, the forwarder config file is provided on the local system
as ``./myconfig/config.yaml``. The ``-v`` option of 'docker run' mounts the
Expand All @@ -73,8 +70,8 @@ you want to embed the forwarder in your own Docker/OCI image.

The command above maps port 514 in the docker container to port 514 of the
system running docker. That is the default port used by syslog. If your remote
syslog servers use different ports, they need to be mapped using the `-p` option
of the "docker run" command.
syslog servers use different ports, they need to be mapped using the ``-p`` option
of the ``docker run`` command.


zhmc_os_forwarder command
Expand Down

0 comments on commit 5025f45

Please sign in to comment.