# Docker Exec Failed: How to Run Commands in Containers

You tried to run a command inside a container, but it failed:

bash
Error response from daemon: Container abc123 is not running

Or:

bash
OCI runtime exec failed: exec failed: executable file not found in $PATH

Or:

bash
Error response from daemon: cannot exec in a stopped container: unknown

docker exec lets you run commands in running containers. Let me show you how to diagnose and fix these failures.

Step 1: Check Container Status

The most common error—container must be running:

```bash # Check container status docker ps -a | grep mycontainer

# Check specific container docker inspect mycontainer --format '{{.State.Status}}'

# Should return "running" for exec to work # If "exited" or "paused", start the container first docker start mycontainer

# For paused containers docker unpause mycontainer ```

Quick check:

```bash # Only running containers appear here docker ps | grep mycontainer

# If not listed, it's not running docker ps -a | grep mycontainer # Shows all including stopped ```

Step 2: Find the Correct Container Name

Name confusion causes exec failures:

```bash # List all containers with names docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"

# Use full name or ID docker exec mycontainer-name ls /app

# Or use container ID (partial works) docker exec abc123 ls /app

# For Docker Compose, names include project prefix docker exec myproject_webapp_1 ls /app ```

Step 3: Check Command Availability

The command must exist in the container:

```bash # Error: executable file not found # Check what's available in the container docker exec mycontainer ls /bin docker exec mycontainer ls /usr/bin

# Use full path if needed docker exec mycontainer /usr/bin/python --version

# Try a shell first to explore docker exec -it mycontainer /bin/sh ```

Different base images have different shells:

```bash # Alpine-based containers docker exec -it mycontainer /bin/sh # Or docker exec -it mycontainer sh

# Debian/Ubuntu-based docker exec -it mycontainer /bin/bash # Or docker exec -it mycontainer bash

# Minimal containers might have neither # Check what exists docker exec mycontainer cat /etc/os-release ```

Step 4: Handle User Permissions

Exec might fail due to user restrictions:

```bash # Error: operation not permitted # Check container user docker exec mycontainer whoami

# Run as root docker exec -u root mycontainer command

# Run as specific user docker exec -u appuser mycontainer command

# Check user in Dockerfile docker inspect mycontainer --format '{{.Config.User}}' ```

File permission issues:

```bash # Can't access files as container user docker exec -u root mycontainer chmod 755 /app/file

# Then try as regular user docker exec mycontainer ls /app/file ```

Step 5: Use Interactive Mode Properly

For interactive commands, use -it flags:

```bash # Wrong: no interactive flags docker exec mycontainer bash # Just runs bash and exits immediately

# Correct: interactive terminal docker exec -it mycontainer bash

# -i keeps STDIN open # -t allocates a pseudo-TTY ```

**Non-interactive commands don't need -it:**

```bash # Run one-off command docker exec mycontainer cat /etc/hosts

# Run script docker exec mycontainer /app/script.sh ```

Step 6: Environment Variables in Exec

Commands might need specific environment:

```bash # Set environment for exec docker exec -e MY_VAR=value mycontainer command

# Multiple variables docker exec -e VAR1=val1 -e VAR2=val2 mycontainer command

# Use current environment docker exec -i --env-file ./env.list mycontainer command

# Check environment in container docker exec mycontainer env ```

Step 7: Working Directory

Exec defaults to container's working directory:

```bash # Check current directory docker exec mycontainer pwd

# Run in specific directory docker exec -w /app mycontainer ls

# Check working directory setting docker inspect mycontainer --format '{{.Config.WorkingDir}}' ```

Step 8: Debug Exec Failures

When exec fails mysteriously:

```bash # Check container logs docker logs mycontainer

# Inspect container state docker inspect mycontainer

# Check process list in container docker top mycontainer

# Check container events docker events --filter container=mycontainer --since 5m ```

Container might be in transition state:

```bash # Container restarting docker inspect mycontainer --format '{{.State.Restarting}}'

# Container paused docker inspect mycontainer --format '{{.State.Paused}}'

# Wait for container to be ready docker wait mycontainer # Wait for next exit sleep 2 docker exec mycontainer command ```

Step 9: Alternative Approaches

If exec keeps failing, try alternatives:

```bash # Attach to container (for interactive containers) docker attach mycontainer

# Use docker run with same image to debug docker run -it --rm --entrypoint sh myimage:latest

# Export container filesystem docker export mycontainer | tar -x

# Copy files out for inspection docker cp mycontainer:/app/file ./local-file ```

Step 10: Handle Minimal Containers

Some containers are built without shells:

```bash # Error: /bin/sh: no such file or directory # Minimal containers (scratch-based) have no shell

# Debug using a sidecar container docker run -it --rm \ --volumes-from mycontainer \ alpine sh # Now explore mycontainer's volumes ```

Or add shell temporarily:

bash
# Copy a shell into container (if writable)
docker cp /bin/sh mycontainer:/bin/sh
docker exec mycontainer /bin/sh

Common Exec Error Patterns

ErrorCauseFix
Container is not runningContainer stoppedStart container: docker start NAME
executable file not foundCommand doesn't existUse full path or install command
operation not permittedPermission deniedRun as root: docker exec -u root
no such containerWrong name/IDCheck name with docker ps
/bin/sh not foundMinimal imageUse sidecar or debug with run

Quick Exec Reference

```bash # Basic exec docker exec CONTAINER COMMAND

# Interactive shell docker exec -it CONTAINER bash

# As root docker exec -u root CONTAINER COMMAND

# In specific directory docker exec -w /path CONTAINER COMMAND

# With environment docker exec -e VAR=value CONTAINER COMMAND

# As specific user docker exec -u USER CONTAINER COMMAND

# Detached mode docker exec -d CONTAINER COMMAND ```

Prevention Best Practices

  1. 1.Add shells to debugging images during development
  2. 2.Set proper working directory in Dockerfile
  3. 3.Run containers with restart policy to avoid stopped state
  4. 4.Use health checks to ensure containers stay healthy
  5. 5.Document common exec commands for team

Quick Reference

TaskCommand
Check statusdocker ps or docker inspect NAME --format '{{.State.Status}}'
Start containerdocker start NAME
Interactive shelldocker exec -it NAME bash
Run as rootdocker exec -u root NAME command
Set working dirdocker exec -w /path NAME command
Set environmentdocker exec -e VAR=val NAME command
List processesdocker top NAME
Check userdocker inspect NAME --format '{{.Config.User}}'

Exec failures are almost always due to the container not running, the command not existing, or permission issues. Check container status first, then verify the command is available in the container's filesystem.