How to fix Container exited with code 127: command not found in Docker

DockerBEGINNERMEDIUMUpdated Dec 1, 2025ยท 157 views

Exit code 127 indicates that a command specified in your Docker container could not be found. This is a standard Linux/Unix shell error code meaning the shell cannot locate the executable you're trying to run.

Quick fix

Fixes Container exited with code 127: command not found

docker logs <container_name_or_id>
  1. Check container logs for the exact error
  2. Verify the command exists in the container
  3. Check your Dockerfile CMD and ENTRYPOINT
  4. Install missing dependencies in your Dockerfile
docker โ€” error
docker logs <container_name_or_id>
Container exited with code 127: command not found

What this error means

Exit code 127 is a standard POSIX/Linux shell exit code that means "command not found." When Docker reports this error, it means the container started but the command specified in CMD, ENTRYPOINT, or the docker run command does not exist or cannot be located in the container's filesystem. This error originates from the shell inside the container, not from Docker itself. The container's shell (typically /bin/sh or /bin/bash) tried to execute a command but couldn't find the binary in the PATH or at the specified location. Common scenarios include: specifying a command that isn't installed in the base image, using the wrong path to an executable, or having scripts with incorrect line endings (Windows CRLF vs Unix LF).

Typical symptoms

  • Container exits immediately after starting with code 127
  • docker logs shows 'command not found' or 'No such file or directory'
  • Container runs fine locally but fails in CI/CD or Kubernetes
  • Error message like '/bin/sh: command: not found'
  • Script fails to execute even though file exists
  • Build succeeds but container fails at runtime

Common causes

  1. The command or binary specified in CMD/ENTRYPOINT doesn't exist in the container image
  2. Incorrect path to the executable (relative vs absolute path)
  3. Missing dependencies - the application requires libraries not installed in the image
  4. Using wrong package manager for base image (apt-get on Alpine instead of apk)
  5. Windows line endings (CRLF) in shell scripts instead of Unix line endings (LF)
  6. Invalid or missing shebang line (#!/bin/bash) in scripts
  7. Volume mount overwrote a directory containing required binaries
  8. Typo in command name or Dockerfile instruction

How to fix "Container exited with code 127: command not found"

1Check container logs for the exact error

Run the following command to see what command failed:

bash
docker logs <container_name_or_id>

Look for messages like "command not found" or "No such file or directory" to identify which specific command is missing.

2Verify the command exists in the container

Run an interactive shell in your container to check if the command exists:

bash
docker run -it --entrypoint /bin/sh <image_name>

Then try running the command manually. If it's not found, you need to install it in your Dockerfile.

3Check your Dockerfile CMD and ENTRYPOINT

Review your Dockerfile for typos or incorrect paths:

dockerfile
# Correct - use absolute paths
CMD ["/usr/local/bin/myapp"]

# Incorrect - relative path may fail
CMD ["myapp"]

Use which <command> or type <command> inside the container to find the correct path.

4Install missing dependencies in your Dockerfile

If the command isn't available, add it to your Dockerfile. Use the correct package manager for your base image:

dockerfile
# For Debian/Ubuntu-based images
RUN apt-get update && apt-get install -y <package_name>

# For Alpine-based images
RUN apk add --no-cache <package_name>

# For RHEL/CentOS-based images
RUN yum install -y <package_name>
5Fix line endings for shell scripts (Windows users)

If you're developing on Windows, your shell scripts might have CRLF line endings. Convert them to LF:

bash
# Using dos2unix
dos2unix your-script.sh

# Or in your Dockerfile
RUN sed -i 's/\r$//' /app/your-script.sh

Alternatively, configure Git to not convert line endings:

bash
git config --global core.autocrlf input
6Ensure script has proper shebang and is executable

Make sure your script starts with a valid shebang line and has execute permissions:

bash
#!/bin/bash
# or for Alpine which uses ash by default:
#!/bin/sh

In your Dockerfile, ensure the script is executable:

dockerfile
COPY script.sh /app/
RUN chmod +x /app/script.sh
7Debug with verbose shell logging

To diagnose which exact command is failing, enable shell tracing:

dockerfile
# Temporarily modify your CMD for debugging
CMD ["/bin/sh", "-x", "/app/entrypoint.sh"]

The -x flag shows each command before execution, helping identify the failing command.

Advanced notes

Multi-stage builds and missing binaries: If using multi-stage builds, ensure you copy all required binaries and their dependencies to the final stage. Use ldd <binary> to list shared library dependencies.

Alpine Linux gotchas: Alpine uses musl libc instead of glibc. Binaries compiled for glibc-based systems won't work on Alpine. Either compile statically, use Alpine-compatible builds, or switch to a glibc-based image.

Debugging in Kubernetes: If the container crashes too quickly to inspect, change the command temporarily:

yaml
command: ["sleep", "infinity"]

Then exec into the pod to debug.

Using exec form vs shell form: The exec form CMD ["executable", "param1"] runs the command directly. The shell form CMD command param1 runs through /bin/sh -c, which requires /bin/sh to exist in the image.

Frequently asked questions

What does "Container exited with code 127: command not found" mean?
" When Docker reports this error, it means the container started but the command specified in CMD, ENTRYPOINT, or the docker run command does not exist or cannot be located in the container's filesystem. This error originates from the shell inside the container, not from Docker itself. The container's shell (typically /bin/sh or /bin/bash) tried to execute a command but couldn't find the binary in the PATH or at the specified location.
How do I fix "Container exited with code 127: command not found"?
Exit code 127 indicates that a command specified in your Docker container could not be found. This is a standard Linux/Unix shell error code meaning the shell cannot locate the executable you're trying to run.
What causes "Container exited with code 127: command not found"?
The command or binary specified in CMD/ENTRYPOINT doesn't exist in the container image Incorrect path to the executable (relative vs absolute path) Missing dependencies - the application requires libraries not installed in the image
Is the "Container exited with code 127: command not found" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading