C++ Microservices in Docker
Expose Native C++ Code Within a Microservice
Microservices have become a popular way to architect applications, particularly those that compose functionality from a variety of loosely coupled systems and services. While there are a variety of frameworks and tools for implementing a microservice architecture, it isn’t always clear how to expose native code like C or C++ code within a wider microservice system. That’s where HydraExpress comes in.
HydraExpress provides a native C++ application server, allowing C++ code to be exposed as a web service over HTTP. HydraExpress implements an API similar to the Java Servlet API (now known as Jakarta Servlet API), making it simple and familiar to write native services. HydraExpress can be deployed inside of a docker container allowing it to be built, managed and deployed alongside other containers within a microservices architecture.
In this first of a series of blogs to get up an running with C++ microservices in Docker, we'll look specifically at how to deploy and enable HydraExpress.
- Part 2: Building Custom Servlets.
- Part 3: Optimizing the Container.
- Part 4: Adding a Numerical Library.
If you want to follow along and don't have a HydraExpress license. Contact us to discuss an evaluation version.
How to Deploy HydraExpress Within Docker
The first step in implementing C++ Microservices with HydraExpress inside of Docker is to deploy and enable HydraExpress within a Docker container.
We’ll define a Dockerfile that installs any prerequisite packages, downloads and installs HydraExpress, and orchestrates starting HydraExpress.
HydraExpress is supported on a wide range of platforms, and for this example we’ll focus on CentOS 7. These steps are equally applicable to other Linux distributions, however specific package names and installation instructions may need to be adjusted to that distribution’s conventions.
For our new Dockerfile, we’ll start with specifying the base image we’ll build on:
Next we’ll download HydraExpress installation media. Links to the latest version of HydraExpress can be downloaded from HydraExpress – Product Downloads. HydraExpress provides two distribution types, Perpetual and Evaluation, and each has slightly different installation options.
Installing a HydraExpress Evaluation License
Let’s start with installing an evaluation version of HydraExpress. Download installation media into our Docker image:
… RUN yum install -y wget RUN mkdir -p /opt/download RUN wget -q -O /opt/download/hydraexpress.run \ https://dslwuu69twiif.cloudfront.net/hydraexpress/2020/hydraexpress_2020_eval_linux_x86-64_gcc_4.8.run RUN chmod a+x /opt/download/hydraexpress.run
In order to install an evaluation version of HydraExpress, we’ll need a license.key file. The license.key file should be attached to your confirmation email. We’ll copy this file into our Docker image:
… COPY license.key /opt/download/license.key
This assumes that the license.key file is located in the same directory as our Dockerfile. With the HydraExpress installer and license.key in place, we can go ahead and install HydraExpress:
… ENV RWSF_HOME /opt/perforce/hydraexpress RUN /opt/download/hydraexpress.run \ -- mode unattended \ --prefix $RWSF_HOME \ --license-file /opt/download/license.key
HydraExpress is now installed in /opt/perforce/hydraexpress within the Docker image.
Installing a HydraExpress Perpetual License
For licensed users of HydraExpress, the installation process is very similar. The main difference is in how the license key is handled during installation.
We need to make two changes to the above steps in order to deploy a perpetual HydraExpress distribution. First, download the perpetual installation media:
… RUN wget -q -O /opt/download/hydraexpress.run \ https://dslwuu69twiif.cloudfront.net/hydraexpress/2020/hydraexpress_2020_linux_x86-64_gcc_4.8.run …
We need to adjust the installation command to pass the installer a license key instead of a license file:
… RUN /opt/download/hydraexpress.run \ --mode unattended \ --prefix /opt/perforce/hydraexpress \ --license-key <license-key>
<license-key> is the license key string included in your confirmation email. Need your license key? Contact us.
Setup and Run the HydraExpress Server
Whether you’ve installed the evaluation or perpetual HydraExpress distributions, the steps for running the server are the same. Start by defining a script (entrypoint.sh) for starting the HydraExpress server:
#! /bin/bash PATH=$RWSF_HOME/bin:$PATH rwsfserver --wait start
This script should be located in the same directory as the Dockerfile. Since this script will be executed by the docker image when it starts, make sure the script is executable:
$ chmod a+x entrypoint.sh
Next, we’ll update our Dockerfile to specify this entry point:
COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"]
With the entry point specified, we’re ready to build our Docker image:
$ docker build -t hydraexpress .
The above command assumes that we’re in the directory where our Dockerfile and other files are located. This will create a docker image named “hydraexpress”. We can show a list of our current images with:
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hydraexpress latest 8cf886709671 8 minutes ago 526MB
With our image created, we can now start our HydraExpress container. HydraExpress’s HTTP connector defaults to listening on port 8090, so we’ll publish that port from our container instance:
$ docker run --rm -it -p 8090:8090 hydraexpress ******************************************************************************* RWSF (TM) - Server Control Script Copyright (c) 2001-2020 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved. ******************************************************************************* RWSF_HOME = /opt/perforce/hydraexpress RWSP_HOME = /opt/perforce/hydraexpress/3rdparty/sourcepro Starting Rogue Wave Agent... INFO| Loading context: /examples/ INFO| Locale directory set to [/opt/perforce/hydraexpress/conf/locale] INFO| Default locale set to [en_US] INFO| Loading locale [en_US], catalog [messages_en_US.xml] INFO| Starting 'AJP 1.3' connector... INFO| Starting 'HTTP/1.1' connector... INFO| Starting 'HTTPS (HTTP/1.1)' connector...
With that we have HydraExpress running in a Docker container, and its HTTP port published. We can connect to it from our browser and connect to the default servlet context:
Next Steps for C++ Microservices
With these steps, you will have successfully deployed HydraExpress within a Docker container.
In the next blog, we look at extending the Docker image to include building and deploying a new C++ servlet.
Want to try this yourself? Contact us for an evaluation version.
For Reference and Further Reading
Documentation on writing C++ Servlets and start implementing C++ microservices today.
The complete Dockerfile and entrypoint.sh file:
FROM centos:7 RUN yum install -y wget RUN mkdir -p /opt/download RUN wget -q -O /opt/download/hydraexpress.run \ https://dslwuu69twiif.cloudfront.net/hydraexpress/2020/hydraexpress_2020_eval_linux_x86-64_gcc_4.8.run RUN chmod a+x /opt/download/hydraexpress.run COPY license.key /opt/download/license.key ENV RWSF_HOME /opt/perforce/hydraexpress RUN /opt/download/hydraexpress.run \ --mode unattended \ --prefix /opt/perforce/hydraexpress \ --license-file /opt/download/license.key COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"]
#! /bin/bash PATH=$RWSF_HOME/bin:$PATH rwsfserver --wait start