dockerdebianx86-64cpu-architectureapple-m1

Check architecture in dockerfile to get amd/arm


We're working with Windows and Mac M1 machines to develop locally using Docker and need to fetch and install a .deb package within our docker environment.

The package needs amd64/arm64 depending on the architecture being used.

Is there a way to determine this in the docker file i.e.

if xyz === 'arm64'
    RUN wget http://...../arm64.deb
else 
    RUN wget http://...../amd64.deb

Solution

  • First you need to check if there is no other (easier) way to do it with the package manager.

    You can use the arch command to get the architecture (equivalent to uname -m). The problem is that it does not return the values you expect (aarch64 instead of arm64 and x86_64 instead of amd64). So you have to convert it, I have done it through a sed.

    RUN arch=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) && \
        wget "http://...../${arch}.deb"
    

    Note: You should add the following code before running this command to prevent unexpected behavior in case of failure in the piping chain. See Hadolint DL4006 for more information.

    SHELL ["/bin/bash", "-o", "pipefail", "-c"]