Make sure to install docker and docker-compose before proceeding with the application.
Create .env file for managing environment configuration for MySQL.
Sample db.env
:
MYSQL_ROOT_PASSWORD=root-password
MYSQL_USER=basic-user
MYSQL_PASSWORD=basic-user-password
MYSQL_DATABASE=database-name
This file can be placed any where in the project, but the default docker-compose expects the file at ./config/db.env
.
Note: Initialize dotnet user secrets if you have never used dotnet secrets.
Create user sceret for accessing the connectionstring established in the db.env
. This command should be run from the /api folder.
dotnet user-secrets set ConnectionStrings.docker-app "Server=db-container;Port=db-port;Database=database-name;Uid=basic-user;Pwd=basic-user-password"
This is not required as there are many other options for database management. But migrations are a stanard functionality in EF.
dotnet ef migrations add InitialCreate
Add DBContext and connectionstring information to Startup.cs
services.AddDbContext<DockerAppContext>(options =>
options.UseMySQL(Configuration["ConnectionStrings.docker-app"]));
Install the aspnet-codegenerator tool in order to quickly scaffold controllers based off the EF context.
aspnet-codegenerator controller -name UserController -api -m User -dc DockerAppContext --relativeFolderPath Controllers/v1
The default VS Code task executes the following:
docker compose up --build --force-recreate --detach