97

I am building the Dockerfile for python script which will run in minikube windows 10 system below is my Dockerfile

Building the docker using the below command docker build -t python-helloworld .

and loading that in minikube docker demon docker save python-helloworld | (eval $(minikube docker-env) && docker load)

Docker File

FROM python:3.7-alpine
#add user group and ass user to that group
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

#creates work dir   
WORKDIR /app

#copy python script to the container folder app
COPY helloworld.py /app/helloworld.py

#user is appuser
USER appuser

ENTRYPOINT  ["python", "/app/helloworld.py"]

pythoncronjob.yml file (cron job file)

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: python-helloworld
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      backoffLimit: 5
      template:
        spec:
          containers:
          - name: python-helloworld
            image: python-helloworld
            imagePullPolicy: IfNotPresent
            command: [/app/helloworld.py]
          restartPolicy: OnFailure

Below is the command to run this Kubernetes job kubectl create -f pythoncronjob.yml

But getting the below error job is not running scuessfully but when u ran the Dockerfile alone its work fine

standard_init_linux.go:211: exec user process caused "exec format error"

LinPy
  • 16,987
  • 4
  • 43
  • 57
Pandit Biradar
  • 1,777
  • 3
  • 20
  • 35

8 Answers8

186

This can also happen when your host machine has a different architecture from your guest container image.

E.g. running an arm container on a host with x86-64 architecture

Rufus
  • 5,111
  • 4
  • 28
  • 45
  • 7
    In my case, I was running x86-64 architecture on Raspberry Pi. Had to change the base image to ARM – mr i.o Jan 15 '21 at 02:29
  • I was wondering why my docker build fails on CI runner. Thanks! – Shinebayar G Feb 24 '21 at 20:42
  • 14
    I got the same thing building on an M1 mac with docker preview for Apple Silicon. My solution was to build on a different device. – Caleb Mar 12 '21 at 21:32
  • 1
    @Caleb Now, that's not really a *solution*, is it? More like a workaround. – Riwen Apr 15 '21 at 08:01
  • 17
    That's crazy, this is the main selling point of containerization, docker etc. – Yagiz Degirmenci May 02 '21 at 18:48
  • 8
    Oof I wouldn't have thought of that. Damn you M1 processor! – mawburn May 23 '21 at 02:53
  • Switch a kubernetes cluster node type, without paying attention to the fact that the arch was different – Dennis Jul 03 '21 at 19:00
  • 1
    @mawburn I know that feel – afe Jul 28 '21 at 15:40
  • 1
    You can also add the platform to build for in the first line: `FROM --platform=linux/x86_64 python:3.7-alpine`. This way you build it on the M1 and can run the container on Linux machines. – Hans Bambel Jul 01 '22 at 13:07
  • I got the same error running a node:latest image in an armv7l (32-bit) machine with docker 19. After I tried a lot of stuff, Finally, I used node:16 (instead of node:lates) and it worked! So, try using an older image, and it may work. – Dim Vai Apr 17 '23 at 21:06
59

I can see that you add the command command: [/app/helloworld.py] to yaml file.

so you need to (in Dockerfile):

RUN chmod +x /app/helloworld.py

set shebang to your py file:

#!/usr/bin/env python # whatever your defualt python to run the script

or setup the command the same as you did in Dockerfile

LinPy
  • 16,987
  • 4
  • 43
  • 57
52

You probably compile your docker image for the wrong platform.

For instance, if you run on arm64 (Apple M1 or M2?) and want to compile to run on intel/amd, you shall use the --platform option of docker build.

docker build --platform linux/amd64 -t registry.gitlab.com/group/project:1.0-amd64 ./

If you use docker-compose.yml to build your image, use:

services:
  app:
    platform: linux/amd64
    build: 
      context: ./myapp
      ...

For the docker platform naming list, and more info, you should read: https://docs.docker.com/build/building/multi-platform/

tip: to build for multiple platforms, use --platform linux/amd64,linux/arm64

Vincent J
  • 4,968
  • 4
  • 40
  • 50
  • If you build images in Apple and use them on Linux host machines then you better specify the platform when building them – kimy82 Aug 05 '22 at 13:29
  • Thanks, defo an issue on M1! I spent the whole day playing around with different `ENTRYPOINT` and `CMD` combos but it was this thing in the end – Raf Aug 15 '22 at 16:04
  • I had the same problem, but was able to build my images using GitHub Actions, where selected platform matches the final one - so it worked. – jkulak Sep 30 '22 at 11:59
43

I recently encountered the problem when running a logstash container

standard_init_linux.go:211: exec user process caused "exec format error"

Noticed that the shebang line (#!/bin/sh) on the entrypoint.sh was typed in the second line instead of the first line of the entrypoint.sh file.

When the shebang line is made as to the first line in the script, the error went away and "docker run -it logstashimage:latest sh" worked perfectly.

11

I had this issue recently. Sharing my experience here. Basically I was trying to run a ARM docker image on X86_64 architecture.

Here is the steps I have followed

  1. Check host architecture

     uname -m # Display the host architecture
     #x86_64
    
     docker pull arm32v7/ubuntu # Get ARM docker image
    
     docker run --rm -t arm32v7/ubuntu uname -m
     standard_init_linux.go:211: exec user process caused "exec format error"
    
  2. Setting up ARM emulation on x86

Using QEMU allows us to to build ARM binaries on x86 machine without needing a cross compiler.

sudo apt-get install qemu binfmt-support qemu-user-static # Install the qemu packages
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes # This step will execute the registering scripts
  1. Run the docker image

    $ docker run --rm -t arm32v7/ubuntu uname -m
    armv7l
    

Reference : Building ARM container on x86

jfk
  • 4,335
  • 34
  • 27
11

Rufus is correct, I came across this issue when attempting to build a x86 server image on my M1 MacBook which is arm64.

I was able to build for the correct target architecture by adding the "platform" attribute to my docker-compose.yml.

services:
    web:
        image: myimage/web:latest
        platform: linux/x86_64
        build:
            context: ./myfolder
            dockerfile: Dockerfile.prod
Noah Pan
  • 111
  • 1
  • 2
  • This solved my issue with docker compose, however note that it only worked with the newer `docker compose` vs. `docker-compose`. The new `docker compose` is a drop in replacement and I had no issues doing just that. – Erebus Jan 13 '22 at 23:00
  • You just saved my life. After 1.5 days of pulling my hair out as to why my new Mac can't spin up the same AWS ECS container using exactly the same code as my old Mac, and after getting no error logs from AWS. This, after pulling an error log from the Celery container on my AWS instance via SSH that gave a me new Google search term to use that ultimately led me to this page and your comment, was the answer. I thank you :). – N.Widds Mar 22 '22 at 16:01
7

Another two reasons could raise this issue if you run docker on Windows:

  • scripts line endings are not LF (linux)
  • scripts encoding should be utf-8 + BOM
DennisKot
  • 87
  • 1
  • 1
-1

I met exactly the same error message. It was fixed by install qemu on the host machine. No idea about the reason.

fishautumn
  • 364
  • 3
  • 6