Root — Dockerfile

Module: root-dockerfile Cohesion: 0.80 Members: 0

Root — Dockerfile

The Dockerfile module defines the build process and runtime environment for the Code Buddy application within Docker containers. It employs a multi-stage build strategy to create optimized, secure, and consistent images for production, alongside a dedicated stage for development.

Unlike typical code modules, the Dockerfile does not contain executable code in the traditional sense. Instead, it's a declarative script that orchestrates the environment setup, dependency installation, application compilation, and final image assembly. Therefore, it has no internal calls, outgoing calls, or execution flows as detected for runtime code.

Purpose

The primary goals of this Dockerfile are:

  1. Containerization: Package the Code Buddy application and its dependencies into a portable, self-contained unit.
  2. Environment Consistency: Ensure that the application runs in the same environment across different machines (development, testing, production).
  3. Production Optimization: Create a lean, secure, and efficient production image by separating build-time concerns from runtime requirements.
  4. Development Workflow: Provide a convenient Docker environment for developers to build, test, and debug the application with hot-reloading capabilities.
  5. Cross-Architecture Support: Explicitly designed to support AMD64 and ARM64 architectures.

Architecture: Multi-Stage Build

The Dockerfile utilizes a multi-stage build pattern, which is crucial for minimizing the final image size and improving security. This approach involves multiple FROM instructions, where each FROM starts a new build stage. Artifacts from previous stages can be selectively copied into subsequent stages.

graph TD
    A[node:20-bookworm] --> B(Stage: builder)
    B -- Copy artifacts --> C(Stage: production)
    A --> D(Stage: development)

    subgraph Build Process
        B -- npm ci --> B1[Install all dependencies]
        B1 -- npm run build --> B2[Compile TypeScript]
        B2 -- npm prune --production --> B3[Remove dev dependencies]
    end

    subgraph Production Image
        C -- node:20-bookworm-slim --> C1[Minimal base image]
        C1 -- apt-get install --> C2[Runtime dependencies]
        C2 -- useradd codebuddy --> C3[Non-root user]
        C3 -- COPY --from=builder --> C4[App artifacts]
        C4 -- ENTRYPOINT --> C5[Run app]
    end

    subgraph Development Image
        D -- npm ci --> D1[Install all dependencies]
        D1 -- COPY . . --> D2[Source code]
        D2 -- npm run dev:node --> D3[Run dev server]
    end

This structure ensures that:

Build Stages Explained

Stage 1: builder

  1. Install Build Dependencies: RUN apt-get update && apt-get install -y python3 make g++

  1. Install Node.js Dependencies: COPY package*.json ./ followed by RUN npm ci

  1. Copy Source Code: COPY . .

  1. Build TypeScript: RUN npm run build

  1. Prune Dev Dependencies: RUN npm prune --production

Stage 2: production

  1. OCI Labels: LABEL org.opencontainers.image.*

  1. Install Runtime Dependencies: RUN apt-get update && apt-get install -y --no-install-recommends git ripgrep curl ca-certificates

  1. Non-Root User: RUN useradd -m -s /bin/bash -u 1001 codebuddy and USER codebuddy

  1. Copy Built Application: COPY --from=builder --chown=codebuddy:codebuddy /app/dist ./dist

  1. Configuration Directories: RUN mkdir -p /home/codebuddy/.codebuddy /home/codebuddy/data

  1. Environment Variables: ENV NODE_ENV=production, ENV HOME=/home/codebuddy, ENV CODEBUDDY_HOME=/home/codebuddy/.codebuddy

  1. Health Check: HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 CMD curl -f http://localhost:3000/api/health 2>/dev/null || exit 0

  1. Exposed Port: EXPOSE 3000

  1. Entry Point: ENTRYPOINT ["node", "/app/dist/index.js"]

  1. Default Command: CMD ["--help"]

Stage 3: development

  1. Install Dev Dependencies: RUN apt-get update && apt-get install -y --no-install-recommends git ripgrep python3 make g++ curl

  1. Install Node.js Dependencies: COPY package*.json ./ followed by RUN npm ci

  1. Copy Source (Initial): COPY . .

  1. Environment: ENV NODE_ENV=development

  1. Exposed Ports: EXPOSE 3000 5173

  1. Dev Entry Point: CMD ["npm", "run", "dev:node"]

Usage

Building the Production Image

To build the optimized production image:

docker build -t codebuddy:latest .

This command will execute both the builder and production stages, resulting in a codebuddy:latest image that is ready for deployment.

Running the Production Image

To run the production image:

docker run -p 3000:3000 -e GROQ_API_KEY="your_api_key" -v codebuddy_data:/home/codebuddy/data codebuddy:latest

Building the Development Image

To build the development image:

docker build --target development -t codebuddy:dev .

The --target development flag explicitly tells Docker to build only up to the development stage.

Running the Development Image

To run the development image, typically with source code mounted from the host:

docker run -p 3000:3000 -p 5173:5173 -v "$(pwd):/app" -e GROQ_API_KEY="your_api_key" codebuddy:dev

Contribution Guidelines

When making changes that affect the Docker build: