dockergitlabgitlab-cigitlab-ci-runnerdockerhub

Docker : exec /usr/bin/sh: exec format error


I created a custom docker image and push it to docker hub but when I run it in CI/CD it gives me this error.

exec /usr/bin/sh: exec format error

Where :

Dockerfile

FROM ubuntu:20.04
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN apt-get install -y python3-pip
RUN pip3 install robotframework

.gitlab-ci.yml

robot-framework:
  image: rethkevin/rf:v1
  allow_failure: true
  script:
    - ls
    - pip3 --version

Output

Running with gitlab-runner 15.1.0 (76984217)
  on runner zgjy8gPC
Preparing the "docker" executor
Using Docker executor with image rethkevin/rf:v1 ...
Pulling docker image rethkevin/rf:v1 ...
Using docker image sha256:d2db066f04bd0c04f69db1622cd73b2fc2e78a5d95a68445618fe54b87f1d31f for rethkevin/rf:v1 with digest rethkevin/rf@sha256:58a500afcbd75ba477aa3076955967cebf66e2f69d4a5c1cca23d69f6775bf6a ...
Preparing environment
00:01
Running on runner-zgjy8gpc-project-1049-concurrent-0 via 1c8189df1d47...
Getting source from Git repository
00:01
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /builds/reth.bagares/test-rf/.git/
Checking out 339458a3 as main...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
Using docker image sha256:d2db066f04bd0c04f69db1622cd73b2fc2e78a5d95a68445618fe54b87f1d31f for rethkevin/rf:v1 with digest rethkevin/rf@sha256:58a500afcbd75ba477aa3076955967cebf66e2f69d4a5c1cca23d69f6775bf6a ...
exec /usr/bin/sh: exec format error
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1

any thoughts on this to resolve the error?


Solution

  • The problem is that you built this image for arm64/v8 -- but your runner is using a different architecture.

    If you run:

    docker image inspect rethkevin/rf:v1
    

    You will see this in the output:

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

    Try building and pushing your image from your GitLab CI runner so the architecture of the image will match your runner's architecture.

    Alternatively, you can build for multiple architectures using docker buildx . Alternatively still, you could also run a GitLab runner on ARM architecture so that it can run the image for the architecture you built it on.


    With modern versions of docker, you may also explicitly control the platform docker uses. Docker will use platform emulation if the specified platform is different from your native platform.

    For example:

    Using the DOCKER_DEFAULT_PLATFORM environment variable:

    DOCKER_DEFAULT_PLATFORM="linux/amd64" docker build -t test .
    

    Using the --platform argument, either in the CLI or in your dockerfile:

    docker build --platform="linux/amd64" -t test .
    
    FROM --platform=linux/amd64 ubuntu:jammy
    

    Systems with docker desktop installed should already be able to do this. If your system is using docker without docker desktop, you may need to install the docker-buildx plugins explicitly.