{"id":49587290,"url":"https://github.com/theastralprogrammer0/aspnetcore-todo-docker","last_synced_at":"2026-05-03T23:03:32.699Z","repository":{"id":274104886,"uuid":"921422642","full_name":"theAstralProgrammer0/aspnetcore-todo-docker","owner":"theAstralProgrammer0","description":"This is where I put all learning experience from ASPNETCORE to practice by building a ToDo Web API using docker containers","archived":false,"fork":false,"pushed_at":"2025-02-02T21:09:53.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T22:19:31.223Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theAstralProgrammer0.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-23T23:17:15.000Z","updated_at":"2025-02-02T21:09:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"79e0eaf3-36ae-476e-a434-03fe0837d9c7","html_url":"https://github.com/theAstralProgrammer0/aspnetcore-todo-docker","commit_stats":null,"previous_names":["theastralprogrammer0/aspnetcore-todo-docker"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/theAstralProgrammer0/aspnetcore-todo-docker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theAstralProgrammer0%2Faspnetcore-todo-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theAstralProgrammer0%2Faspnetcore-todo-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theAstralProgrammer0%2Faspnetcore-todo-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theAstralProgrammer0%2Faspnetcore-todo-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theAstralProgrammer0","download_url":"https://codeload.github.com/theAstralProgrammer0/aspnetcore-todo-docker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theAstralProgrammer0%2Faspnetcore-todo-docker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32587828,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T22:12:39.696Z","status":"ssl_error","status_checked_at":"2026-05-03T22:09:10.534Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-05-03T23:03:31.467Z","updated_at":"2026-05-03T23:03:32.688Z","avatar_url":"https://github.com/theAstralProgrammer0.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Running an ASP.NET Core Web API and SQLite Database in Docker Containers\n\n## Goal\nBuild and run a full-fledged ASP.NET Core Web API backend for a ToDo App using Docker containers, with SQLite as the database. The approach ensures:\n- Persistent data and configurations across container restarts.\n- A fully automated setup using scripts, Dockerfiles, and Docker Compose.\n- No local installations apart from Docker-related tools.\n- Development via CLI only, using Vim as the text editor.\n\n## To Use ```aspnetcore-todo-docker```\n\n### Step 1: Run docker-compose\n```bash\ndocker-compose up --build\n```\n\n### Step 2: Query WebAPI endpoints\n1. **```GET``` all items in the ```todoItems``` table**\n```bash\ncurl -X GET http://localhost:5080/api/todoItems/\n```\n\n2. **```GET``` a specific item in the ```todoItems``` table**\n```bash\ncurl -X GET http://localhost:5080/api/todoItems/{id}\n```\nFor instance:\n```bash\ncurl -X GET http://localhost:5080/api/todoItems/{1}\n```\nOR\n```bash\ncurl -X GET http://localhost:5080/api/todoItems/1\n```\n\n3. **```POST``` a new item in the ```todoItems``` table on the database**\n```bash\ncurl -X POST -H \"Content-type: application/json\" \\\n-d '{\n   id: 4,\n   name: 'New ToDo Item',\n   priority: 2,\n   isCompleted: false,\n   dueDate: \"2023-10-26T18:30:00Z\"\n}' http://localhost:5080/api/todoItems\n```\n\n4. **```UPDATE``` an existing item in the ```todoItems``` table on the database**\n```bash\ncurl -X PUT -H \"Content-type: application/json\" \\\n-d '{\n   id: 4,\n   name: 'Same ToDo Item',\n   description: 'Difference is there is now a description',\n   priority: 1,\n   isCompleted: false,\n   dueDate: \"2027-10-26T11:44:55Z\"\n}' http://localhost:5080/api/todoItems/4\n```\n\n5. **```DELETE``` an existing item from the ```todoItems``` table on the database**\n```bash\ncurl -X DELETE http://localhost:5080/api/todoItems/4\n```\n\n## Steps to Achieve the Goal\n\n### Step 1: Set Up the Project Structure\n1. **Create the Project Directory:**\n   ```bash\n   mkdir aspnetcore-todo-docker \u0026\u0026 cd aspnetcore-todo-docker\n   ```\n\n2. **Generate the ASP.NET Core Web API Project:**\n   ```bash\n   docker run --rm -v $(pwd):/app -w /app mcr.microsoft.com/dotnet/sdk:9.0 \\\n     dotnet new webapi -o TodoApp\n   ```\n\n3. **Navigate to the Project Directory:**\n   ```bash\n   cd TodoApp\n   ```\n\n### Step 2: Define the ToDo Model and Context\n1. **Create the `TodoItem` Model:**\n   Create a `Models` directory and a `TodoItem.cs` file:\n   ```bash\n   mkdir Models \u0026\u0026 vim Models/TodoItem.cs\n   ```\n   Add the following code:\n   ```csharp\n   namespace TodoApp.Models\n   {\n       public class TodoItem\n       {\n           public int Id { get; set; }\n           public string Name { get; set; }\n           public bool IsComplete { get; set; }\n       }\n   }\n   ```\n\n2. **Create the `TodoContext` Class:**\n   ```bash\n   vim Models/TodoContext.cs\n   ```\n   Add:\n   ```csharp\n   using Microsoft.EntityFrameworkCore;\n\n   namespace TodoApp.Models\n   {\n       public class TodoContext : DbContext\n       {\n           public TodoContext(DbContextOptions\u003cTodoContext\u003e options) : base(options) { }\n\n           public DbSet\u003cTodoItem\u003e TodoItems { get; set; }\n       }\n   }\n   ```\n\n### Step 3: Configure SQLite in ASP.NET Core\n1. **Install Entity Framework Core SQLite Provider:**\n   Add the package via Docker:\n   ```bash\n   docker run --rm -v $(pwd):/app -w /app mcr.microsoft.com/dotnet/sdk:9.0 \\\n     dotnet add package Microsoft.EntityFrameworkCore.Sqlite\n   ```\n\n2. **Update `appsettings.json` to Use SQLite:**\n   ```bash\n   vim appsettings.json\n   ```\n   Add:\n   ```json\n   {\n     \"ConnectionStrings\": {\n       \"DefaultConnection\": \"Data Source=Todo.db\"\n     }\n   }\n   ```\n\n3. **Register the Database Context:**\n   Update `Program.cs`:\n   ```csharp\n   using Microsoft.EntityFrameworkCore;\n   using TodoApp.Models;\n\n   var builder = WebApplication.CreateBuilder(args);\n\n   // Add services to the container.\n   builder.Services.AddControllers();\n   builder.Services.AddDbContext\u003cTodoContext\u003e(options =\u003e\n       options.UseSqlite(builder.Configuration.GetConnectionString(\"DefaultConnection\")));\n\n   var app = builder.Build();\n\n   app.MapControllers();\n\n   app.Run();\n   ```\n\n### Step 4: Create the Todo Controller\n1. **Add the Controller:**\n   ```bash\n   mkdir Controllers \u0026\u0026 vim Controllers/TodoController.cs\n   ```\n   Add:\n   ```csharp\n   using Microsoft.AspNetCore.Mvc;\n   using TodoApp.Models;\n   using System.Linq;\n\n   namespace TodoApp.Controllers\n   {\n       [ApiController]\n       [Route(\"api/[controller]\")]\n       public class TodoController : ControllerBase\n       {\n           private readonly TodoContext _context;\n\n           public TodoController(TodoContext context)\n           {\n               _context = context;\n           }\n\n           [HttpGet]\n           public IActionResult GetAll() =\u003e Ok(_context.TodoItems.ToList());\n\n           [HttpPost]\n           public IActionResult Create(TodoItem item)\n           {\n               _context.TodoItems.Add(item);\n               _context.SaveChanges();\n               return CreatedAtAction(nameof(GetAll), new { id = item.Id }, item);\n           }\n       }\n   }\n   ```\n\n### Step 5: Prepare Docker Infrastructure\n1. **Create the Dockerfile:**\n   ```bash\n   vim Dockerfile\n   ```\n   Add:\n   ```dockerfile\n   FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build\n   WORKDIR /app\n   COPY . .\n   RUN dotnet restore\n   RUN dotnet publish -c Release -o /out\n\n   FROM mcr.microsoft.com/dotnet/aspnet:9.0\n   WORKDIR /app\n   COPY --from=build /out .\n   ENTRYPOINT [\"dotnet\", \"TodoApp.dll\"]\n   ```\n\n2. **Create the Docker-Compose File:**\n   ```bash\n   vim docker-compose.yml\n   ```\n   Add:\n   ```yaml\n   version: '3.8'\n   services:\n     todoapi:\n       build: .\n       ports:\n         - \"5000:5000\"\n       volumes:\n         - ./data:/app/data\n       environment:\n         - ASPNETCORE_ENVIRONMENT=Development\n       depends_on:\n         - db\n\n     db:\n       image: sqlite\n       volumes:\n         - ./data:/data\n   ```\n\n3. **Run the Containers:**\n   ```bash\n   docker-compose up --build\n   ```\n\n### Step 6: Test and Validate\n1. **Test the API:**\n   Use `curl` or Postman to interact with `http://localhost:5000/api/todo`.\n\n2. **Validate Persistence:**\n   Stop and restart the containers to confirm data persistence.\n\n### Step 7: Automate Setup\n1. **Create a Setup Script:**\n   ```bash\n   vim setup.sh\n   ```\n   Add:\n   ```bash\n   #!/bin/bash\n   set -e\n\n   docker-compose down\n   docker-compose up --build\n   ```\n\n2. **Run the Script:**\n   ```bash\n   chmod +x setup.sh\n   ./setup.sh\n   ```\n\n### Conclusion\nThis setup enables a lightweight, CLI-based development environment for your ASP.NET Core Web API and SQLite database. All configurations and data persist across container restarts, ensuring a smooth and efficient development workflow.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheastralprogrammer0%2Faspnetcore-todo-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheastralprogrammer0%2Faspnetcore-todo-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheastralprogrammer0%2Faspnetcore-todo-docker/lists"}