0

I have one of my spec files like this

          containers:
            - name: webserver
              image: PRIVATE_REPO/project/projectname:${TAG}
              imagePullPolicy: Always
              ports:
                - containerPort: 8080
                  name: http

I have the TAG value defined in an env file. When I did this with docker-compose it worked on docker-compose. I don't think I can do this for Kubernetes, I was wondering if there is a way to do this.

1 Answers1

2

You cannot do this the same way in Kubernetes.

Docker Compose permits transformations to be applied to its YAML config files so that you can, for example, replace environment variables with values as you were doing. Essentially, the YAML file you provide to Docker Compose is a template that the tool transforms into the YAML that it uses to deploy containers.

Kubernetes CLI (kubectl) does not support such transformations. You will need to ensure that the YAML files you provide to kubectl are a literal representation of the YAML that you want to be applied to the cluster.

There are various ways to address this "templating problem" with Kubernetes but you will need to use additional tools to do this.

Simplistically, you can use a Linux tool like sed to replace variables with their values. Because you'll likely be using YAML configs, you can use a tool like yq that is designed to process YAML and, because yq understands YAML structure, the tool is better suited than e.g. sed.

Because this is a common need with Kubernetes, there are Kubernetes-specific tools for templating configurations files. See Helm, Kustomize, Jsonnet and CUE among others.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88