exec user process caused: exec format error [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
4 min

banner

# exec user process caused: exec format error [Solved]

The Docker error "standard_init_linux.go: exec user process caused "exec format error" occurs for multiple reasons:

  1. Forgetting to add #!/bin/bash to the top of your .sh file (e.g. entrypoint.sh).
  2. Trying to run a Docker image that was built on x86 architecture on an arm64 or aarch64 machine.
  3. Using the incorrect line endings for your operating system, e.g. using CRLF (Windows) line endings used on a Unix machine (LF).
  4. Using an incorrect encoding, e.g. saving the file with a BOM (Byte Order Mark) encoding (e.g. utf-8-bom). Use the basic utf-8 encoding instead.

# Add the shebang character sequence at the top of your script

The first thing you need to ensure is to add the shebang character sequence at the top of your script (the .sh file).

For example, for a bash script, you would add the following line at the top (must be the first line).

entrypoint.sh
#!/bin/bash

Make sure there are no spaces in front of the line and the character sequence is not misspelled.

If that doesn't work, you can also try the following line.

entrypoint.sh
#!/usr/bin/env bash

If you use Alpine Linux (without bash), add the following line at the top of your .sh file.

entrypoint.sh
#!/bin/ash

The line has to be the very first line in your shell script, otherwise, it won't work.

If you get the "standard_init_linux.go:: exec user process caused "no such file or directory" error, then you are probably on an Alpine architecture that doesn't have a bash shell and you need to use #!/bin/ash (instead of #!/bin/bash) as shown in the previous code sample.

# The image might be built with a different architecture

Another common cause of the error is when your image is built with a different architecture than the one on your server.

In this case, you need to rebuild the image using the correct architecture.

For example, your image might be built using x86 (32-bit), so it cannot be used on an arm64 (64-bit) or aarch64 (64-bit) machine.

You might also have an image that was built on an arm64 architecture and is attempted to be run on amd64.

Conversely, you would also get the error if you try to run an amd64 image on a non-amd64 machine (e.g. ARM or 32-bit).

If you have an ARM-based chip, the Docker build command will assume arm64.

This is often the case if you have a MacBook Pro.

You can use the --platform parameter to specify the platform when running the docker build command.

For example, assuming ARM64 (ARM architecture that supports 64-bit processing).

shell
docker buildx build --platform=linux/arm64 -t <IMAGE_NAME>:<IMAGE_VERSION>-arm64 .

Make sure to replace the IMAGE_NAME and IMAGE_VERSION placeholders with the actual image name and version.

Similarly, if we assume AMD64 (AMD-developed architecture that supports 64-bit processing). Also known as x86-64, x64, and Intel 64.

shell
docker buildx build --platform=linux/amd64 -t <IMAGE_NAME>:<IMAGE_VERSION>-amd64 .

You will also get this error if you build your Docker image on an M1 chip and upload the image to be run on AWS (e.g. AWS Fargate or AWS ECS).

shell
docker buildx build --platform=linux/amd64 -t <IMAGE_NAME>:<IMAGE_VERSION>-amd64 .

If the error persists after rebuilding the image, update the FROM statement in your Dockerfile to also specify the platform

Dockerfile
FROM --platform=linux/amd64 BASE_IMAGE:BASE_IMAGE_VERSION

If you use a sub-image, make sure the FROM statement (with --platform) is specified in the parent image's Dockerfile.

# Make sure your shell script is saved using the correct line endings

Another thing you should verify is that your shell script was saved using the correct line ending characters.

If you are on Windows, the script file should be saved using CRLF (CarriageReturn + LineFeed) as the end of line sequence.

If you are on macOS or Linux, the file should be saved using LF (LineFeed) characters for line endings.

You might run into issues if you've saved your script file on Windows (CRFL) and attempt to run it on a Unix (LF) machine (macOS or Linux).

In this case, you need to save the file with Unix line endings (LF).

I've written a detailed guide on how to set the line endings in Visual Studio code.

After you save the file with the correct end-of-line characters, you have to rebuild your image.

# Make sure your script file was saved using the correct encoding

Another thing you need to verify is that your script file was saved using the correct encoding (utf-8).

Using an incorrect encoding often causes the error, especially when the encoding that is used has a BOM character (Byte Order Mark).

Make sure to set the file to be encoded using utf-8.

For example, if your current encoding is utf-8-bom or utf-32, utf-16, etc, switch the encoding to utf-8.

After you switch the encoding, rebuild your Docker image.

I've written a detailed guide on how to change the file encoding in Visual Studio Code.

# Conclusion

To solve the Docker error "standard_init_linux.go: exec user process caused "exec format error", make sure:

  1. You haven't forgotten to add the #!/bin/bash character sequence at the start of your script.
  2. You aren't trying to run a Docker image that was built on a different architecture.
  3. You aren't using incorrect line-ending characters.
  4. You aren't using an incorrect encoding for the script file.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.