How to make docker reset the image on remote server? - docker

On docker build from jenkins 1st docker images are created tagged properly as LATEST on remote server.
On rebuild it supposed to overwrite docker image on server. But it didn't.
Virtually it creates new docker images with no-tag repository and no-tag tag. And avoid using predefined domain name for image.
So creates a new because it's supposed create NEW image because they are totally different.
Is there any way to avoid just deleting image directly from the remote server? But to update image docker on same tag(domain) name?
Any ideas on workaround? How to avoid making new docker images with static unchanged TAG name from jenkins build.
Because it eats a lot of memory space on the moment when run on cron.
How can I overwrite docker image or make a facade that it's overwrited?

You can delete no-tag images after build image from jenkins by: docker rmi $(docker images -f "dangling=true" -q)

Related

Check if local docker image latest

In my use case I always fetch the image tagged with "latest" tag. This "latest" tag gets updated regularly. So even if the latest tag image is updated on registry, the "docker run" command does not update it on local host. This is expected behavior as the "latest" image exists on local host.
But I want to make sure that if the "latest" image on local host and registry are different then it should pull the latest image from registry.
Is there any way to achieve this?
You can manually docker pull the image before you run it. This is fairly inexpensive, especially if the image hasn't changed. You can do it while the old container is still running to minimize downtime.
docker pull the-image
docker stop the-container
docker rm the-container
docker run -d ... --name the-container the-image
In an automated environment you might consider avoiding the latest tag and other similar fixed strings due to exactly this ambiguity. In Kubernetes, for example, the default behavior is to reuse a local image that has some name, which can result in different nodes running different latest images. If you label your images with a date stamp or source-control ID or something else such that every image has a unique tag, you can just use that tag.
Finding the tag value can be problematic outside the context of a continuous-deployment system; Docker doesn't have any built-in way to find the most recent tag for an image.
# docker pull the-image:20220704 # optional
docker stop the-container
docker rm the-container
docker run -d ... --name the-container the-image:20220704
docker rmi the-image:20220630
One notable advantage of this last approach is that it's very easy to go back to an earlier build if today's build happens to be broken; just switch the image tag back a build or two.

Smart docker cleaning

We are using docker to build and push images to repositories.
Is there a 'smart' way to clean/remove docker objects (images, layers, volumes etc.) but not remove cache files (we need to preserve cache so our builds takes as low time as possible)?
We are already:
removing images that were pushed to repo (couse we do not need them on building machine) - docker rmi <already_pushed_image>
Is there something more we can do? We could also have scheduled job with docker rmi $(docker images -q -f dangling=true), but that would also remove cache from our builds?

What are Dangling Images in Docker/How they created/How to remove them

How do dangling images get created? Whenever I tried to create them it is not happening.
I had used the same concept as mentioned in docker docs. If my DockerFile contains FROM alphine:3.4 then I build the image as docker build -t image1 .
After some time I update it as FROM alphine:3.5 and again building it as docker build -t image1 .
But it is showing me three images no dangling one??
What are Dangling Images in Docker?
Dangling Images are temporary images that are created at the time of building the images in docker server. Since images are build on layers of commits.
How they created?
There are created automatically. You can see them using docker images -f dangling=true
How to remove them?
You can do system purge to remove danggling images like this docker images purge.
If you really want to create a dangling image, simply interrupt the build process before it's finished.
Honestly don't understand why you want to though; they are a by-product of a failed build, that's all.

How to load updated docker image onto other machine

I have 2 hosts running the same docker customized image. I have modified the image on host 1 and saved the image to a custom.tar. If I take that image and load it onto host 2 will it just update or should I remove the old docker image first?
There are 2 ways to do that with repository and without repository using load and save.
With repository below are the steps.
Log in on Docker Hub
Click on Create Repository.
Choose a name and a description for your repository and click
Create.
Log into the Docker Hub from the command line
docker login --username=yourhubusername --email=youremail#company.com
tag your image
docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]
Push your image to the repository you created
docker push <hub-user>/<repo-name>:<tag>
Pull the image to host 2
docker pull <hub-user>/<repo-name>:<tag>
This will add the image to docker hub and available on internet and now you can pull this image to any system.
With this approach you can keep the same images with different tags on system. But if you don't need old images better to delete that to avoid junk.
Without docker hub.
This command will create tar bundle.
docker save [OPTIONS] IMAGE [IMAGE...]
example: docker save busybox > busybox.tar
Load an image from a tar archive or STDIN
docker load [OPTIONS]
example:docker load < busybox.tar.gz
Recommended: Docker hub or DTR approach easy to manage unless you have bandwidth issue in case your file is large.
Refer:
Docker Hub Repositories

Cached Docker image?

I created my own image and pushed it to my repo on docker hub. I deleted all the images on my local box with docker rmi -f ...... Now docker images shows an empty list.
But when I do docker run xxxx/yyyy:zzzz it doesn't pull from my remote repo and starts a container right away.
Is there any cache or something else? If so, what is the way to clean it all?
Thank you
I know this is old now but thought I'd share still.
Docker will contain all those old images in a Cache unless you specifically build them with --no-cache, to clear the cache down you can simply run docker system prune -a -f and it should clear everything down including the cache.
Note: this will clear everything down including containers.
You forced removal of the image with -f. Since you used -f I'm assuming that the normal rmi failed because containers based on that image already existed. What this does is just untag the image. The data still exists as a diff for the container.
If you do a docker ps -a you should see containers based on that image. If you start more containers based on that same previous ID, the image diff still exists so you don't need to download anything. But once you remove all those containers, the diff will disappear and the image will be gone.

Resources