Search code examples
dockerdockerfilealpine-linuxalpine-package-keeper

Dockerfile alpine apk add using ARG to pin version fails


I have a docker file that as follows:

ARG ALPINE_VERSION=3.16.2
ARG GLAB_VERSION="1.22.0"
ARG JQ_VERSION="1.6"

FROM alpine:$ALPINE_VERSION

RUN apk add --no-cache "glab~=${GLAB_VERSION}" "jq~=${JQ_VERSION}"

ENTRYPOINT [ "glab" ]

When I try to build the image, I get an error because it is unable to resolve the version of glab:

docker build -t gitlab/glab:latest .
[+] Building 6.6s (5/5) FINISHED                                                                                                                                                                                                                        
 => [internal] load build definition from Dockerfile                                                                                                                                                                                               0.0s
 => => transferring dockerfile: 258B                                                                                                                                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                                                                                    0.0s
 => [internal] load metadata for docker.io/library/alpine:3.16.2                                                                                                                                                                                   2.0s
 => CACHED [1/2] FROM docker.io/library/alpine:3.16.2@sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad                                                                                                                      0.0s
 => ERROR [2/2] RUN apk add --no-cache "glab~=${GLAB_VERSION}" "jq~=${JQ_VERSION}"                                                                                                                                                                 4.4s
------                                                                                                                                                                                                                                                  
 > [2/2] RUN apk add --no-cache "glab~=${GLAB_VERSION}" "jq~=${JQ_VERSION}":                                                                                                                                                                            
#5 1.009 fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/x86_64/APKINDEX.tar.gz                                                                                                                                                                  
#5 1.880 fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/x86_64/APKINDEX.tar.gz
#5 4.227 ERROR: 'glab~=' is not a valid world dependency, format is name(@tag)([<>~=]version)
------
executor failed running [/bin/sh -c apk add --no-cache "glab~=${GLAB_VERSION}" "jq~=${JQ_VERSION}"]: exit code: 99

If I update the Dockerfile to use hardcoded values:

RUN apk add --no-cache "glab~=1.22.0" "jq~=1.6"

it works:

docker build -t gitlab/glab:latest .
[+] Building 17.5s (6/6) FINISHED                                                                                                                                                                                                                       
 => [internal] load build definition from Dockerfile                                                                                                                                                                                               0.0s
 => => transferring dockerfile: 239B                                                                                                                                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                                                                                    0.0s
 => [internal] load metadata for docker.io/library/alpine:3.16.2                                                                                                                                                                                   0.6s
 => CACHED [1/2] FROM docker.io/library/alpine:3.16.2@sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad                                                                                                                      0.0s
 => [2/2] RUN apk add --no-cache "glab~=1.22.0" "jq~=1.6"                                                                                                                                                                                         16.6s
 => exporting to image                                                                                                                                                                                                                             0.2s 
 => => exporting layers                                                                                                                                                                                                                            0.2s 
 => => writing image sha256:46bccd18a999a696ef9e80fb75983a6b5655a774b88b1412b55707cb152823e5                                                                                                                                                       0.0s 
 => => naming to docker.io/gitlab/glab:latest                                                                                                                                                                                                      0.0s 
                                                                                                                                                                                                                                                        
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them              

Is it possible to use ARG to set the versions somehow?


Solution

  • The issue was becuase I was specifying the GLAB_VERSION and JQ_VERSION before the FROM ... instruction.

    Updating the Dockerfile as follows works:

    ARG ALPINE_VERSION=3.16.2
    FROM alpine:$ALPINE_VERSION
    
    ARG GLAB_VERSION=1.22.0
    ARG JQ_VERSION="1.6"
    
    RUN apk add --no-cache "glab~=${GLAB_VERSION}" "jq~=${JQ_VERSION}"
    
    ENTRYPOINT [ "glab" ]