What this error means
You ran a command like:
- docker exec -it <container> bash
and got:
- OCI runtime exec failed: exec: "bash": executable file not found in $PATH
This means the container does not have bash installed, or its PATH does not include the directory where bash resides. Many minimal images (Alpine, BusyBox, distroless) either ship only sh/ash or have no shell at all.
Quickstart (most common fixes)
- Use the available shell: docker exec -it <container> sh
- Install bash in the container image (if appropriate):
- Alpine: apk add --no-cache bash
- Debian/Ubuntu: apt-get update && apt-get install -y --no-install-recommends bash
- Choose a base image that already includes bash (e.g., ubuntu:22.04) when you need a bash shell.
- Fix scripts/ENTRYPOINT to use a shell that exists, e.g., #!/bin/sh instead of #!/usr/bin/env bash if on Alpine.
Minimal working example
Reproduce the error and fix it with Alpine.
# Start a minimal container without bash
docker run -d --name demo alpine:3.20 sleep 600
# This fails (no bash)
docker exec -it demo bash
# Use the available shell (sh)
docker exec -it demo sh
# Optionally install bash (adds size and packages)
docker exec -it demo sh -lc "apk add --no-cache bash"
# Now bash works
docker exec -it demo bash
Minimal Dockerfile that guarantees bash is available:
FROM alpine:3.20
RUN apk add --no-cache bash ca-certificates
CMD ["bash", "-lc", "echo Bash is available; sleep 3600"]
docker build -t alpine-with-bash . && docker run --rm -d --name demo2 alpine-with-bash
Step-by-step diagnosis and fixes
- Check what shells exist
docker exec -it <container> sh -lc "command -v bash || true; command -v sh || true; ls -1 /bin /usr/bin 2>/dev/null | grep -E '^(ba)?sh$' || true"
- If bash is missing, prefer using sh.
- Inspect PATH
docker exec -it <container> sh -lc 'echo "$PATH"'
- If bash is installed but not found, ensure its directory (e.g., /bin, /usr/bin) is in PATH. Fix PATH in the image or startup scripts. For sh sessions you can test:
docker exec -it <container> sh -lc 'export PATH=$PATH:/usr/local/bin:/usr/bin:/bin; command -v bash'
- Fix your ENTRYPOINT/CMD and scripts
- Use a shebang that exists on the base image.
#!/bin/sh
set -euo pipefail
# your script
- Avoid #!/usr/bin/env bash when the image doesn’t include bash.
- Install bash (if justified)
- Alpine:
docker exec -it <container> sh -lc 'apk add --no-cache bash'
- Debian/Ubuntu:
docker exec -it <container> sh -lc 'apt-get update && apt-get install -y --no-install-recommends bash'
Persist this by baking it into your Dockerfile to avoid mutable containers.
- Choose the right base image
- If your tooling or scripts require bash, start from an image that includes it (e.g., ubuntu, debian). If not, prefer sh-compatible scripts and minimal images.
- Compose override example
# docker-compose.yml
services:
app:
image: alpine:3.20
command: ["sh", "-c", "echo running; sleep 3600"]
If you need a debug shell, exec with sh:
docker compose exec app sh
Common base images and default shells
| Base image | Default shell | Bash present by default |
|---|---|---|
| alpine | /bin/sh (ash) | No |
| busybox | /bin/sh | No |
| debian/ubuntu | /bin/sh, bash | Yes (bash installed) |
| distroless/scratch | None | No (no shell) |
Pitfalls
- Distroless/scratch: No shell at all; docker exec bash or sh won’t work. Use a sibling debug container (e.g., busybox) sharing the network namespace: docker run --rm -it --network container:<id> busybox sh.
- ENTRYPOINT scripts with bash shebang on Alpine will fail at start or during exec.
- CRLF line endings in scripts can break sh/bash in containers. Ensure LF endings.
- SHELL in Dockerfile affects build-time RUN, not runtime shells. It won’t install bash.
- Overriding ENTRYPOINT to bash in docker run on Alpine will fail: use sh or install bash first.
Performance notes
- Installing bash increases image size (Alpine + bash ≈ +1–2 MB, Debian/Ubuntu larger base). Prefer sh-compatible scripts if possible.
- Keep images minimal for faster pulls and better cold-start. Only include bash where needed (e.g., developer/debug images).
- Use multi-stage builds: compile/test in a full image with bash; publish a minimal runtime image that uses sh or no shell.
- Avoid mutating running containers (apk/apt-get install). Bake dependencies into the image for reproducibility and faster restarts.
Tiny FAQ
Why does docker exec bash fail on Alpine? Alpine doesn’t include bash by default; use sh or install bash.
Can I fix this without installing bash? Yes. Use sh/ash and write POSIX-sh compatible scripts.
I’m on Kubernetes and kubectl exec -- bash fails. Same issue? Yes. The container image likely lacks bash. Use sh or a debug image/ephemeral container.
How do I know which shell to use? Try sh first: docker exec -it <container> sh. If it fails, your image may be distroless/scratch.
Can I just change PATH to make bash work? Only if bash is actually installed. PATH won’t help if the binary is missing.
What if I need bash for advanced features? Start from an image that includes bash, or install it in your Dockerfile and maintain a separate debug/developer image.