i have an web api (dotNetCore 3.1) and im trying to Dockerize it.
Things start to get complicated when I want to write files into my base Host. From what I have read this is probably related to defining a "volume" .
Lets say that i have this part of code:
using (StreamWriter sr = new StreamWriter(@"C:\program\tmp.txt"))
{
while (max == -1 || counter < max)
{
try
{
sr.WriteLine($"Counter: {++counter}");
}
catch (Exception)
{
throw;
}
}
}
I manage to build a container and run it properly, but an exception was probably thrown out because he does not know the specified Path.
Here is my Dockerfile:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-nanoserver-1903 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["dockerTest/dockerTest.csproj", "dockerTest/"]
RUN dotnet restore "dockerTest/dockerTest.csproj"
COPY . .
WORKDIR "/src/dockerTest"
RUN dotnet build "dockerTest.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "dockerTest.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "dockerTest.dll"]
I would be happy if you could help me link the host file system to the container.
When you start docker container using the CLI you'll have to use the option to mount a drive. (you might have to play with the host machine path a bit).
$ docker run -d --name devtest -v C:\data\input.data:/data nginx:latest
Once the container starts you don't have to use host path you can
using (StreamWriter sr = new StreamWriter(@"/data/input.data"))
This should work for the most part. With little twists.
You should also consider docker compose files that'll save you a lot of typing and passing too many command line parameters.
More info here:
Docker CLI: https://docs.docker.com/storage/volumes/
Docker Compose: https://docs.docker.com/compose/compose-file/#volume-configuration-reference
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.