Wednesday, March 18, 2020

dotnet ef Command Doesn't Work inside a Docker Container

I was facing this issue where I needed to run dotnet ef command inside a docker container and it's not recognizing the command.

Basically, my Dockerfile looked something like.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
RUN dotnet tool install -g dotnet-ef --version 3.1.1 
RUN export PATH="$PATH:/root/.dotnet/tools"
RUN dotnet ef
I was installing dotnet-ef command, and I was setting the path. But still, I was getting this error,
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-ef does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
I have been trying different things for a couple of hours, finally, thanks to Petr Onderka with his brilliant answer managed to solve the issue. The reason is for it's to not work is the environment variables set by export don't survive across directives. What we need to do is to use the ENV directive instead.

And this worked.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
RUN dotnet tool install -g dotnet-ef --version 3.1.1 
ENV PATH $PATH:/root/.dotnet/tools
RUN dotnet ef --version
Thanks Petr Onderka again.

I hope this saves someone's time.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment