Resolved

Docker init container fails with missing script error

Docker
DevOps
CI/CD
Backend
Debugging
Containerization
Build
Infrastructure

Init container crashes with 'no such file or directory' when script exists in source but is excluded from Docker image | Caused by .dockerignore blocking required scripts directory

Issue

The Docker-based backend system failed during startup due to the unavailability of critical initialization scripts required by the init container. Despite the scripts existing in the source repository (rag-backend/scripts/), they were not included in the production Docker image, causing the init container to crash with an error indicating that the script file was not found.

Culprit

Two configuration errors combined to exclude the scripts from the Docker image:

  1. Overly Aggressive .dockerignore: The exclusion pattern for rag-backend/ inadvertently excluded the entire directory, including the necessary scripts/ subdirectory.
  2. Missing Dockerfile Copy Command: The production stage of the Dockerfile did not include instructions to copy the scripts to their required location in the image (/app/scripts/).

The build process completed successfully but resulted in an image missing essential runtime dependencies.

Solution

The issue was resolved through the following steps:

  1. Updated .dockerignore: Modified the exclusion pattern to explicitly allow the scripts/ directory while still excluding other files in the rag-backend/ directory.

  2. Enhanced Dockerfile: Added critical steps to the Dockerfile to ensure the scripts were included in the image. This included installing necessary tools (git) and explicitly copying the scripts to the correct location with proper file ownership.

  3. Rebuilt and Pushed Docker Image: The corrected Docker image was rebuilt and pushed as version v1.0.1. The init container now successfully starts, executes the scripts, and completes initialization without errors.

These changes ensured that all necessary scripts were included in the Docker image, enabling the system to function as intended.