[Fix] exec /usr/bin/sh: exec Format Error

The Error ‘exec /usr/bin/sh: exec format error’, is an error that is seen when you are working on docker and are trying to push a custom image to Docker hub.

Given below is the snippet of the error ‘exec /usr/bin/sh: exec format error’ you might see when you try to run it using CI/CD:

exec /usr/bin/sh: exec format error

The error, ‘exec /usr/bin/sh: exec format error’ is seen because while your runner uses a different architecture, you built this image for arm64/v8.

To check your architecture you can run the command mentioned below:

docker image inspect rethkevin/rf:v1

Then you will see the output given below:

...
        "Architecture": "arm64",
        "Variant": "v8",
        "Os": "linux",
...


To fix the error, exec /usr/bin/sh: exec format error, ensure that the architecture of your image matches the architecture of your runner, try building and pushing your image from your GitLab CI runner and error would be fixed.

As an alternative, docker buildx can be used to build for many architectures.

Another option is to use an ARM-based GitLab runner that can execute the image for the architecture you used to build it. You can find the documentation form the link mentioned here.

exec /usr/bin/sh: exec Format Error

Steps to Fix the Error exec /usr/bin/sh: exec format error:

Method 1: Matching Your Architecture

One of the shortest and the most effective way to fix the error is to ensure that the architecture of your image matches the architecture of your runner, try building and pushing your image from your GitLab CI runner.

This should help you fix and understand the error, ‘exec /usr/bin/sh: exec format error’.

Method 2: Building Multiple Architectures

If the above solution does not fix the error as an alternative, docker buildx can be used to build for many architectures.

Another option is to use an ARM-based GitLab runner that can execute the image for the architecture you used to build it. You can find the documentation for this here.

This should help you fix and understand the error, ‘exec /usr/bin/sh: exec format error’.

Conclusion

Make sure that the architecture of your image matches the architecture of your runner in order to quickly and easily resolve the “exec /usr/bin/sh: exec format error” error.

To do this, try building and pushing your image from your GitLab CI runner. also as an alternative, docker buildx command can be used to build many architectures.