简体   繁体   中英

How to build an image using Configmap

I am trying to make our deployment process simpler. Currently, we build a jar file for our Spring Boot application then, build an image with the following DockerFile.

FROM [DTRServer]/java/javaimage_mmddyyyy:8
EXPOSE 8080
COPY /conf/myapp-conf.yaml /etc/k8t/myapp-conf.yaml
ADD target/myapp_mmddyyyy.jar myapp.jar
ENTRYPOINT ["java", "-jar", "myapp.jar", "--spring.config.location=/etc/k8t/myapp-conf.yaml"]

In our k8t, we create a Configmap(app-config). So our deployment.yaml is like

      containers:
        - image: [DTRServer]/projectname/myappimage:0.2.4-43
          imagePullPolicy: Always
          name: myapp
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: myapp-conf
              mountPath: /etc/k8t/
      volumes:
        - name: myapp-conf
          configMap:
            name: myapp-conf

Why do I need to specify spring.config.location in Dockerfile when building? We are going to use Configmap. So I deleted config file info from Dockerfile and deployment.yaml.

FROM [DTRServer]/java/javaimage_mmddyyyy:8
EXPOSE 8080
ADD target/myapp_mmddyyyy.jar myapp.jar
ENTRYPOINT ["java", "-jar", "myapp.jar"]
      containers:
        - image: [DTRServer]/projectname/myappimage:0.2.4-43
          imagePullPolicy: Always
          name: myapp
          ports:
            - containerPort: 8080
      volumes:
        - name: myapp-conf
          configMap:
            name: myapp-conf

Then, pod doesn't like it. It complains that it cannot find config values. How do I build an image without configuration info, and spin up a pod using Configmap?

Spring Boot 2.4 configuration tree support can be used together with Kubernetes ConfigMaps. If you want multiple files to be loaded from the mounted directory such that the file name is the key and the contents become the value, the configtree: prefix needs to be used. The volumeMounts section still needs to be present in the deployment.yml to specify where the ConfigMap data should be mounted. The deployment.yml would look something like this:

  containers:
    - image: [DTRServer]/projectname/myappimage:0.2.4-43
      imagePullPolicy: Always
      name: myapp
      ports:
        - containerPort: 8080
      volumeMounts:
        - name: myapp-conf
          mountPath: /etc/k8t/
      env:
        - name: SPRING_CONFIG_IMPORT
          value: configtree:/etc/k8t/
  volumes:
    - name: myapp-conf
      configMap:
        name: myapp-conf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM