简体   繁体   中英

Accessing CSV files on host from .NET Core App in Docker container

I am having a trouble to access CSV files on host from ASP.NET Core Web API app running on docker container. The Web app works in Release mode according to the Dockerfile in the official document . Here is my Dockerfile and docker-compose.yml

aspnetapp
|- MyApp
|  |- MyApp.csproj
|- Dockerfile
|- data
|  |- user_input.csv (I want to access here from .NET App on docker)
...

Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80 
EXPOSE 443
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs
COPY ["MyApp/MyApp.csproj", "MyApp/"]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]

docker-compose.yml

version: "3"
services:
  aspnetapp:
    image: aspnetapp-img
    build: ./aspnetapp
    container_name: aspnetapp-ctr
    environment:
      ConnectionStrings__DefaultConnection: "server=mysql; uid=root; pwd=P@ssw0rd!; database=mydb;"
    ports:
      - "5000:80"
    depends_on:
      - mysql
  mysql:
    image: mysql-img
    ...

(MyApp also uses mysql on docker, but I've omitted this part because it's not relevant to this question)

To access CSV files on host ( user_input.csv ) from MyApp container, I assume that I should mount aspnetapp/data to somewhere on aspnetapp-img . If the CSV file changes by user while running the container, the app should access the new file, so I need to mount it instead of COPY in Dockerfile.

Here's where my question arose.

  1. Where do I mount on the docker container?
  2. How to access the file from controller?
[ApiController]
[Route("api/[controller]")]
public class FileController : ControllerBase
{
  public IActionResult AccessCsvFile()
  {
    var filePath = Path.Combine(_appEnvironment.ContentRootPath, "user_input.csv");
    ...

Can I access the csv file from the controller in this way?

Multiple options:

  1. Command line
docker run -v <relative/absolute path/data/user_input.csv>:/data/input.csv 

  1. Docker-compose
   volumes:
      - <relative/absolute path/data/user_input.csv>:/data/input.csv 

more here: https://docs.docker.com/storage/volumes/ and https://docs.docker.com/compose/#preserve-volume-data-when-containers-are-created

If you want to use Azure: https://github.com/Azure/azurefile-dockervolumedriver

And other plugins: https://docs.docker.com/engine/extend/legacy_plugins/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM