Error while using "apk search" with Alpine. - blitterated/docker-dev-env GitHub Wiki

NOTE: This occured before switching to Arch and then Debian based images. It's saved here for posterity.

This is what the problem looks like:

bash-5.1# apk search ag
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/main: No such file or directory
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/community: No such file or directory

curling with headers shows that we're being redirected to the same URL, but with a trailing slash. See the location field in the header

λ ~: curl -i https://dl-cdn.alpinelinux.org/alpine/v3.14/main
HTTP/2 301
server: nginx
content-type: text/html
location: http://dl-cdn.alpinelinux.org/alpine/v3.14/main/
strict-transport-security: max-age=31536000
x-frame-options: DENY
x-content-type-options: nosniff
via: 1.1 varnish, 1.1 varnish
accept-ranges: bytes
date: Wed, 07 Jul 2021 03:00:03 GMT
age: 0
x-served-by: cache-lga21974-LGA, cache-dal21283-DAL
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1625626804.787410,VS0,VE113
content-length: 162

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

If we curl the same URL with the trailing slash, everything seems fine.

λ ~: curl -i https://dl-cdn.alpinelinux.org/alpine/v3.14/main/
HTTP/2 200
server: nginx
content-type: text/html
strict-transport-security: max-age=31536000
x-frame-options: DENY
x-content-type-options: nosniff
via: 1.1 varnish, 1.1 varnish
accept-ranges: bytes
date: Wed, 07 Jul 2021 02:59:52 GMT
age: 0
x-served-by: cache-lga21929-LGA, cache-dal21242-DAL
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1625626792.238700,VS0,VE114

<html>
<head><title>Index of /alpine/v3.14/main/</title></head>
<body>
<h1>Index of /alpine/v3.14/main/</h1><hr><pre><a href="../">../</a>
<a href="aarch64/">aarch64/</a>                                           30-Jun-2021 15:35                   -
<a href="armhf/">armhf/</a>                                             30-Jun-2021 15:35                   -
<a href="armv7/">armv7/</a>                                             30-Jun-2021 15:35                   -
<a href="mips64/">mips64/</a>                                            30-Jun-2021 15:35                   -
<a href="ppc64le/">ppc64le/</a>                                           05-Jul-2021 18:31                   -
<a href="s390x/">s390x/</a>                                             30-Jun-2021 15:35                   -
<a href="x86/">x86/</a>                                               30-Jun-2021 15:35                   -
<a href="x86_64/">x86_64/</a>                                            30-Jun-2021 15:35                   -
</pre><hr></body>
</html>

If I open /etc/apk/repositories and add the trailing slashes, then run apk search ag, there's a new error:

WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/main/: No such file or directory

Ultimately, it turns out that deleting apt lists and apk cache in the RUN command of your Dockerfile is a bad idea for an image you're iterating on. So don't do this:

RUN apk --update add \
      ... && \
    rm -rf /var/lib/apt/lists/* && \
    rm /var/cache/apk/*
⚠️ **GitHub.com Fallback** ⚠️