Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to define original image id and image name used in custom images #1978

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

aminfa
Copy link

@aminfa aminfa commented May 8, 2024

TL;DR: This PR allows one to add two labels to containers that define which original image was used as the base of the image being run.

The problem

Sometimes it is necessary to build a custom docker image that only slightly changes a base image. For example:

FROM nginx:1.25.4
COPY nginx.conf /etc/nginx/nginx.conf

When running this custom image, watchtower cannot figure out that the base nginx image is stale, as it instead only checks if our custom nginx image is stale:

Could not do a head request for "custom-nginx:latest", falling back to regular pull.
Reason: registry responded to head request with "401 Unauthorized", auth: "Bearer realm=\"https://auth.docker.io/token\",service=\"registry.docker.io\",scope=\"repository:library/nginx-as-container-rootnginx:pull\",error=\"insufficient_scope\""
Unable to update container "custom-nginx-1": Error response from daemon: pull access denied for custom-nginx, repository does not exist or may require 'docker login': denied: requested access to the resource is denied. Proceeding to next.

The solution

With this PR, it is possible to add two labels to the container that overwrites the image ID and image name used by watchtower to decide whether the image is stale:

docker run -d \
   -l com.centurylinklabs.watchtower.original-image=nginx \
   -l com.centurylinklabs.watchtower.original-image-id=$(docker inspect nginx:1.25.4 | jq '.[0]'.Id) \
   -l com.centurylinklabs.watchtower.monitor-only=1 \
    custom_nginx

Now, watchtower reports a new nginx version:

$ ./watchtower --run-once
INFO[0001] Watchtower v0.0.0-unknown                    
INFO[0001] Using no notifications                       
INFO[0001] Checking all containers (except explicitly disabled with label) 
INFO[0001] Running a one time update.                   
INFO[0003] Original image id for nginx:latest found: ("sha256:c613f16b6642) 
INFO[0003] Found new nginx:latest image (1d668e06f1e5)

Current limitations

Currently automatic updating of the images will break the containers as it will replace the container using images of the original base image. Here it would start nginx:1.25.5 instead of a custom version of it.
Thus, it is necessary to set monitor-only=1.

Further work

I haven't done: "Tests that verify the code your contributing" and "Updates to the documentation" because I wanted to wait and see if this feature is even desired.

Feel free to reject the PR if the feature does not fit within the scope of this project.

@aminfa aminfa requested a review from simskij as a code owner May 8, 2024 20:15
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulations on opening your first pull request! We'll get back to you as soon as possible. In the meantime, please make sure you've updated the documentation to reflect your changes and have added test automation as needed. Thanks! 🙏🏼

@aminfa aminfa changed the title Add ability to define original image id and image name used in custom images feate: add ability to define original image id and image name used in custom images May 9, 2024
@aminfa aminfa changed the title feate: add ability to define original image id and image name used in custom images feat: add ability to define original image id and image name used in custom images May 9, 2024
@kondas
Copy link

kondas commented Jul 11, 2024

+1, this is a helpful feature for myself, and others who are using watchtower for monitoring only, and have custom local built images.

One very useful addition would be the possibility to have the ID of the base image optional. I dont really know if its a workable approach, but would perfectly fit my use case, when using fixed version tags in the base images, not rolling ones. In this case watchtower should gather the ID for the base image found in com.centurylinklabs.watchtower.original-image label.

This way the addition of the base image is straightforward in the local build context like this:

ARG RUNDECK_IMAGE
FROM ${RUNDECK_IMAGE:-rundeck/rundeck:SNAPSHOT}
<stuff>
ARG RUNDECK_IMAGE
LABEL com.centurylinklabs.watchtower.original-image=${RUNDECK_IMAGE:-rundeck/rundeck:SNAPSHOT}

docker-compose.yml

services:
  app:
    pull_policy: build
    build:
      context: ./build/
      args:
        RUNDECK_IMAGE: rundeck/rundeck:5.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants