1. Overview

The “executable not found” error in Alpine Docker containers can be a real pain, even when the executable is right where it should be in the $PATH. It’s a common problem, especially when using Alpine’s lightweight images.

In this tutorial, we’ll explain why this happens and learn how to fix it.

2. Understanding the “Executable Not Found” Error in Alpine

The $PATH environment variable plays a crucial role in locating executable files. It tells the shell where to look when we try to run a command. If an executable exists in one of the directories listed in $PATH, the shell finds and executes it.

However, Alpine Linux uses the musl libc library instead of the common glibc, which most Linux distros rely on. This difference can cause issues with dynamic linking. As a result, executables fail to find the libraries they depend on.

These failures often lead to a misleading “command not found” error, even though the file is present.

Dynamic linking here means that executables rely on external libraries at runtime to function. If these libraries are missing or incompatible, the system won’t be able to run the program.

In Alpine, the absence of glibc or related libraries can prevent an executable from running, resulting in errors that confuse users. Hence, understanding these issues helps diagnose and resolve the problem efficiently.

3. Diagnosing the Issue

Before jumping into solutions, let’s identify the problem correctly. Here are a couple of quick checks to help diagnose the “executable not found” error in Alpine.

3.1. Check if the Executable Is Present

First, we need to check if the executable is actually there. We can use the ls command to list everything in the directory where the file should be:

$ ls /usr/local/bin

If the file shows up in the output, we know it’s present. However, that doesn’t mean it’s usable.

To confirm the system can find it, we use the which command to see if the executable is accessible through the $PATH:

$ which <our_executable>

After running the above command, if which shows the full path, the system knows where the file is. But if nothing appears, even though the file exists, something is wrong.

The system can’t locate it, likely due to an issue with $PATH or the executable’s compatibility with Alpine.

3.2. Verify Executable Compatibility With Alpine

Now, let’s dig a bit deeper. We need to check if the executable is compatible with Alpine.

The file command comes in handy here. It tells us about the type of file we’re dealing with. Additionally, the command tells us whether the executable is dynamically linked and what libraries it depends on:

$ file /usr/local/bin/<our_executable>

This command gives us details about the executable.

We have to look closely at the output for mentions of glibc or ld-linux-x86-64.so.2. These are signs that the binary was built for a system using glibc, which Alpine doesn’t support by default since it uses musl.

If the executable depends on glibc, we have to either install the compatibility layer (gcompat) or switch to a different base image. This issue is common in Alpine due to its minimalistic design.

4. Common Causes and Solutions

Now that we’ve diagnosed the problem, let’s explore the most common reasons behind the “executable not found” error in Alpine. More importantly, let’s also cover how to fix them.

4.1. Missing Libraries

Missing shared libraries, especially those from glibc, often trigger the “executable not found” error in Alpine. Since Alpine uses musl, which isn’t fully compatible with glibc, binaries built for glibc may fail to run.

To fix this, we can install either glibc or a compatibility layer like gcompat.

Let’s install gcompat:

$ apk add gcompat
(1/3) Installing musl-obstack (1.2.3-r2)
(2/3) Installing libucontext (1.2-r3)
(3/3) Installing gcompat (1.1.0-r4)
OK: 9 MiB in 17 packages

After installation, let’s use the ldd command to check for missing libraries:

$ ldd /usr/local/bin/<our_executable>

This command shows any unresolved dependencies, which helps us confirm if we’ve fixed the issue.

4.2. Binary Built for a Different Architecture

Sometimes, the “executable not found” error isn’t about missing libraries. Instead, it’s a simple case of mismatched architecture. Imagine trying to fit a square peg into a round hole.

If the executable was built for a different CPU architecture (like ARM) than the one Alpine runs on (like x86), it simply won’t fit. This mismatch leads to compatibility issues, and the binary can’t run.

To fix this, we have two main options. First, we can switch to a compatible base image that matches the binary’s architecture. This way, everything fits snugly, and the executable should run without a hitch.

Alternatively, we can recompile the binary specifically for Alpine if we have the source code. This ensures it’s tailored to the musl environment and avoids any architecture-related hiccups.

4.3. Statically Linked Binaries

So far, we’ve tackled issues related to missing libraries and mismatched architectures. But what if we could avoid these dynamic linking headaches altogether? That’s where static linking comes in.

Instead of relying on the system’s libraries at runtime, we can bundle all the necessary tools directly into the executable itself. This makes it self-contained and less prone to compatibility issues, ensuring it works across different systems.

We can build a statically linked library in Go:

$ CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app

This command tells Go to disable CGO (which enables dynamic linking) and create a static binary specifically for Linux.

Similarly, in C/C++, we can achieve static linking using the gcc compiler:

$ gcc -static -o app app.c

The -static flag instructs the compiler to include all necessary libraries within the executable itself.

Therefore, we eliminate the need for external library dependencies using static linking. This approach guarantees that the binary works across different systems, including Alpine.

5. Best Practices for Avoiding These Issues

While troubleshooting is helpful, let’s find out some proactive measures to prevent these issues from happening in the first place. Following these best practices can save us time and frustration in the long run.

5.1. Pre-Installing Dependencies

To avoid the “executable not found” errors, it’s essential to identify and install all the necessary dependencies right from the start — during the build process itself.

In Alpine, we use the apk add command in our Dockerfile to include the packages we need. For example, if we know our executable relies on certain libraries, we can add a similar line:

$ RUN apk add --no-cache libc6-compat bash

This step ensures the container has everything it needs to run our executable without any surprises.

5.2. Using Multi-Stage Builds

Multi-stage builds are another powerful tool in our Docker toolbox. They enable us to separate the build environment from the runtime environment. This keeps our final image lean and efficient.

We can use a larger base image in the first stage to compile or install our application and all its dependencies. Then, in the second stage, we copy only the necessary output (the compiled executable) into a smaller Alpine-based image.

Let’s take a look at an example using Go:

FROM golang:alpine AS build
WORKDIR /app
COPY . .
RUN go build -o myapp

FROM alpine
COPY --from=build /app/myapp /usr/local/bin/

This approach enables us to build complex applications while keeping the final image small and free of unnecessary build tools. This improves overall efficiency.

6. Conclusion

In this article, we explored the common “executable not found” error in Alpine Docker containers and how to fix it. We tackled common causes like missing libraries and mismatched architectures.

Additionally, we learned how to diagnose the problem and apply best practices like installing dependencies and using multi-stage builds.

Finally, while Alpine’s lightweight nature is a big plus, it’s important to be aware of potential compatibility issues.