Exit code 2 in Docker indicates a misuse of a shell command or invalid arguments passed to a command inside the container. This is a standard Unix/Linux convention where code 2 typically signals command syntax errors, missing required arguments, or incorrect usage of shell builtins.
Exit code 2 is a Unix/Linux convention that typically means "misuse of shell builtins" or command syntax errors. Unlike Docker-specific exit codes (125, 126, 127), exit code 2 comes from the command running inside your container, not from Docker itself. When your container exits with code 2, it usually means the command or script executed inside the container received invalid arguments, encountered a syntax error, or was called incorrectly. Many command-line tools use exit code 2 specifically to indicate usage errors or invalid options. Common examples include: bash returning 2 for syntax errors, grep returning 2 for invalid options, and many CLI tools using 2 to signal incorrect command invocation. The error originates from the application layer, so you need to examine what command failed and why.
First, examine the container logs to see what command failed and why:
docker logs <container_name_or_id>Look for error messages like "syntax error", "missing argument", "invalid option", or usage help text that indicates incorrect command invocation.
Start the container with an interactive shell to manually test commands:
docker run -it --entrypoint /bin/sh <image_name>Once inside, try running the commands from your Dockerfile or entrypoint script manually to identify which one fails.
A common cause is missing files. Check that your scripts and referenced files exist:
# Inside the container
ls -la /app/
cat /app/entrypoint.shIf using WORKDIR, ensure the directory exists and contains your files. Volume mounts can overwrite directories, hiding files copied during build.
Validate your shell scripts for syntax errors before building:
# Check bash script syntax
bash -n your-script.sh
# Check sh script syntax
sh -n your-script.shCommon issues include:
- Missing closing brackets in conditionals: [ -f file ] not [ -f file
- Unclosed quotes or missing spaces around brackets
- Using bash-specific syntax in /bin/sh scripts
Ensure your Dockerfile uses the correct format for CMD and ENTRYPOINT:
# Exec form (recommended) - runs command directly
CMD ["python", "app.py"]
# Shell form - runs through /bin/sh -c
CMD python app.pyIf using exec form, ensure it's valid JSON. A malformed array like CMD ["python" "app.py"] (missing comma) will cause parsing errors.
If developing on Windows, shell scripts may have CRLF line endings that cause parsing errors:
# Convert to Unix line endings
dos2unix your-script.sh
# Or in Dockerfile
RUN sed -i 's/\r$//' /app/entrypoint.shYou can also configure Git to handle line endings:
git config --global core.autocrlf inputEnable verbose output to see exactly which command fails:
# Temporarily add -x flag for debugging
CMD ["/bin/sh", "-x", "/app/entrypoint.sh"]Or add set -x at the top of your shell script. This prints each command before execution, making it easy to identify the failing line.
Commands may fail if required environment variables are missing:
# Check environment in container
docker run --rm <image_name> env
# Or inspect a running container
docker exec <container_id> envEnsure variables referenced in scripts are defined in your Dockerfile or docker-compose.yml.
Understanding exit codes: Exit code 2 is not Docker-specific. Docker only defines codes 125 (daemon error), 126 (command cannot execute), and 127 (command not found). Code 2 comes from the command inside your container.
grep and diff special cases: Some tools have specific meanings for exit code 2. For example, grep returns 2 for errors (vs 1 for "no match"), and diff returns 2 for trouble (vs 1 for "files differ").
Docker Compose exit codes: When using docker-compose up --exit-code-from <service>, the compose command returns the exit code of the specified service. Exit code 2 from compose itself may indicate configuration issues.
Debugging crashed containers: If the container exits too quickly, override the entrypoint:
docker run -it --entrypoint /bin/sh <image_name> -c "sleep infinity"Then exec into it to debug.
Multi-stage builds: Ensure scripts copied from builder stages have correct permissions and line endings. Use chmod +x and consider running dos2unix in the Dockerfile.
image operating system "linux" cannot be used on this platform
How to fix 'image operating system linux cannot be used on this platform' in Docker
manifest unknown: manifest unknown
How to fix 'manifest unknown' in Docker
cannot open '/etc/passwd': Permission denied
How to fix 'cannot open: Permission denied' in Docker
Error response from daemon: failed to create the ipvlan port
How to fix 'failed to create the ipvlan port' in Docker
toomanyrequests: Rate exceeded for anonymous users
How to fix 'Rate exceeded for anonymous users' in Docker Hub