Developing ASP.NET Core Applications on macOS with VS Code, Docker, and MySQL
This tutorial explains how to set up a macOS development environment with Visual Studio Code and .NET Core, create an ASP.NET Core web application using Yeoman, configure MySQL via Docker, and package and deploy the app with Docker and docker‑compose.
This article demonstrates how to develop an ASP.NET Core web application on macOS using Visual Studio Code, Docker, and MySQL, providing a step‑by‑step guide from environment setup to production deployment.
Environment preparation : Install Visual Studio Code, the .NET Core SDK, Node.js, npm, Yeoman, the ASP.NET generator, and Docker for Mac. Download links are provided for each tool.
Install front‑end utilities with npm:
npm install bower gulp grunt-cli -gInstall Yeoman and the ASP.NET generator:
npm install yo -g npm install generator-aspnet -gCreate the project : Create a folder, generate the project with Yeoman, restore packages, and open it in VS Code.
mkdir aspnet-mysql
cd aspnet-mysql
mkdir src
cd src
yo aspnetChoose "Web Application [without Membership and Authorization]" and select Bootstrap as the front‑end framework. Then run:
dotnet restore code .VS Code will generate .vscode/launch.json and .vscode/task.json for debugging.
Run MySQL in a Docker container : Set up a Docker host (e.g., Docker Machine) and start a MySQL container for development.
eval $(docker-machine env {machine-name}) docker run --name mysql-dev -e MYSQL_ROOT_PASSWORD=P2ssw0rd -e MYSQL_DATABASE=ef -p 3306:3306 -d mysqlConnect to the container with a MySQL client (e.g., MySQL Workbench) to verify the ef database.
Configure Entity Framework Core to use MySQL : Add the Pomelo MySQL provider and a custom nuget.config because the package is pre‑release.
"Pomelo.EntityFrameworkCore.MySql": "1.0.0-prerelease-20160726" <?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="NuGet official package source" value="https://nuget.org/api/v2/" />
<add key="PomeloMysql" value="https://www.myget.org/F/pomelo/api/v2/"/>
</packageSources>
</configuration> dotnet restore --configfile nuget.configIn Startup.cs add the DB context:
services.AddDbContext<YourContext>(options =>
options.UseMySql(Configuration.GetConnectionString("Mysql")));
await SampleData.InitDB(app.ApplicationServices);Define the MySQL connection string in appsettings.json :
"ConnectionStrings": {
"DefaultConnection": "Data Source=aspnetweb01.db",
"Mysql": "Server={docker machine ip};database=ef;uid=root;pwd=P2ssw0rd;"
}Dockerfile for the application (already generated by Yeoman, minor edits shown):
FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore", "--configfile", "nuget.config"]
RUN ["dotnet", "build"]
EXPOSE 5000/tcp
ENTRYPOINT ["dotnet", "run", "--server.urls", "http://0.0.0.0:5000"]Build and run the image:
docker build -t ups216/aspnet-mysql . docker run --name aspnet-mysql-dev -p 5000:5000 ups216/aspnet-mysqlAccess the app at http://{docker‑host-ip}:5000 .
Production deployment with docker‑compose : Create appsettings.Production.json that points to a MySQL service named db , then define docker-compose.yml with two services (db and web).
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=aspnetweb01.db",
"Mysql": "Server=db;database=ef;uid=ef;pwd=P2ssw0rd;"
}
} version: '2'
services:
db:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: P2ssw0rd
MYSQL_DATABASE: ef
MYSQL_USER: ef
MYSQL_PASSWORD: P2ssw0rd
web:
build: .
depends_on:
- db
links:
- db
ports:
- "5000:5000"
restart: always
environment:
ASPNETCORE_ENVIRONMENT: ProductionRun a single command to start both containers:
docker-compose upAll source code, Dockerfiles, and configuration files are available on GitHub (https://github.com/ups216/aspnet-mysql/).
DevOps
Share premium content and events on trends, applications, and practices in development efficiency, AI and related technologies. The IDCF International DevOps Coach Federation trains end‑to‑end development‑efficiency talent, linking high‑performance organizations and individuals to achieve excellence.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.