OpenShift + BuildConfig+ ConfigMaps

HI,

I am trying to create and run a buildconfig yml file.

C:\OpenShift>oc version
Client Version: 4.5.31
Kubernetes Version: v1.18.3+65bd32d
  • Background:-

I have multiple Springboot WebUI applications which i need to deploy on OpenShift

To have separate set of config yml files ( image stream, buildconfig, deployconfig, service, routes), for each and every application seems to be very inefficient.

Instead i would like to have a single set of parameterized yml files to which i can pass on custom parameters to setup each individual application

  • Solution so far:-

Version One

Dockerfile-

FROM org/rhelImage
USER root
# Install Yum Packages
RUN yum -y install\
net-tools\
&& yum -y install nmap-ncat\

RUN curl -s --create-dirs --insecure -L ${ARTIFACTURL} -o ${APPPATH}/${ARTIFACT}

# Add docker-entrypoint.sh to the image
ADD docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod -Rf 775 /app && chmod 775 /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
RUN chmod -R g+rx /app


# Expose port
EXPOSE $MY_PORT

# Set working directory when container starts
WORKDIR $APPPATH

# Starting the applicaiton using ENTRYPOINT
#ENTRYPOINT ["sh","/docker-entrypoint.sh"]


$ oc create configmap myapp-configmap --from-env-file=MyApp.properties
configmap/myapp-configmap created

$ oc describe cm myapp-configmap
Name:         myapp-configmap
Namespace:    1234
Labels:       <none>
Annotations:  <none>

Data
====
APPPATH:
----
/app
ARTIFACT:
----
myapp.jar
ARTIFACTURL:
----
"https://myorg/1.2.3.4/myApp-1.2.3.4.jar"
MY_PORT:
----
12305
Events:  <none> 

buildconfig.yaml snippet

 strategy:
        dockerStrategy:
          env:
            - name: GIT_SSL_NO_VERIFY
              value: "true"
            - name: ARTIFACTURL
              valueFrom:
                configMapKeyRef:
                  name: "myapp-configmap"
                  key: ARTIFACTURL
            - name: ARTIFACT
              valueFrom:
                configMapKeyRef:
                  name: "myapp-configmap"
                  key: ARTIFACT

This works fine. However I somehow need to have those env: variables in file.

I am doing this to have greater flexibility, i.e. lets say I have a new variable introduced in docker file, I need NOT change the buildconfig.yml I just add the new key:value pair to the property file, rebuild and we are good to go

This is what I do next;

Version Two

Dockerfile

FROM org/rhelImage
USER root
# Install Yum Packages
RUN yum -y install\
net-tools\
&& yum -y install nmap-ncat\

#Intializing the variables file;
RUN ["sh", "-c", "source ./MyApp.properties"]

RUN curl -s --create-dirs --insecure -L ${ARTIFACTURL} -o ${APPPATH}/${ARTIFACT}

# Add docker-entrypoint.sh to the image
ADD docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod -Rf 775 /app && chmod 775 /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
RUN chmod -R g+rx /app


# Expose port
EXPOSE $MY_PORT

# Set working directory when container starts
WORKDIR $APPPATH

# Starting the applicaiton using ENTRYPOINT
#ENTRYPOINT ["sh","/docker-entrypoint.sh"]


$ oc create configmap myapp-configmap --from-env-file=MyApp.properties=C:\MyRepo\MyTemplates\MyApp.properties
configmap/myapp-configmap created

C:\OpenShift>oc describe configmaps test-configmap
Name:         myapp-configmap
Namespace:    1234
Labels:       <none>
Annotations:  <none>

Data
====
MyApp.properties:
----
APPPATH=/app
ARTIFACTURL="https://myorg/1.2.3.4/myApp-1.2.3.4.jar"
ARTIFACT=myapp.jar
MY_PORT=12035
Events:  <none> 

buildconfig.yaml snippet

 source:
        contextDir: "${param_source_contextdir}"
        configMaps:
          - configMap:
              name: "${param_app_name}-configmap"

However the build fails

STEP 9: RUN ls ./MyApp.properties
ls: cannot access ./MyApp.properties: No such file or directory
error: build error: error building at STEP "RUN ls ./MyApp.properties": error while running runtime: exit status 2 
 

This means that the config map file didnt get copy to folder.

Can you please suggest what to do next?

Hello shirmukherjee,

In Dockerfile version 2:
You have passed the below line:
RUN ["sh", "-c", "source ./MyApp.properties"] which simply expected one of two situations:

  1. The file is already built in or created already in the base image you are using.
  2. You have copied the file from your machine to the image in an earlier layer in the dockerfile which doesn’t exist in the dockerfile you added above.

So the command will fail because there is not file with such a name in the image, and the configmap you created will be defined in openshift as an openshift object and it has nothing to do with the dockerfile.

You can use the config map in the build stage using the way you mentioned in the first method.

If you want to use those env from a file, you can put them in a file and copy it from your machine to the image in a step before the step RUN ["sh", "-c", "source ./MyApp.properties"]

Also you can take a look on this article from redhat based on openshift 3.11, it’s talking about how to use template to consolidate the entire application yaml files along with how to pass parameters to be customized in the runtime.

Thanks,
KodeKloud Support