Image and container in docker after a new build - docker

I've just started with Docker and I have a question given the following scenario:
I build an image
I run such an image in a container
I notice that I've missed something in the Dockerfile and I build a new version of the image
My question is: does the container know about the updated image? If not, what is the right workflow? So far I do this: stop the container -> remove it -> create a new one with the same name from the latest image .... but I don't think that it is the right way to proceed.

does the container know about the updated image?
No, your container is still using everything in the image based on which it started.
what is the right workflow?
Stop your previous container
Run a new container using your newer image
Note: you could either docker rm your previous container or not to do so, but if you want to run your new container with the same container name, you have to remove the previous one.

It's enough to stop the running container and start it again. It should pick up the latest built version of the image.

Related

Updating container on VM. How?

I specified a docker image when creating a small VM. Because of this feature, I expected a fairly hands-off way of updating the container to the latest image, but I can't find any documentation on how to do that, or at least a method that works. What the documentation says is that updating the configuration will cause the container to be updated to the latest image and the VM will be stopped & restarted, but this doesn't happen.
I've only been able to update the container by using the Cloud shell from the container registry page. Am I missing a more obvious way to do this?
Docker Tags
The version of the image is specified in the tag.
If you want the most recent, use the latest tag.
Otherwise, a version can be specified.
Example:
fedora/httpd:version1.0 will grab fedora image with version1.0.
fedora/httpd:latest will grab the latest fedora image.
Check what versioning format your image is using, and specify a version when pulling the image.
Updating your container to use newest image
To do this, you likely need to just stop the container, specify the image you want to use, and run the new container.
The key here is to trigger a new pull from the registry. If you are using the latest tag, the latest image should be pulled from the registry. Your important data/configuration should all be made persistent through volume mounting, etc. So you should just be able to plug-and-play with this new image.
If you are looking for the simplest way, maybe try writing a script to stop your running container, pull the latest image, and run this image.
It is slightly challenging to give an exact answer on this issue because there a couple ways to go about it.
Documentation for Docker Pull and Docker Tag
Use Tag for image instead of latest.
use something like this
Image: name_of_imgae:1.0

How to catch changes in building docker image?

I am new to docker container. I met a problem about how to catch changes of my code. Some lines in my local style.css file have been changed. Then I built docker image again, but actually nothing changed when I browsed my app.
Here are some methods I found online and tried, but didn't work.
remove image and build again
--no-cache=true
add a comment in Dockerfile to make it different
docker system prune
--pull
(I also used git pull to get code on my cloud instance, these files were checked to be the latest.)
I know little about docker mechanism, could anyone tell me what the problem is?
Extra info I found:
After stopping container and removing image, I restart my instance, then build image, run container again. Only in this way, can I catch those changes. Does anyone know the problem?
Many thanks!
There appears to be a disconnect on the difference between a container and an image. The container is the running instance of your application. It is based on an image that you have already built. That image has a tag for easier referencing, but the real reference to the image is a sha256 hash and building a new image will change the hash that the tag points to without impacting any of your running containers.
Therefore, the workflow to update your running application in docker is to:
Build a new image
Stop the running container
Start a new container pointing to that image
Cleanup any old images or stopped containers
If you are using docker-compose, it automates the middle two steps with a docker-compose up command, and that even deletes the old container. Most users keep a few copies of older images to allow easy rollback.

updating docker image given changes to local filesystem

I am trying to work out how I can update an existing image when I make changes to the local filesystem that was used to create the docker image. I thought that I could use docker commits to do that, but it seems that that allows you to change the image when there are changes to the filesystem on a running image?
/app.py
build from file system
sudo docker build -t app
now there are local changes to /app.py. How do I change the image app to reflect the changes to /app.py? right now I'm having to delete the old image and then create a new one.
sudo docker rmi app
sudo docker build -t app
any help is appreciated!
First of all, there's no running image, only running container. Image is something deliverable in Docker way, you build your image and then start a container from it.
To your problem, I think you have mentioned your options:
Rebuild your image
Go inside a running container, make changes and docker commit it back. Personally I only use this way to fix a tiny problem or make a hotfix to my image if docker build takes a really long time.
Docker uses union FS with copy on write to build image, which means if you want make a change to an image, you can't change it in-place, it'll create extra layer(s) to reflect your change(s), it'll just use the same image name in some cases. And from the perspective of delivery, I think it's totally OK to build a new image (with different tag) for each release, or even it should be done this way, that's why you have an Dockerfile, and images are not only something you start your container, they're actually versioned delivery artifacts and you can roll back to any version if you want/need. So I think your current solution is OK.
A few more words here: for local development and test, you can just mount your /app.py as a volume to your container when you start it, something like docker run -v /path/to/host/app.py:/path/to/container/app.py your_base_image_to_run_app, then anything you changed on your local FS to app.py, it'll reflect to the container. When you finish your job, build a new image.
As per your current design Solution is to create new image and assign same tag.
Best solution is expose environment variables from docker image and use those variable to update app.py so that you don't need to change image every time.Only one image is sufficient.

Is it possible to upgrade image of an existing container?

I want to upgrade the image where an existing container runs. After executing docker pull, I got the latest image. But the container still run on the older image. Is it possible to make an existing container to run on latest image? Thanks.
It's not possible at this time, but there's an open feature request to support this use case.

Docker updating image along when dockerfile changes

I'm playing with docker by creating a Dockerfile with some nodejs instructions. Right now, every time I make changes to the dockerfile I recreate the image by running sudo docker build -t nodejstest . in my project folder however, this creates a new image each time and swallows my ssd pretty soon.
Is there a way I can update an existing image when I change the dockerfile or I'm forced to create a new one each time I make changes to the file?
Sorry if it's a dumb question
Docker build support caching as long as there is no ADD instruction. If you are actively developing and changing files, only what is after the ADD will be rebuilt.
Since 0.6.2 (scheduled today), you can do docker build --rm . and it will remove the temporary containers. It will keep the images though.
In order to remove the orphan images, you can check them out with docker images, and perform a docker rmi <id> on one of them. As of now, there is an auto-prune and all untagged images (orphans, previous builds) will be removed.
According to this best practices guide if you keep the first lines of your dockerfile the same it'll also cache them and reuse the same images for future builds
During development, it makes less sense to re-build a whole container for every commit. Later, you can automate building a Docker container with your latest code as part of your QA/deployment process.
Basically, you can choose to make a minimal container that pulls in code (using git when starting the container, or using -v /home/myuser/mynode:/home/myuser/mynode with ENTRYPOINT to run node).
See my answer to this question:
Docker rails app and git

Resources