So maybe you’re not using Alpine Linux solely as a Docker container, and you want to have a useful set of documentation installed. First things first: when you install packages, make sure you grab the -doc package as well (duh). So for instance, if you want to install rsync, make sure you run apk add rsync rsync-doc. (Alternately, using shell expansion: apk add rsync{,-doc}.)

But how to install any missing documentation packages for packages that have already been installed? When trying to figure this out for myself, I found this article, and it did a good job of doing what I wanted, but–as mentioned in the article–it can be slow. Even the solution at the end of the article took 55 seconds to enumerate 45 packages. (I think this is due to running apk info on each and every package to see whether it has a -doc package or not, which takes just takes time.)

I worked on it a bit and came up with a quicker solution, which uses a single invocation of apk search with the full list of potential -doc packages:

$ apk list -I |    # get all installed packages...
    # for all non-doc packages, strip off the version and append -doc...
    awk '!/(-doc)/ { print gensub(/^(.*)-[[:digit:]].*/, "\\1-doc", "g", $1); }' |
    # ...get apk to tell us what is real and what isn't...
    xargs apk search |
    # ...strip off the version yet again (apk add doesn't like it)...
    awk '{ print gensub(/^(.*)-[[:digit:]].*/, "\\1", "g", $1); }' |
    # ...and then actually install them.
    xargs -rt sudo apk add -i

Doing it this way took only four seconds (down from 55!) and actually caught eleven more packages that had docs than the other method. Not bad!