tesseract "Error opening data file ./tessdata/eng.traineddata" in a Docker container
Fixes tesseract's 'Error opening data file ./tessdata/eng.traineddata' inside Docker containers. Use when a contract OCR stage works on a dev machine but fails in a container image. Key trigger: TESSDATA_PREFIX points at a path that does not exist in the image.
TL;DR: TESSDATAPREFIX is pointing at a directory that does not exist inside the container, so tesseract falls back to the relative path ./tessdata and finds nothing. Find where eng.traineddata actually lives in the image, set TESSDATAPREFIX to that absolute path, and re-run. One ENV line in the Dockerfile fixes it permanently.
Error opening data file ./tessdata/eng.traineddataConfirm the failure and inspect the variable inside the container:
docker exec [container] sh -c 'echo "TESSDATA_PREFIX=$TESSDATA_PREFIX"; tesseract --version'Expected: TESSDATA_PREFIX is empty or a path that does not exist in the image.Find the real traineddata location:
docker exec [container] find /usr -name "eng.traineddata"Expected: a path like /usr/share/tesseract-ocr/5/tessdata/eng.traineddata.Test the fix in a throwaway run using the path from step 2:
docker exec -e TESSDATA_PREFIX=/usr/share/tesseract-ocr/5/tessdata [container] tesseract scan.png stdoutExpected: OCR text prints instead of the data-file error.Make it permanent in the Dockerfile:
ENV TESSDATA_PREFIX=/usr/share/tesseract-ocr/5/tessdatathen rebuild and re-run. Expected: the error is gone on every run with no manual env flags.If step 2 finds nothing, the language pack is not installed: add
tesseract-ocr-engto the image package install step and rebuild. Expected: eng.traineddata appears under the tessdata directory.
Use this when
- tesseract works on your dev machine but fails in Docker with the data-file error
- the error path starts with ./tessdata
- you built a custom image or switched base images recently
Not for this skill when
- the error names a different language (e.g. fra.traineddata) - that is a missing language pack; install that pack instead
- tesseract is not installed at all in the image (command not found)
- the error happens outside Docker - check the local TESSDATA_PREFIX instead
Variant phrasings
tesseract docker Error opening data file eng.traineddata
tesseract TESSDATA_PREFIX docker not working
tesseract failed loading language eng in container
Why it happens
When TESSDATA_PREFIX is unset or relative, tesseract resolves ./tessdata against the working directory. Local installs usually carry a correct compiled-in default, but Docker images (especially slim or multi-stage builds) often lack the env var or place traineddata somewhere the default does not cover.
Edge cases
- Point TESSDATA_PREFIX at the tessdata directory itself, not its parent; a trailing slash matters on some builds.
- Debian splits the packs:
tesseract-ocralone ships no languages, so installtesseract-ocr-eng(or the pack you need). - If you mount a custom tessdata volume, the mount must land exactly at TESSDATA_PREFIX.
- The error is about location, not model flavor - tessdatafast vs tessdatabest makes no difference here.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstPiqZwtHlr-V8aRMZ3cJzg