Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mount volumes .NET Core Docker

I have a following Dockerfile to create a .NET Core 2.1 APP:

FROM microsoft/dotnet:2.1.402-sdk AS builder
WORKDIR /app
# copy csproj and restore as distinct layers
COPY . .
RUN dotnet restore ./myproject.sln
# copy everything else and build
COPY . .
RUN dotnet publish ./myproject/myproject.csproj -c Release -o /app/out

# build runtime image
FROM microsoft/dotnet:2.1.4-aspnetcore-runtime
WORKDIR /app
COPY --from=builder /app/out ./
ENV ASPNETCORE_ENVIRONMENT Production
ENTRYPOINT ["dotnet", "myproject.dll"]

I create the Docker image and the container can be instantiated without any issues. When I try to share data between this container an another one, I then create the following docker-compose file:

version: "2"

services:
  another_app:
    restart: always
    image: another_app:latest
    container_name: another_app
    ports:
      - "4000"
    volumes:
      - shared-folder:/dist

  myproject_app:
    restart: always
    image: myproject:latest
    container_name: myproject
    volumes:
      - shared-folder:/app

volumes:
  shared-folder:

Configuring that way it does not work. I get a weird .NET message for that app:

myproject_app | Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
myproject_app |   http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
another_app| 0|another-a | Node Express server listening on http://localhost:4000 

Now I found the problem goes away when I define the volume NOT in the root of the application. For example, if I do:

myproject_app:
    restart: always
    image: myproject:latest
    container_name: myproject
    volumes:
      - shared-folder:/app/another-folder

It then works. Why cannot you mount a volume at the root level of your .NET Core application and why do you get that error?

like image 265
Carlos Torrecillas Avatar asked Mar 07 '19 09:03

Carlos Torrecillas


1 Answers

I have faced a similar issue and I think I know the reason why :

Configuring that way it does not work. I get a weird .NET message for that app:

myproject_app | Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
myproject_app |   http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
another_app| 0|another-a | Node Express server listening on http://localhost:4000 

This message happens because the dotnet tool couldn't find the .dll file so it treated it as a dotnet subcommand which then throws an error because it requires the dotnet sdk. ( since the last build only contains the runtime )

The Main Problem

The problem lies in the usage of the docker volume shared-folder:/app and WORKDIR being /app. What really happens here is that when the docker volume is connected it overwrites the contents of the container /app directory with the shared-folder directory in the local machine and hence it wasn't able to find the app .dll file and the above error happened.

This is why when you changed it to shared-folder:/app/another-folder it was able to able to work perfectly as it was mapped to an empty directory in the container.

like image 71
Kandil Avatar answered Oct 03 '22 23:10

Kandil