Running ASP.NET Core in Docker with Vagrant

Goal

Run an ASP.NET Core application in Docker inside a Linux VM, accessible from your host machine.

Create the ASP.NET Core application

dotnet new webapi -n Bustroker.Notes.WebApi

Create Linux VM with Vagrant

Configure Vagrantfile to map port 8080 from VM to host:

config.vm.network "forwarded_port", guest: 8080, host: 8080

Setup Docker

Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app

## Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

## Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o publish

## Build runtime image
FROM mcr.microsoft.com/dotnet/sdk:5.0
WORKDIR /app
COPY --from=build-env /app/publish .
ENTRYPOINT ["dotnet", "Bustroker.Notes.WebApi.dll"]

docker-compose.yml:

services:
  webui:
    environment:
      - ASPNETCORE_ENVIRONMENT=docker-compose
    build: .
    ports:
      - "8080:80"

Port mapping chain: App (80) → VM (8080) → Host (8080)

Run

Inside the VM:

cd /path/to/app
docker-compose up

Access from host browser: localhost:8080