1

I have made a pod, and it has only one container, I've been able to connect ot it by using kubectl exec -it -c <container_name> <pod_name> bash

And I've installed and done several stuff on it.

Now I wish to create an image out of that container to record all those changes in a single image.

I am using docker as the driver. And typically, if this container would have been just any other regular docker container (not managed by k8s), I would have just made a docker commit <container_name>

Is there a way to something similar but considering the container is managed inside a pod of k8s?

1 Answer 1

0

Now I wish to create an image out of that container to record all those changes in a single image.

That is generally the wrong way to go about it: you should create your image using a Dockerfile, and then deploy it to Kubernetes to test it. You should treat your running container as "read-only" and only makes changes by updating your Dockerfile (or similar) and re-building the image.

Doing things this way ensures that you are able to re-create the image, and allows you to use version control on the Dockerfile so that you have a record of what changes you made and why.

Is there a way to something similar but considering the container is managed inside a pod of k8s?

The only way to create an image* from the running container would be to use docker commit. There is no Kubernetes mechanism for performing this action. You'll need to log into the Kubernetes node on which your container is running and run docker commit there, and then potentially docker push the image to a repository if you intend on running it elsewhere.


*: That's a bit of a simplification. E.g., you could use something like tar to export the filesystem, and once you have that there are various way to unpack it into an image.

3
  • Yeah I know dockerfile would be better, but I was just looking for a quick workarround to get all that I have done without having to do Dockerfile for saving time. It's not something that will be changing in the future, it was just a one-time thing. Either way, when you mention to run docker commit on the kubernetes node, to what would I commit to? the pod is not a container that I could commit right? Apr 10, 2023 at 17:08
  • You said in your question that you're using the docker driver for Kubernetes. That means that each container in your pod is an actual Docker container. I don't use Docker in my k8s environments, but you should be able to run docker ps on the appropriate node and get a list of containers; from there you'll probably need to match up the container id with the pod somehow.
    – larsks
    Apr 10, 2023 at 17:54
  • well I have made a deployment of 3 pods, each with 2 containers. I wen I run docker ps on the managing noe (I am using minikube so it's all on my local pc really) I get only 1 kubernetes container. And even if I delete the deployment and have no deployments & pods , that container is still there. Should I have seen 1 k8s container per pod-container there was? Apr 13, 2023 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.