Skip to content

Add Docker Compose support with persistent SQLite volume #239

@nanotaboada

Description

@nanotaboada

Description

Currently, the project uses manual Docker build/run commands and stores the SQLite database inside the container filesystem. This violates container immutability and doesn’t persist data between runs.

Proposed Solution

Introduce Docker Compose to:

  • Automate image building and container orchestration
  • Mount a Docker-managed volume to persist the SQLite database across container restarts
  • Copy a pre-seeded players-sqlite3.db from the image to the volume only on first run

Suggested Implementation

docker-compose.yml

services:
  api:
    image: dotnet-samples-aspnetcore-webapi
    container_name: aspnetcore-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "9000:9000"
    volumes:
      - storage:/storage/
    environment:
      - STORAGE_PATH=/storage/players-sqlite3.db
    restart: unless-stopped

volumes:
  storage:

Dockerfile

# Entrypoint script and image-bundled, pre-seeded SQLite database
COPY --chown=root:root --chmod=755      scripts/entrypoint.sh       ./entrypoint.sh
COPY --chown=root:root --chmod=755      storage                     ./docker-compose/storage

# Install SQLite runtime libs, add non-root user and prepare volume mount point
RUN adduser --disabled-password --gecos "" aspnetcore && \
    mkdir -p /storage && \
    chown -R aspnetcore:aspnetcore /storage

scripts/entrypoint.sh

#!/bin/sh
set -e

IMAGE_STORAGE_PATH="/app/docker-compose/players-sqlite3.db"
VOLUME_STORAGE_PATH="/storage/players-sqlite3.db"

echo "✔ Starting container..."

if [ ! -f "$VOLUME_STORAGE_PATH" ]; then
    echo "⚠️ No existing database file found in volume."
    if [ -f "$IMAGE_STORAGE_PATH" ]; then
        echo "Copying database file to writable volume..."
        cp "$IMAGE_STORAGE_PATH" "$VOLUME_STORAGE_PATH"
        echo "✔ Database initialized at $VOLUME_STORAGE_PATH"
    else
        echo "⚠️ Database file missing at $IMAGE_STORAGE_PATH"
        exit 1
    fi
else
    echo "✔ Existing database file found. Skipping seed copy."
fi

echo "✔ Ready!"
echo "🚀 Launching app..."
exec "$@"

Acceptance Criteria

  • docker compose up builds and runs the container with ASP.NET Core app on port 9000
  • SQLite DB is copied to the volume only if not already present
  • Data persists after docker compose down and is restored on next up
  • docker compose down -v resets the DB, and re-seeds it on next run
  • README.md is updated with Compose usage instructions

Metadata

Metadata

Assignees

Labels

.NETPull requests that update .NET codecontainersPull requests that update containers codeenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions