Cron Job Failing? The Ultimate Guide to Fixing 'docker: command not found'
Content
## The Problem
You've set up a `cron` job to execute a `docker exec` command. It works perfectly when you run it in your terminal, but when you check the logs, you find a perplexing error. This is a real-world scenario encountered by developer DP from the `wiki.lib00.com` community.
**Crontab Configuration (`crontab -l`):**
```crontab
*/2 * * * * docker exec ee-php-fpm-8.4.13 php /eeBox/eeProject/app/public/index.php '/data-import/import?source=all' >> /var/log/wiki.lib00/api_import.log 2>&1
```
**Error Log (`api_import.log`):**
```plaintext
/bin/sh: docker: command not found
/bin/sh: docker: command not found
```
Why does the `docker` command, which is readily available in your terminal, suddenly become "not found" in the world of `cron`?
---
## The Root Cause: A Tale of Two `PATH` Environments
The core of the issue is this: **the execution environment for a `cron` job is isolated and vastly different from your interactive terminal environment**.
- **Terminal Environment**: When you open a terminal, your system loads configuration files like `.bash_profile` or `.zshrc`. These files set up a comprehensive `PATH` environment variable, which tells the shell where to find executables, including paths like `/usr/local/bin` (where `docker` is often located).
- **Cron Environment**: `cron` runs tasks in the background in a non-interactive mode, loading a very minimal, default environment. Its `PATH` variable typically only includes basic system directories like `/bin` and `/usr/bin`. The directory `/usr/local/bin` is not part of this default search path.
Consequently, when `cron`'s execution shell (`/bin/sh`) tries to run `docker`, it can't find the command in its limited `PATH`, leading to the `command not found` error.
---
## The Solutions
Here are three solutions, ranging from simple to more robust. **The first one is highly recommended** for its clarity and reliability.
### Solution 1: Use the Absolute Path (Highly Recommended)
This is the best practice for solving this problem as it removes all environmental ambiguity.
1. **Find the Absolute Path to `docker`**
Run the `which` command in your terminal:
```bash
which docker
```
You will typically get an output like `/usr/local/bin/docker`.
2. **Modify Your Crontab**
Use `crontab -e` to edit your job, replacing `docker` with its full, absolute path.
**Before:**
```crontab
docker exec ee-php-fpm-8.4.13 ...
```
**After:**
```crontab
*/2 * * * * /usr/local/bin/docker exec ee-php-fpm-8.4.13 php /eeBox/eeProject/app/public/index.php '/data-import/import?source=all' >> /var/log/wiki.lib00/api_import.log 2>&1
```
### Solution 2: Define `PATH` in the Crontab File
If you have multiple cron jobs that depend on commands in non-standard paths, you can define the `PATH` variable at the top of your crontab file.
1. Run `crontab -e`.
2. Add a line at the very top that defines a `PATH` including all necessary directories.
```crontab
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
*/2 * * * * docker exec ee-php-fpm-8.4.13 php ...
```
All subsequent jobs in the file will inherit this more complete `PATH`.
### Solution 3: Wrap the Command in a Shell Script
For complex commands or long command chains, wrapping them in a dedicated shell script is a cleaner and more maintainable approach.
1. **Create a Script File**
Create a file named `run_import_from_lib00.sh`.
```bash
#!/bin/sh
# Ensure the PATH includes the directory for docker
export PATH="/usr/local/bin:/usr/bin:/bin:$PATH"
# Define the log file
LOG_FILE="/var/log/wiki.lib00/api_import.log"
# Execute your Docker command
/usr/local/bin/docker exec ee-php-fpm-8.4.13 php /eeBox/eeProject/app/public/index.php '/data-import/import?source=all' >> "$LOG_FILE" 2>&1
```
2. **Make It Executable**
```bash
chmod +x /path/to/your/run_import_from_lib00.sh
```
3. **Update Your Crontab**
Now, your cron job simply needs to call this script.
```crontab
*/2 * * * * /path/to/your/run_import_from_lib00.sh
```
---
## Conclusion
The `command not found` error in `cron` is almost always caused by a `PATH` environment mismatch. While several solutions exist, **using the absolute path to the command** is the most direct and foolproof method. Understanding the environmental differences between `cron` and your interactive shell is a key step toward becoming a more effective system administrator or developer. We hope this guide from DP@lib00 helps you resolve this issue quickly.
Related Contents
macOS RAM Disk Deep Dive: Is Memory Allocation Dynamic or Fixed?
Duration: 00:00 | DP | 2026-07-09 20:02:36Resolving PHP "could not find driver" Error: Ultimate Guide to Missing PDO Database Drivers
Duration: 00:00 | DP | 2026-07-04 08:03:00Declutter Your Desktop: How to Change macOS Screenshot Save Location via Command Line
Duration: 00:00 | DP | 2026-07-10 08:05:12How to Fix "Permission denied" Error When Running Shell Scripts in Mac/Linux
Duration: 00:00 | DP | 2026-07-06 08:54:30macOS Advanced Guide: How to Elegantly Set Programs to Run at Startup
Duration: 00:00 | DP | 2026-07-07 09:20:15Resolving Nginx Permission Denied (13) Errors for WebP Images Generated by PHP Imagick
Duration: 00:00 | DP | 2026-07-05 21:17:00Boost Mac Productivity: How to Set F1-F12 as Standard Function Keys on macOS
Duration: 00:00 | DP | 2026-07-12 08:15:37Bypassing macOS Restrictions: How to Set a 1-Digit Login Password
Duration: 00:00 | DP | 2026-07-13 08:20:49How to Fix Mac mini Not Receiving SMS Messages and iCloud Sync Stuck
Duration: 00:00 | DP | 2026-07-06 22:07:00Complete Guide to Installing and Configuring Git Server on Synology NAS: Basic to Advanced
Duration: 00:00 | DP | 2026-07-16 20:39:02Why Do .smbdelete Hidden Files Appear After Deleting on Mac SMB Shares? Causes & Ultimate Solutions
Duration: 00:00 | DP | 2026-06-27 19:10:00Complete Guide to Setting Docker Container Timezone to UTC+8 (Asia/Shanghai)
Duration: 00:00 | DP | 2026-06-30 20:43:30How to Prevent Apple Compressor from Replacing Dots with Underscores in Filenames
Duration: 00:00 | DP | 2026-07-17 20:44:15Stop Using rm! 5 Pro Ways to Quickly Clear Log Files in Linux
Duration: 00:00 | DP | 2026-07-18 20:49:27Ultimate Guide to Fixing Nginx [warn] conflicting server name Warning
Duration: 00:00 | DP | 2026-07-21 09:02:28Never Lose Output Again: How to Show Unlimited Scrollback History in macOS iTerm2
Duration: 00:00 | DP | 2026-07-21 21:05:04Fixing 'Unable to locate package openjdk-17-jdk' in PHP 8 Docker (Debian Trixie)
Duration: 00:00 | DP | 2026-07-25 09:23:18Docker Compose Advanced: Configuring Static IPs and Cross-Container SOCKS5 Proxies
Duration: 00:00 | DP | 2026-07-26 09:28:30Recommended
Why Encode Hashes with Base64/Base16 After MD5? A Deep Dive into Hashing vs. Encoding
00:00 | 174Many developers are familiar with hashing algorith...
Dynamically Update Page Titles in Vue Router: From Basics to i18n and TypeScript
00:00 | 171Still manually updating page titles in your Vue ap...
The Ultimate CSS Flexbox Guide: Easily Switch Page Header Layouts from Horizontal to Vertical
00:00 | 116This article provides a deep dive into a common CS...
Mastering Markdown Spacing: The Ultimate Guide to Controlling Your Document Layout
00:00 | 192Ever struggled with adjusting the vertical spacing...