How to fix Exit Code 127 in Kubernetes

KubernetesBEGINNERMEDIUMUpdated Dec 6, 2025· 99 views

Exit code 127 means "command not found"—the binary or script specified in your container command does not exist. Verify the path, install missing dependencies, or fix your Dockerfile.

Quick fix

Fixes Exit Code 127

kubectl logs <pod-name>
kubectl logs <pod-name> --previous
  1. Check logs for the missing command
  2. Verify command configuration in pod spec
  3. Test the image locally
  4. Install missing dependencies in Dockerfile
kubernetes — error
kubectl logs <pod-name>
kubectl logs <pod-name> --previous
Exit Code 127

What this error means

Exit code 127 is a standard Linux error code meaning "command not found." The container attempted to execute a command or binary that doesn't exist in the container filesystem. This isn't a Kubernetes-specific error—it's the container runtime reporting that the specified entrypoint, command, or script cannot be located. The issue is always in the container image or pod specification, not in Kubernetes itself.

Typical symptoms

  • Container fails immediately with exit code 127
  • Logs show "command not found" or "no such file or directory"
  • Pod enters CrashLoopBackOff after instant failures
  • Entrypoint script path incorrect in Dockerfile or pod spec
  • Commands work in local Docker but fail in Kubernetes
  • Multi-stage builds missing copied binaries
  • Alpine images missing expected commands

Common causes

  1. Typo in command name or path in pod specification
  2. Binary not installed in the container image
  3. Script path in ENTRYPOINT or CMD does not exist
  4. Multi-stage Docker build failed to copy required binaries
  5. PATH environment variable does not include binary location
  6. Using bash commands in images that only have sh
  7. Volume mount shadowing directory containing the command
  8. CRLF line endings in scripts causing shebang to fail

How to fix "Exit Code 127"

1Check logs for the missing command

View the error message to identify what's not found:

bash
kubectl logs <pod-name>
kubectl logs <pod-name> --previous

Common error patterns:
- "/bin/bash: command not found" - bash not installed
- "exec: ./start.sh: not found" - script doesn't exist or bad shebang
- "python: command not found" - Python not in image

2Verify command configuration in pod spec

Check what command Kubernetes is trying to run:

bash
kubectl get pod <pod-name> -o jsonpath='{.spec.containers[0].command}'
kubectl get pod <pod-name> -o jsonpath='{.spec.containers[0].args}'

Compare with what's in the image:

bash
docker run --rm myimage:latest ls -la /app/
docker run --rm myimage:latest which python

Ensure paths match exactly.

3Test the image locally

Reproduce the issue outside Kubernetes:

bash
# Run with the same command
docker run --rm myimage:latest /app/start.sh

# If that fails, try interactively
docker run -it --rm myimage:latest /bin/sh

# Then check if command exists
which python
ls -la /app/

If it fails locally, fix the Dockerfile.

4Install missing dependencies in Dockerfile

Add the missing command to your image:

dockerfile
# For Alpine
RUN apk add --no-cache bash python3 curl

# For Debian/Ubuntu
RUN apt-get update && apt-get install -y python3 curl

# For multi-stage builds, copy the binary
COPY --from=builder /app/mybin /usr/local/bin/mybin

Rebuild and push:

bash
docker build -t myimage:latest .
docker push myimage:latest
5Use absolute paths for commands

Avoid relying on PATH—use full paths:

yaml
# Fragile - depends on PATH
command: ["python", "app.py"]

# Robust - explicit path
command: ["/usr/bin/python3", "/app/app.py"]

Find the correct path:

bash
docker run --rm myimage:latest which python3
6Fix shebang and line endings for scripts

Scripts may fail with "not found" due to incorrect shebangs or line endings:

bash
# Check for Windows line endings
file start.sh
# Shows: "start.sh: ASCII text, with CRLF line terminators" = problem

# Fix line endings
sed -i 's/\r$//' start.sh
# Or in Dockerfile
RUN sed -i 's/\r$//' /app/start.sh

Ensure shebang matches available shell:

bash
#!/bin/sh      # Works in Alpine
#!/bin/bash    # Requires bash installation

Advanced notes

Exit code 127 in multi-stage builds usually means COPY --from failed silently or copied to the wrong path. Verify each stage:

dockerfile
# Debug by listing files in final stage
RUN ls -la /usr/local/bin/

Volume mounts can shadow directories containing binaries. If /app is mounted as an empty volume, anything originally in /app becomes inaccessible.

For architecture mismatches (AMD64 image on ARM node), the binary may exist but fail to execute. Check with:

bash
kubectl get nodes -o wide  # Shows architecture
docker inspect myimage:latest | grep Architecture

Use multi-arch images or ensure node selector matches image architecture.

Frequently asked questions

What does "Exit Code 127" mean?
" The container attempted to execute a command or binary that doesn't exist in the container filesystem. This isn't a Kubernetes-specific error—it's the container runtime reporting that the specified entrypoint, command, or script cannot be located. The issue is always in the container image or pod specification, not in Kubernetes itself.
How do I fix "Exit Code 127"?
Exit code 127 means "command not found"—the binary or script specified in your container command does not exist. Verify the path, install missing dependencies, or fix your Dockerfile.
What causes "Exit Code 127"?
Typo in command name or path in pod specification Binary not installed in the container image Script path in ENTRYPOINT or CMD does not exist
Is the "Exit Code 127" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading