error docker

Docker Exit Code 127 - Command Not Found

Understanding Docker exit code 127 - the container's entrypoint or command could not be found in the container's filesystem.

What It Means

Docker exit code 127 indicates that the command specified in CMD, ENTRYPOINT, or docker run could not be found inside the container. The binary or script does not exist in the container’s filesystem or is not in the container’s PATH.

Common Causes

  • Typo in CMD or ENTRYPOINT command
  • Binary not installed in the container image
  • Using a slim/alpine base image that lacks common utilities
  • Script not copied into the image during build
  • Wrong working directory (WORKDIR) set in Dockerfile
  • Shell not available in the image (e.g., /bin/bash in alpine)
  • Multi-stage build not copying the binary to the final stage

How to Fix

Check the command exists in the container

# Try running a shell to inspect
docker run -it --entrypoint /bin/sh my-image
# If /bin/sh doesn't exist either, the image is very minimal

# Check if the binary exists
docker run --rm my-image which node
docker run --rm my-image ls -la /app/

Fix Dockerfile CMD/ENTRYPOINT

# Wrong: binary doesn't exist
CMD ["nodejs"]  # It's 'node', not 'nodejs'

# Right
CMD ["node", "server.js"]

# Wrong: bash doesn't exist in alpine
FROM node:20-alpine
CMD ["/bin/bash", "-c", "node server.js"]

# Right: use sh in alpine
FROM node:20-alpine
CMD ["/bin/sh", "-c", "node server.js"]
# Or better:
CMD ["node", "server.js"]

Fix missing files in multi-stage builds

# Stage 1: Build
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm ci && npm run build

# Stage 2: Production
FROM node:20-alpine
WORKDIR /app

# Don't forget to copy the built files!
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./

CMD ["node", "dist/server.js"]

Install missing packages

# Alpine - install missing tools
FROM alpine:3.19
RUN apk add --no-cache curl bash jq

# Debian/Ubuntu - install missing tools
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

Fix WORKDIR issues

# Wrong: script is not in the WORKDIR
WORKDIR /app
COPY scripts/start.sh /scripts/start.sh
CMD ["./start.sh"]  # Looks in /app, not /scripts

# Right: copy to the correct location
WORKDIR /app
COPY scripts/start.sh ./start.sh
RUN chmod +x ./start.sh
CMD ["./start.sh"]

Debug with docker run

# Check what's in the image
docker run --rm my-image ls -la /app/
docker run --rm my-image cat /app/package.json
docker run --rm my-image echo $PATH