Dan
Dan

Reputation: 2546

.NET Core web app won't run in Docker container

I have a vanilla .NET Core 2 web app that I setup in JetBrains Rider and I immediately started working on a Docker environment for it. I followed this guide to get started:

https://docs.docker.com/engine/examples/dotnetcore/

I altered it slightly to come up with this:

FROM microsoft/dotnet:latest AS packager

RUN mkdir -p /opt/build
WORKDIR /opt/build

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

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

# --

# Build runtime image
FROM microsoft/dotnet:runtime AS runtime

RUN mkdir -p /opt/app
WORKDIR /opt/app

COPY --from=packager /opt/build/bin/. .

ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]

The image builds and when I go to run the container I get the following output:

dan@mycomputer ~/Desktop/coreapi (master)
$ docker build -t myapp .
Sending build context to Docker daemon  25.09kB
Step 1/12 : FROM microsoft/dotnet:latest AS packager
 ---> e1a56dca783e
Step 2/12 : RUN mkdir -p /opt/build
 ---> Using cache
 ---> 95f9c936d0d1
Step 3/12 : WORKDIR /opt/build
 ---> Using cache
 ---> 64f26c356fd7
Step 4/12 : COPY *.csproj .
 ---> Using cache
 ---> 38a2fb7ca6bb
Step 5/12 : RUN dotnet restore
 ---> Using cache
 ---> 70dbc44d98ae
Step 6/12 : COPY . .
 ---> Using cache
 ---> b1019d53a861
Step 7/12 : RUN dotnet publish -c Release -o bin
 ---> Using cache
 ---> 8e112606633a
Step 8/12 : FROM microsoft/dotnet:runtime AS runtime
 ---> cc240a7fd027
Step 9/12 : RUN mkdir -p /opt/app
 ---> Using cache
 ---> 954f494febc4
Step 10/12 : WORKDIR /opt/app
 ---> Using cache
 ---> b74be941e7dc
Step 11/12 : COPY --from=packager /opt/build/bin/. .
 ---> Using cache
 ---> 4c229192d99b
Step 12/12 : ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]
 ---> Using cache
 ---> fb6ef4015fba
Successfully built fb6ef4015fba
Successfully tagged myapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

dan@mycomputer ~/Desktop/coreapi (master)
$ docker run -p 5001:5001 myapp:latest
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
  http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

The app doesn't run and I get a message that says I need to install the SDK. What's up with that? Shouldn't the runtime Docker image have everything neede to run the app?

Upvotes: 2

Views: 1398

Answers (1)

Dan
Dan

Reputation: 2546

I was able to find the solution by modifying the ENTRYPOINT to run tail -f /dev/null. From there, I entered the container and saw that the name of the binary adjusts based on your project name which the Docker documentation didn't make clear to me.

I also updated my base images and this solved my problem. Here is my latest Dockerfile below:

FROM microsoft/dotnet:2.1-sdk AS packager

RUN mkdir -p /opt/build
WORKDIR /opt/build

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

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

# --

# Build runtime image
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime

RUN mkdir -p /opt/app
WORKDIR /opt/app

COPY --from=packager /opt/build/bin/. .

EXPOSE 80

ENTRYPOINT ["dotnet", "/opt/app/coreapi.dll"]

Upvotes: 1

Related Questions