How can I run dotnet CLI commands inside a running container?

469 views Asked by At

I have a running container called tami-app. The Dockerfile is based on a .NET 5 SDK:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /Tami.Operations.Api

COPY src/Tami.Operations.Domain/Tami.Operations.Domain.csproj /Tami.Operations.Domain/
COPY src/Tami.Operations.Api/Tami.Operations.Api.csproj .
RUN dotnet restore

COPY src/Tami.Operations.Domain /Tami.Operations.Domain
COPY src/Tami.Operations.Api .

RUN dotnet build -c Release
RUN dotnet publish -c Release --no-build -o /Tami.Operations.Api/publish

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS runtime
WORKDIR /Tami.Operations.Api
COPY --from=build /Tami.Operations.Api/publish .
ENTRYPOINT ["dotnet", "Tami.Operations.Api.dll"]

I would like to enter the terminal to run .NET CLI commands such as dotnet ef migrations. So I type docker exec -it tami-app bash and a bash terminal prompt shows up. I then type dotnet --info and it tells me that no SDKs installed.

I would like to use the mcr.microsoft.com/dotnet/sdk:5.0 as part of the container, how do I do that?

I would like to use the dotnet CLI tool to run migrations.

2

There are 2 answers

0
Zvi On BEST ANSWER

The dotnet tools must be installed inside the Dockerfile. The following worked for me:

FROM mcr.microsoft.com/dotnet/sdk:5.0
WORKDIR /app

RUN dotnet tool install --global dotnet-ef --version 5.0.17

ENV PATH="$PATH:/root/.dotnet/tools"

EXPOSE 8010
1
Pieterjan On

To open a terminal to your Docker container, you can use Docker Desktop

Open terminal for Docker Container