I feel like I have a fundamental misunderstanding of how to work with docker.
Say that I have test.py
which prints 1+1. I then proceed to containerize it with a Dockerfile
that has FROM, CMD, etc…
After building the image, I can now run docker run ...
and it outputs 2, yay!
But now let say that on my local, I edit test.py
so that it prints 2+2. Do I now have to rebuild the image and make a new container? Can I use the same name as I did when I built the 1+1 version? What is this the correct way to do it???
2
Answers
Yes, you have to rebuild the image. You don’t have to rename the image – just use the old name. And you must not delete the old image.
When you rerun your container, then it will use the new version. Still running containers are not affected, you have to stop them.
docker images
will show<none>
as its name or tag.docker system prune
will clean up the old image.If the container just runs a one-off test, you might delete the old container before building a new image, or use the
docker run --rm
option to have the container delete itself.But if it’s a long-running container, you will need to stop and recreate it. The container will keep running the old code until you recreate it, and it is safe to run the old container while you build the new one.