In our previous post, we walked through how to setup a PyRFC server program.  Now we will discuss how to create a Dockerfile to containerize your program. If you aren’t familiar with Docker or Dockerfiles, we suggest reviewing this documentation. After reading this post, you should have a working Dockerfile that can be used to build a PyRFC server image.

To begin, you should open a new file and save as “dockerfile” with the text editor of your choice.

Base Image

With your dockerfile open, lets start by adding the below lines into our file. This will establish the base os that we want our container to use. In this case being Ubuntu.

# Use Ubuntu runtime as base image
FROM ubuntu:latest

Updates and Installs

Now that we have downloaded the latest version of ubuntu, we should update all of the ubuntu packages. Along with updating ubuntu, we also install python3, git, and ipython.

RUN apt-get update \
&& apt-get install -y -qq python3-pip git \
&& cd/usr/local/bin \
&& ln -s /usr/bin/python3 python \
&& pip3 install --upgrade pip \
&& pip3 install ipython \
&& rm -rf /var/lib/apt/lists/*

Clone and Install PyRFC

Next we clone the PyRFC repo and install the PyRFC wheel.

RUN cd /tmp \
&& git clone https://github.com/SAP/PyRFC.git \
&& cd /tmp/PyRFC/dist \
&& pip3 install pyrfc-1.9.5-cp35-cp35m-linux_x86_64.whl

Change Terminal Background and Cache Recent Libraries

RUN echo "tput setaf 1;\necho '===\nBefore: iphython\nRun: ldconfig\n==='\ntput sgr0" > /root/.bashrc

Mount NWRFC SDK as Volume

At this point, we are ready to mount the NWRFC SDK as a volume under the /usr/sapnwrfcsdk directory.

RUN mkdir -p /etc/ld.so.conf.d/ \
&& mkdir -p /usr/sap/ \
&& echo "# include nwrfcsdk" > /etc/ld.so.conf.d/nwrfcsdk.conf \
&& echo "/usr/sap/nwrfcsdk/lib" >> /etc/ld.so.conf.d/nwrfcsdk.conf

Install Required Modules

If you have modules that need to be installed by pip, then you should include them in your requirements.txt file. This file should be located in the same directory as your dockerfile. In this step, we are copying your requirements.txt file from your local directory into the container at the specified directory.

COPY ./requirements.txt /usr/sap/requirements.txt
RUN pip3 install --trusted-host pypi.python.org -r /usr/sap/requirements.txt

Set Environment Variables

On the second line, we are copying all of the files in our current directory into the /usr/sap directory within our container. We want to be sure to have our NWRFC SDK included within our current directory, so that our environment path is set correctly in the container.

WORKDIR /usr/sap
COPY . /usr/sap
ENV PATH="${PATH}:/usr/sap/nwrfcsdk/lib"
RUN ldconfig /usr/local/lib

Start PyRFC Server

Finally, we are ready to start our server. Just insert the path to your server code.

ENTRYPOINT ["ipython", "/path/to/server/code"]

Conclusion

Unless you have modules that need to be installed by pip, you now have  a working PyRFC Dockerfile. If you need help creating your requirements.txt file, please see this post.

If you are looking for ways to build a cleaner docker image, read our post regarding dockerignore files.

About the author