Add support for OS package manager like apt and apk in Dockerfiles
While your docker support is great to keep the FROM directive updated, it could be enhanced by including support for the OS package managers (like APT for Debian and derivatives, APK for Alpine...).
With this addition we could completely rely on dependabot to keep our Docker images updated, instead of having to keep that manually updated.
Example 1 (APT on Ubuntu):
apt-get install apache2=2.2.20-1ubuntu1 \
apache2.2-common=2.2.20-1ubuntu1 \
apache2.2-bin=2.2.20-1ubuntu1 \
apache2-mpm-worker=2.2.20-1ubuntu1
Example 2 (APK on Alpine):
apk add packagename=1.2.3-suffix
We could even get fancy and support version constraints, like >=.
I'm really keen to support this too - we've got several cases internally that'd benefit from this so we've wanted it for a while, too!
It wouldn't be super hard to implement, but it's not a tiny project either. We'd need to parse shell expressions so we could handle things like RUN apt-get update && apt-get install -y foo=1.0.0 (far more complicated examples exist too...!), and we'd need to integrate with the various package registries, ideally detecting the distro and release by looking at the base image (recursively).
Unfortunately we won't have capacity to implement this in the near future. If you're really keen for support, we would accept a PR though :-)
The repo is this one, right? https://github.com/dependabot/dependabot-core
Yep!
This would be very helpful!
Would it be easier to code the update if we used files like pip and gem? For example an akp.txt file. Then use something like xargs to run apk add. That way they don't have to figure out how to parse the packages out of the Dockerfile.
I suppose the parser implementation complexity would be the same, but you'll have the overhead of having to load that file somehow when you build the docker image (since you have to install those packages, don't you?)
We ended up making a quick Python script to roll the pins: https://gist.github.com/mst-ableton/d0b80692571718fcb0a8f3984add9c03. As it uses Python it's not easily upstreamable, but the idea is to run apt-get update inside the container and parse the output of apt-get upgrade -s to see what it would have upgraded to. Because it's doing two docker builds, it may take a while to run. Hope this effort can jumpstart a Dependabot-native implementation in the future.
Been bashing my head against the wall with this one for https://github.com/ironPeakServices/iron-redis At one side you want to pin your package versions, but the other way you can't keep maintaining the package versions manually or whenever there is a security fix.
I've taken a look at what would be involved to make this a reality - I almost started a standalone project to do it, but having it part of Dependabot feels more appropriate, plus there's a better code structure already.
Questions/thoughts for any dependabot-core maintainers (@feelepxyz @jurre @greysteil ?):
- I can see value in reusing the Docker
FileFetcher, but having a separatepackage_managerused for different base OS'es, a la:
docker_alpinedocker_ubuntudocker_centos- etc etc
how would you prefer the file hierarchy to look here? extra top level directories for each
package_managerrespectively? or subdirectories within top leveldocker? maybedocker/lib/dependabot/docker_(alpine|ubuntu|centos)?
-
There might be some potential for shared/reusable logic in the various
FileParser's andFileUpdater's, maybe even a single sharedFileParser/FileUpdater, TBD. -
I think each
UpdateCheckerwill likely be unique, to talk to the different package repositories respectively for each OS. Things like Ubuntu PPA's and the equivalents for other OS'es will be interesting to deal with also... as these cannot be 100% inferred from the contents of the dependency file (Dockerfile) only? -
I can see a potential need to actually "run" the Docker image with a command to trawl/read the likes of:
/etc/os-release/etc/apt*/etc/yum*for "what package repositories need to be poked by theUpdateChecker, is there a facility available to do that?
At one side you want to pin your package versions, but the other way you can't keep maintaining the package versions manually or whenever there is a security fix.
@hazcod I think the core principal here from my standpoint, is for a Dockerfile to produce a deterministic image output. It's tricky, but hard versioning at the OS package level goes a long way towards that working (with the exception of a Linux distribution pulling the rug out from under you and 404'ing the repo URLs for a specific OS release).
3. I can see a potential need to actually "run" the Docker image with a command to trawl/read the likes of:
/etc/os-release/etc/apt*/etc/yum*for "what package repositories need to be poked by theUpdateChecker, is there a facility available to do that?
https://github.com/dependabot/dependabot-core#setup
To run all of Dependabot Core, you'll need Ruby, Python, PHP, Elixir, Node, Go, Elm, and Rust installed.
No current provision to have Docker installed or accessible as part of the list of helpers specified?
I don't maintain Dependabot anymore, but you're in safe hands with @feelepxyz and @jurre. I know they've been swamped in the last few weeks, though, and may be taking some well deserved time off over Christmas.
Appreciate you looking into this @CpuID. I just want to preface this with a note that I'm not sure if we will be able to timely review, merge and support such a contribution at this time.
We've paused accepting new ecosystems, and this patch might be of similar proportions.
Having said that, I'll try to answer some of your questions:
how would you prefer the file hierarchy to look here? extra top level directories for each package_manager respectively? or subdirectories within top level docker? maybe docker/lib/dependabot/docker_(alpine|ubuntu|centos)
I imagine that the implementations will be relatively similar, and it feels like it should be part of the docker package_manager.
What I imagine right now (without much context on this, so I may very well be wrong):
- We make it all part of the
dockerpackage_manager - We have separate UpdateCheckers for each OS we support:
docker/lib/dependabot/docker/update_checkers/alpine_update_checker.rb,docker/lib/dependabot/docker/update_checkers/ubuntu_update_checker.rbetc. This will have to be integrated in the mainUpdateChecker - It seems like we might be able to reuse/extend the
FileUpdaterand subsequent steps, as long as it's aware of how to update an OS package and handle the shell parsing etc. It might make sense to pull this out into its own class that we call from the existingFileUpdater.
It's hard to say what it should look like exactly without doing some more investigation though, and I would definitely re-evaluate once we have a better idea of how many parts of the codebase we can reuse and how much we end up having to change.
@jurre thanks for the response :)
I think your suggestion for using docker/lib/dependabot/docker/update_checkers/alpine_update_checker.rb etc makes sense, I'm happy with that filename hierarchy (depending on which class is sharded out respectively, TBD based on findings during implementation). Eg. could be docker/lib/dependabot/docker/file_parsers/alpine_file_parser.rb.
I'll see if I get free cycles to put something together, and see how far I get.
We aim to provide the best user experience possible for each of these, but we have found we've lacked the capacity – and in some cases the in-house expertise – to support new ecosystems in the last year.
@jurre hiring? :)
@jurre hiring? :)
We are! https://boards.greenhouse.io/github/jobs/2383025 https://boards.greenhouse.io/github/jobs/2384868
If dependencies were stored in a JSON file similar to package.json, jq and xargs can be used to generate the install command and update the versions:
apt.json
{
"nginx": "1.18.0-0ubuntu1",
"openssl": "1.1.1f-1ubuntu2.4",
"ca-certificates": "20210119~20.04.1"
}
Run in Dockerfile:
jq -r 'to_entries | .[] | .key + "=" + .value' apt.json | xargs apt-get install -y
An action can read the version to update the JSON:
apt-cache policy nginx | grep -oP '(?<=Candidate:\s)(.+)'
Here's a working example.
A script updates the latest version of packages in the JSON file: https://github.com/wildpeaks/docker-nginx/blob/main/docker/update_dependencies.sh
#!/bin/bash
JSON=$( cat dependencies.json )
for PACKAGE in $( echo $JSON | jq -r 'keys | .[]' ); do
VERSION=$( apt-cache policy "$PACKAGE" | grep -oP '(?<=Candidate:\s)(.+)' )
JSON=$( echo $JSON | jq '.[$package] = $version' --arg package $PACKAGE --arg version $VERSION )
done
echo $JSON | python -m json.tool > dependencies.json
A cron Action runs the update script and creates a matching pull request: https://github.com/wildpeaks/docker-nginx/blob/main/.github/workflows/dependencies.yml
# ...
- name: Update dependencies
working-directory: docker
run: |
sudo apt-get update
sh update_dependencies.sh
- name: Create PR
uses: peter-evans/create-pull-request@v3
with:
commit-message: "chore(deps): update dependencies.json"
branch: features/update-dependencies
title: Update APT packages
body: Updated dependencies.json
delete-branch: true
And the Dockerfile uses the JSON file to install pinned versions: https://github.com/wildpeaks/docker-nginx/blob/main/docker/Dockerfile#L7
# ...
COPY dependencies.json /tmp/dependencies.json
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
&& apt-get install -y --no-install-recommends jq \
&& jq -r 'to_entries | .[] | .key + "=" + .value' /tmp/dependencies.json | xargs apt-get install -y --no-install-recommends \
&& rm /tmp/dependencies.json
# ...
Hello! I was wondering if there was any ongoing effort or plan to get this implemented. This feature would be a huge help!
This would be useful for me. I had an issue where the Ubuntu repositories gave me a very old version of a package. Ive started pinning my package versions, but now I have increased maintenance overhead.
Here's a working example.
A script updates the latest version of packages in the JSON file: https://github.com/wildpeaks/docker-nginx/blob/main/docker/update_dependencies.sh
#!/bin/bash JSON=$( cat dependencies.json ) for PACKAGE in $( echo $JSON | jq -r 'keys | .[]' ); do VERSION=$( apt-cache policy "$PACKAGE" | grep -oP '(?<=Candidate:\s)(.+)' ) JSON=$( echo $JSON | jq '.[$package] = $version' --arg package $PACKAGE --arg version $VERSION ) done echo $JSON | python -m json.tool > dependencies.jsonA cron Action runs the update script and creates a matching pull request: https://github.com/wildpeaks/docker-nginx/blob/main/.github/workflows/dependencies.yml
# ... - name: Update dependencies working-directory: docker run: | sudo apt-get update sh update_dependencies.sh - name: Create PR uses: peter-evans/create-pull-request@v3 with: commit-message: "chore(deps): update dependencies.json" branch: features/update-dependencies title: Update APT packages body: Updated dependencies.json delete-branch: trueAnd the Dockerfile uses the JSON file to install pinned versions: https://github.com/wildpeaks/docker-nginx/blob/main/docker/Dockerfile#L7
# ... COPY dependencies.json /tmp/dependencies.json RUN DEBIAN_FRONTEND=noninteractive apt-get update \ && apt-get install -y --no-install-recommends jq \ && jq -r 'to_entries | .[] | .key + "=" + .value' /tmp/dependencies.json | xargs apt-get install -y --no-install-recommends \ && rm /tmp/dependencies.json # ...
That works fantastically! Thank you!
Did you ever figure out how to get it to work on non-Ubuntu? E.g. Alpine Docker builds? I saw that you have the repo docker-browser-sync without the dependency updates action.
That works fantastically! Thank you!
Glad it helps :)
Did you ever figure out how to get it to work on non-Ubuntu? E.g. Alpine Docker builds? I saw that you have the repo docker-browser-sync without the dependency updates action.
The browser-sync one didn't need it because it's a NPM dependency (so Dependabot is the one updating the JSON file).
As for Alpine, sorry I never tried but the main challenge would be to find an Alpine equivalent of the apt-cache policy command whereas the rest should be similar (afaik jq is also available on Alpine).