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???
CodePudding user response:
- You do have to rebuild the image.
- It may have the same name, or a different name. If you give it the same name, the old image will still exist locally, but
docker images
will show<none>
as its name or tag.docker system prune
will clean up the old image. - You must run a new container against the new 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.
docker run --rm the-image
# prints 2
$EDITOR test.py
docker build -t the-image .
docker run --rm the-image
# prints 4
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.
docker run -d -p 8080:8080 --name the-container the-image
curl http://localhost:8080/test-result
# prints 2
$EDITOR test.py
docker build -t the-image .
curl http://localhost:8080/test-result
# still prints 2
docker stop the-container
docker rm the-container
docker run -d -p 8080:8080 --name the-container the-image
curl http://localhost:8080/test-result
# now prints 4
CodePudding user response:
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.