Is it possible to upgrade image of an existing container? - docker

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.

Related

How do I get the last push of an image with the x.x tag if I already have an old push of an x.x image?

In 2019, I made a pull image of Python 3.6. After that, I was sure that the image was self-updating (I did not use it actively, I just hoped that the latest pushes themselves were pulled from the repository or something like that), but I was surprised when I accidentally noticed the download/creation date is 2019.
Q: How does image pull work? Are there flags so that the layer hash/its relevance* is checked every time the image is built? Perhaps there is a way to set this check through the docker daemon config file? Or do I have to delete the base image every time to get a new image?
What I want: So that every time I build my images, the base image is checked for the last push (publication of image) in the docker hub repository.
Note: I'm talking about images with an identical tag. Also, I'm not afraid to re-build my images, there is no purpose to preserve them.
Thanks.
You need to explicitly docker pull the image to get updates. For your custom images, there are docker build --pull and docker-compose build --pull options that will pull the base image (though there is not a "pull" option for docker-compose up --build).
Without this, Docker will never check for updates for an image it already has. If your Dockerfile starts FROM python:3.6 and you already have a local image with that name and tag, Docker just uses it without contacting Docker Hub. If you don't have it then Docker will pull it, once, and then you'll have it locally.
The other thing to watch for is that the updates do eventually stop. If you look at the Docker Hub python image page you'll notice that there are no longer rebuilds for Python 3.5. If you pin to a very specific patch version, the automated builds generally only build the latest patch version for each supported minor version; if your image is FROM python:3.6.11 it will never get updates because 3.6.12 is the latest 3.6.x version.

How to apply a security patch to an existing docker image?

IF there is a docker image using a particular base image is running as a container and there is a new security upgrade for the base image. What is the best practice to apply that security patch to the docker image.
Also how to know if there is a security patch available for the base image .
Let's say that you have a Dockerfile that is based on an image called "Base:latest" and you've built an image called "MyImage:latest:latest"
If "Base:latest" has updated with security updates, you need to rebuild your "MyImage:latest".
Containers are image instances, so if you need the security updates that need to be reflected in a container, the container should be re-created based on the "MyImage:latest" image.
Notice that you wouldn't want to use the "latest" tag for base images in production, because you won't be able to reproduce the same deployed environment, so the best practice is to use a specific version tag like "1.0". If an update is available, you'll need to updated your Dockerfile from "Base:1.0" to "Base:1.1".
So if your image is based on another image and you want to run security updates without waiting for a new and updated version of the base image, you can run a security update command in your Dockerfile and make sure to rebuild your image occasionally and recreate the container.
You could probably automate this process using tools like Watchtower by automatically rebuild your image on a regular basis and then recreate your container.
Another option is to run automated updates in the container level, probably by using a script that runs every day, but you should take into consideration the impact on the running process load-wise (networking, cpu, etc.)

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

Image and container in docker after a new build

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.

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