Your scheduled task works when you run docker compose exec app node scripts/report.js, but the 02:00 run leaves no report and no useful error. Cron starts with a small environment, and its output often goes somewhere Docker does not collect.
Choose where the schedule should live, then make sure the job gets its environment and sends output somewhere you can inspect. This guide compares a host crontab, cron inside the application image, Supercronic, and Ofelia, with examples for a Compose app on Linux.
Choosing where the schedule lives
Put the schedule as close as possible to the thing that owns the job. A host crontab suits a single server; a scheduler inside an image travels with the app; Ofelia runs commands in other containers. Here is the tradeoff:
| Method | Schedule lives in | Environment | Where output goes | Use it when |
|---|---|---|---|---|
Host cron with docker compose exec | Server crontab | The running container’s configured environment | Host log file or cron mail | One server, simple task |
| Traditional cron in the image | Image or mounted crontab | Must be passed into the cron job | Redirect each job to container output | You need to keep an existing cron setup |
| Supercronic | Image or mounted crontab | Inherits the container environment | Container stdout and stderr | The schedule belongs with one container |
| Ofelia | Compose labels or config | Runs through Docker in the target container | Ofelia and job output in scheduler logs | You schedule jobs across existing containers |
All four approaches need an alert or a check if missing a run matters. A running scheduler only proves that its process is alive, not that a scheduled command succeeded.
Running a command from the host crontab
Use host cron when the server owns the schedule and you want to keep the application image unchanged. This example runs a Node.js script in the already-running app service every day at 02:00:
0 2 * * * cd /srv/myapp && /usr/bin/docker compose exec -T app node /app/scripts/report.js >> /var/log/myapp-report.log 2>&1Install the line with crontab -e for the account that can access Docker. -T disables pseudo-terminal allocation, and the redirection captures both standard output and errors in the host log. Replace /srv/myapp, app, the script path, and the Docker executable path with values from your server.
Cron starts commands with a small PATH, so use absolute executable paths and set the working directory explicitly. The command above runs inside the existing service, so it sees the environment configured for that container. It also needs that container to be running when the schedule fires.
If the job needs a clean container each time instead, use docker compose run --rm --no-deps from the host crontab. That creates a one-off container from the service definition and removes it after the command ends; confirm its command and environment in a manual run before scheduling it.
Running traditional cron inside the image
Keep cron in the image when you need to preserve an existing crontab format or want a single container to own its schedule. With Debian’s cron, install the daemon, add a file under /etc/cron.d, and run cron in the foreground:
FROM node:22-bookworm-slim
RUN apt-get update \ && apt-get install -y --no-install-recommends cron \ && rm -rf /var/lib/apt/lists/*
WORKDIR /appCOPY . .COPY cronjobs /etc/cron.d/app-jobsRUN chmod 0644 /etc/cron.d/app-jobs
CMD ["cron", "-f"]The /etc/cron.d/app-jobs file needs a user field because cron reads system crontabs. Write the full path to Node and redirect the job output to the main process’s file descriptors so Docker can collect it:
SHELL=/bin/shPATH=/usr/local/bin:/usr/bin:/bin0 2 * * * root cd /app && /usr/local/bin/node /app/scripts/report.js > /proc/1/fd/1 2>&1The -f flag keeps cron in the foreground as the container’s main process. This traditional cron setup does not automatically pass the container’s environment variables to each job; use Supercronic below if the task relies on variables from Compose. The /proc/1/fd/1 redirect assumes the job runs as root, as shown here.
Running Supercronic as the container process
Supercronic reads a crontab and is designed for containers. It passes the environment inherited by the container to jobs and writes job output and status to stdout or stderr, which makes docker logs useful without per-line redirection. Install a pinned Supercronic release in your image using the official installation instructions.
Create /app/crontab with a standard five-field schedule and no username column:
*/5 * * * * cd /app && node /app/scripts/report.jsRun the same application image as a separate scheduler service. The example passes the .env file to the job container and starts Supercronic as its main process:
services: scheduler: image: example/myapp:1.2.0 env_file: - .env command: ['supercronic', '/app/crontab'] restart: unless-stoppedReplace the image with the one that contains your application code, Supercronic, and the crontab. Because this service uses the same image, the scheduled script and its dependencies stay in step with the deployed app. It is a separate container, so it can run the schedule without installing a cron daemon in the web process.
Check the scheduler’s output after it has had a chance to run:
docker compose logs --since=10m schedulerSupercronic logs the schedule and job status along with the command’s output. If the app needs a particular timezone, set it in the scheduler container and confirm that the image has timezone data installed; otherwise set the server and schedule expectations to UTC.
Scheduling commands in other containers with Ofelia
Use Ofelia when one scheduler needs to run commands inside more than one existing container. Add labels to the target service and run Ofelia with access to the Docker socket:
services: app: image: example/myapp:1.2.0 env_file: - .env labels: ofelia.enabled: 'true' ofelia.job-exec.report.schedule: '0 0 2 * * *' ofelia.job-exec.report.command: 'node /app/scripts/report.js'
ofelia: image: mcuadros/ofelia:0.3.22 command: daemon --docker depends_on: - app volumes: - /var/run/docker.sock:/var/run/docker.sock:ro restart: unless-stoppedOfelia’s 0.3.x schedule starts with seconds, so 0 0 2 * * * means 02:00:00. The job runs in the app container and inherits its environment. Pin the Ofelia image to a release that you have checked against its job configuration docs.
Mounting the Docker socket gives the scheduler access to the Docker API. A read-only mount does not make Docker API operations read-only; only give this socket to a scheduler you trust. Start Ofelia after the labeled service so it can read the labels at startup.
Passing environment variables and capturing output
Test the exact command with the same user and environment the scheduled job receives. A command that works in your interactive shell can fail under cron because the executable path, working directory, or variables differ.
For a host cron job using docker compose exec, the command runs in the existing container, so values from its Compose environment or env_file are present. For a new one-off container, pass an env file or Compose service configuration explicitly. Traditional cron inside a Debian image starts jobs with a reduced environment; avoid relying on container variables being present there. Supercronic inherits them, while Ofelia’s job-exec runs in the target container.
Send output to a place the scheduler actually reads. Host cron needs file redirection or mail configured on the server; traditional cron needs a redirect such as /proc/1/fd/1; Supercronic writes to the container logs; Ofelia reports job runs through its own logs. After a manual run, check the exit code and output before waiting for the scheduled run.
Troubleshooting a job that does not run
Check the schedule syntax first. Standard cron uses five fields for minute, hour, day of month, month, and day of week. Ofelia 0.3.x labels use six fields with seconds first; copying the five-field expression into that label changes its meaning.
Then check the scheduler process, timezone, and command context. For Compose services, these commands show whether the scheduler is up and what it logged:
docker compose psdocker compose logs --tail=100 schedulerFor host cron, inspect the host’s cron service logs and the output file configured on the crontab line. Confirm the cron entry uses the right account, absolute paths, working directory, and service name. If the job takes longer than its interval, prevent overlapping runs with a lock or configure a scheduler that waits for the previous run to finish.
Finally, make the job report success somewhere outside the process that runs it. For a task that must run reliably, send a heartbeat only after success or check a timestamp/result in the application. A healthy container does not prove that its scheduled command completed.
Conclusion
You chose a scheduler based on where the job belongs, and routed its environment and output to places you can check. Host cron is a good fit for a small server, Supercronic suits schedules packaged with one service, and Ofelia schedules commands across existing containers.
For related reading:
- Run a Linux server health-check script on a schedule, for cron, output, and failure alerts on the host.
- Read and schedule server commands in Serversinc, for recurring host commands and their run history.
Serversinc lets you schedule shell commands on a server and inspect each run’s status, exit code, and output in command history. Use it for short host tasks; commands run as root and have a two-minute timeout.