How to imperatively change a container's image

2 min read | by Jordi Prats

To change a container's image we can:

  • Update the yaml definition and push it using kubectl apply
  • Use kubectl edit to update it using a definition fetched from the Kubernetes cluster itself
  • To use kubectl set image to set the new image

Let's check how to use kubectl set image

To update a container's image on a deployment we will have to specify first the deployment, for example deployment/nginx-deployment and then tell use the container's name to specify the new image. For example, on the following command we are updating the container named nginx to use the image nginx:1.16.1:

kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

Besides being able to update it on a deployment we can also do it for pod, replicationcontroller , daemonset and replicaset

We can also use kubectl set image to update container image in a yaml file without applying it:

kubectl set image -f definition.yaml nginx=nginx:1.16.1 --local -o yaml > definition-updated.yaml

Posted on 14/05/2021