Skip to content

Instantly share code, notes, and snippets.

@Two4
Created October 20, 2021 21:26
Show Gist options
  • Save Two4/46121957deb499ebf5b83f44adf158d3 to your computer and use it in GitHub Desktop.
Save Two4/46121957deb499ebf5b83f44adf158d3 to your computer and use it in GitHub Desktop.
Dockerfile to allow Java programs using OR-Tools to be containerised (using google-jib) along with a third-party solver (CPLEX in this case)
# Dockerfile to allow Java programs using OR-Tools to be containerised - using google-jib - along with a third-party
# solver (CPLEX in this case)
# ref: https://hub.docker.com/_/debian
FROM debian:11 AS env
#############
## SETUP ##
#############
RUN apt-get update -qq \
&& apt-get install -qq \
git pkg-config wget make autoconf libtool zlib1g-dev gawk g++ curl subversion \
swig lsb-release default-jdk maven \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Install CMake 3.21.1
RUN wget -q "https://cmake.org/files/v3.21/cmake-3.21.1-linux-x86_64.sh" \
&& chmod a+x cmake-3.21.1-linux-x86_64.sh \
&& ./cmake-3.21.1-linux-x86_64.sh --prefix=/usr/local/ --skip-license \
&& rm cmake-3.21.1-linux-x86_64.sh
###########
## CPLEX ##
###########
FROM env AS cplex
# path to CPLEX Unix installer on docker-build host
ARG CPLEX_INSTALLER_PATH
WORKDIR /root/cplex-installer
ADD $CPLEX_INSTALLER_PATH cplex_installer.bin
# path where CPLEX is to be installed on the docker image
ARG CPLEX_INSTALL_PATH=/opt/ibm/CPLEX
SHELL ["/bin/bash", "-c"]
RUN touch installer.properties \
&& echo 'INSTALLER_UI=silent' >> installer.properties \
&& echo 'LICENSE_ACCEPTED=true' >> installer.properties \
&& echo "USER_INSTALL_DIR=${CPLEX_INSTALL_PATH}" >> installer.properties \
&& chmod u+x cplex_installer.bin \
&& ./cplex_installer.bin -f installer.properties \
&& rm -rf /root/cplex-installer
ENV UNIX_CPLEX_DIR=${CPLEX_INSTALL_PATH}
ENV USE_CPLEX=ON
################
## OR-TOOLS ##
################
FROM cplex AS devel
WORKDIR /root
#ARG SRC_GIT_BRANCH
#ENV SRC_GIT_BRANCH ${SRC_GIT_BRANCH:-master}
#ARG SRC_GIT_SHA1
#ENV SRC_GIT_SHA1 ${SRC_GIT_SHA1:-unknown}
## Download sources
## use SRC_GIT_SHA1 to modify the command
## i.e. avoid docker reusing the cache when new commit is pushed
#RUN git clone -b "${SRC_GIT_BRANCH}" --single-branch https://github.com/google/or-tools \
#&& echo "sha1: $(cd or-tools && git rev-parse --verify HEAD)" \
#&& echo "expected sha1: ${SRC_GIT_SHA1}"
#FIXME uncomment the above lines and comment the below line for a Dockerfile more like the original
RUN git clone --branch "stable" --single-branch --progress "https://github.com/google/or-tools"
# Build third parties
FROM devel AS third_party
WORKDIR /root/or-tools
RUN make detect && make third_party
# Build project
FROM third_party AS build
RUN make detect_java && make java
# Remove extra javadoc and source jars to allow more efficient wildcard COPY command in the next stage
FROM build AS base
RUN rm -f /root/or-tools/temp_java/ortools-java/target/*-javadoc.jar \
&& rm -f /root/or-tools/temp_java/ortools-java/target/*-sources.jar \
&& rm -f /root/or-tools/temp_java/ortools-linux-x86-64/target/*-javadoc.jar \
&& rm -f /root/or-tools/temp_java/ortools-linux-x86-64/target/*-sources.jar
# The base image below could probably be any image with a compatible JRE
FROM debian:11 AS app-image
RUN apt-get update && apt-get install -y default-jre
# copy custom-built dependencies over and add them to /app/libs directory (where jib puts dependency jars)
# these dependencies need to marked as 'provided' in scope in your .pom, so jib doesn't package it with the non-custom
# maven-central versions. Be careful version mismatches.
COPY --from=base /root/or-tools/temp_java/ortools-java/target/*.jar /app/libs/
COPY --from=base /root/or-tools/temp_java/ortools-linux-x86-64/target/*.jar /app/libs/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment