Skip to content

Fix wget/tar error on Alpine Linux

Adam Cohen requested to merge fix-command-error-on-alpine-linux into master

This is a fix for a strange error that I've encountered when attempting to build an alpine linux based container:

$ git clone git@gitlab.com:gitlab-org/security-products/analyzers/gemnasium.git && cd gemnasium
$ analyzer-debug qa/fixtures/ registry.gitlab.com/security-products/gemnasium:3

 => [internal] load build definition from Dockerfile.dev                                                                                                                        0.0s
 => => transferring dockerfile: 786B                                                                                                                                            0.0s
 => [internal] load .dockerignore                                                                                                                                               0.0s
 => => transferring context: 2B                                                                                                                                                 0.0s
 => [internal] load metadata for registry.gitlab.com/security-products/gemnasium:3                                                                                              0.0s
 => [internal] load build context                                                                                                                                               0.0s
 => => transferring context: 37B                                                                                                                                                0.0s
 => [1/5] FROM registry.gitlab.com/security-products/gemnasium:3                                                                                                                0.0s
 => CACHED [2/5] COPY ./distro-setup.sh .                                                                                                                                       0.0s
 => CACHED [3/5] RUN ./distro-setup.sh                                                                                                                                          0.0s
 => ERROR [4/5] RUN mkdir -p /go &&       GO_DOWNLOAD=$(uname -a | grep -q "x86_64" && echo "go1.17.6.linux-amd64.tar.gz" || echo "go1.17.6.linux-arm64.tar.gz") &&       wge  16.2s
------
 > [4/5] RUN mkdir -p /go &&       GO_DOWNLOAD=$(uname -a | grep -q "x86_64" && echo "go1.17.6.linux-amd64.tar.gz" || echo "go1.17.6.linux-arm64.tar.gz") &&       wget https://golang.org/dl/$GO_DOWNLOAD -O /tmp/$GO_DOWNLOAD &&       tar -C /usr/local -xzf /tmp/$GO_DOWNLOAD:
#8 0.302 Connecting to golang.org (172.217.167.113:443)
#8 0.666 Connecting to go.dev (216.239.32.21:443)
#8 1.258 Connecting to dl.google.com (172.217.24.46:443)
#8 1.560 saving to '/tmp/go1.17.6.linux-amd64.tar.gz'
#8 1.677 go1.17.6.linux-amd64   0% |                                | 1152k  0:01:53 ETA
#8 2.677 go1.17.6.linux-amd64  10% |***                             | 14.0M  0:00:16 ETA
#8 3.676 go1.17.6.linux-amd64  21% |******                          | 27.0M  0:00:11 ETA
#8 4.676 go1.17.6.linux-amd64  31% |*********                       | 40.0M  0:00:08 ETA
#8 5.676 go1.17.6.linux-amd64  41% |*************                   | 53.0M  0:00:07 ETA
#8 6.677 go1.17.6.linux-amd64  50% |****************                | 65.2M  0:00:05 ETA
#8 7.677 go1.17.6.linux-amd64  59% |*******************             | 76.7M  0:00:04 ETA
#8 8.677 go1.17.6.linux-amd64  69% |**********************          | 89.6M  0:00:03 ETA
#8 9.677 go1.17.6.linux-amd64  79% |*************************       |  102M  0:00:02 ETA
#8 10.68 go1.17.6.linux-amd64  89% |****************************    |  114M  0:00:01 ETA
#8 11.66 go1.17.6.linux-amd64  98% |******************************* |  127M  0:00:00 ETA
#8 11.76 go1.17.6.linux-amd64 100% |********************************|  128M  0:00:00 ETA
#8 11.76 '/tmp/go1.17.6.linux-amd64.tar.gz' saved
------
executor failed running [/bin/sh -c mkdir -p /go &&       GO_DOWNLOAD=$(uname -a | grep -q "x86_64" && echo "$GO_VERSION.linux-amd64.tar.gz" || echo "$GO_VERSION.linux-arm64.tar.gz") &&       wget https://golang.org/dl/$GO_DOWNLOAD -O /tmp/$GO_DOWNLOAD &&       tar -C /usr/local -xzf /tmp/$GO_DOWNLOAD]: exit code: 1

It seems that the issue is caused by the following lines in the Dockerfile.dev file:

wget https://golang.org/dl/$GO_DOWNLOAD -O /tmp/$GO_DOWNLOAD && \
tar -C /usr/local -xzf /tmp/$GO_DOWNLOAD

The issue can be fixed in any of the following ways:

  1. Insert another command in between the wget and tar commands:

    wget https://golang.org/dl/$GO_DOWNLOAD -O /tmp/$GO_DOWNLOAD && \
    ls -al && \
    tar -C /usr/local -xzf /tmp/$GO_DOWNLOAD

    or

    wget https://golang.org/dl/$GO_DOWNLOAD -O /tmp/$GO_DOWNLOAD && \
    sleep 1 && \
    tar -C /usr/local -xzf /tmp/$GO_DOWNLOAD
  2. Install a non-busybox version of tar:

    RUN apk add tar && mkdir -p /go && \
          GO_DOWNLOAD=$(uname -a | grep -q "x86_64" && echo "$GO_VERSION.linux-amd64.tar.gz" || echo "$GO_VERSION.linux-arm64.tar.gz") && \
          wget https://golang.org/dl/$GO_DOWNLOAD -O /tmp/$GO_DOWNLOAD && \
          tar -vC /usr/local -xzf /tmp/$GO_DOWNLOAD

I've tried debugging the issue, but I can't figure out why it's happening. It seems like a race condition, like the wget command is still writing the file to disk while the tar command is trying to extract it.

Edited by Adam Cohen

Merge request reports