dotnet核心File.Move问题

问题描述 投票:2回答:1

我的Dockerfile是这个

FROM mcr.microsoft.com/dotnet/core/runtime:3.0

COPY publish/ app/
RUN mkdir -p /in
RUN mkdir -p /tmp

ENTRYPOINT ["dotnet", "app/Watcher.dll"]

并且我使用此命令构建映像

docker build -t watcher/sl_user_test:1.1 .

所以,我的docker-compose是这个

watcher.sl_user_test:
  image: watcher/sl_user_test:1.1
  container_name: watcher_sl_user_test
  build: .
  restart: always
  volumes:
    - /var/storage/in:/in:z
    - /var/storage/tmp:/tmp:z

在我的dotnet核心应用程序中,我在/ in文件夹中得到一个文件,并将其移至/ tmp / aaa代码是这个

File.Move("/in/git.zip", $"/tmp/{Guid.NewGuid()}/git.zip", true);

问题是此命令复制文件并且不移动它,为什么?如果我进入容器,我可以通过bash执行mv,并且可以运行

c# docker .net-core
1个回答
0
投票
using System;
using System.IO;


namespace FileMoveDemo
{
    class Program
    {
        public static void Main()
        {
            string path = @"/app/Source/mytext.txt";
            string path2 = @"/app/Destination/mytext.txt";
            try
            {
                if (!File.Exists(path))
                {
                    // This statement ensures that the file is created,
                    // but the handle is not kept.
                    using (FileStream fs = File.Create(path)) { }
                }

                // Ensure that the target does not exist.
                if (File.Exists(path2))
                    File.Delete(path2);

                // Move the file.
                File.Move(path, path2, true);
                Console.WriteLine("{0} was moved to {1}.", path, path2);

                // See if the original exists now.
                if (File.Exists(path))
                {
                    Console.WriteLine("The original file still exists, which is unexpected.");
                }
                else
                {
                    Console.WriteLine("The original file no longer exists, which is expected.");
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }
    }
}

Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY FileMoveDemo.csproj ./
RUN dotnet restore

# copy and publish app and libraries
COPY ./ ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/runtime:3.0 AS runtime
WORKDIR /app
COPY ./Source/mytext.txt ./Source/
COPY ./Destination/mytext.txt ./Destination/
COPY --from=build /app/out ./
CMD ["dotnet", "FileMoveDemo.dll"]

和文件夹结构

enter image description here

控制台输出:

root@3613fe3391dc:/app# ls -l
total 116
drwxr-xr-x 1 root root  4096 Nov 21 18:05 Destination
-rwxr--r-- 1 root root 86584 Nov 21 18:04 FileMoveDemo
-rw-r--r-- 1 root root   406 Nov 21 18:04 FileMoveDemo.deps.json
-rw-r--r-- 1 root root  5120 Nov 21 18:04 FileMoveDemo.dll
-rw-r--r-- 1 root root   572 Nov 21 18:04 FileMoveDemo.pdb
-rw-r--r-- 1 root root   146 Nov 21 18:04 FileMoveDemo.runtimeconfig.json
drwxr-xr-x 1 root root  4096 Nov 21 18:10 Source
-rw-r--r-- 1 root root     0 Nov 21 18:10 mytext.txt
root@3613fe3391dc:/app# rm mytext.txt 
root@3613fe3391dc:/app# ls -l
total 116
drwxr-xr-x 1 root root  4096 Nov 21 18:05 Destination
-rwxr--r-- 1 root root 86584 Nov 21 18:04 FileMoveDemo
-rw-r--r-- 1 root root   406 Nov 21 18:04 FileMoveDemo.deps.json
-rw-r--r-- 1 root root  5120 Nov 21 18:04 FileMoveDemo.dll
-rw-r--r-- 1 root root   572 Nov 21 18:04 FileMoveDemo.pdb
-rw-r--r-- 1 root root   146 Nov 21 18:04 FileMoveDemo.runtimeconfig.json
drwxr-xr-x 1 root root  4096 Nov 21 18:10 Source
root@3613fe3391dc:/app# touch Source/mytext.txt
root@3613fe3391dc:/app# ls -l Source/
total 0
-rw-r--r-- 1 root root 0 Nov 21 18:10 mytext.txt
root@3613fe3391dc:/app# ls -l Destination/
total 4
-rw-r--r-- 1 root root 13 Nov 21 17:59 mytext.txt
root@3613fe3391dc:/app# dotnet FileMoveDemo.dll 
/app/Source/mytext.txt was moved to /app/Destination/mytext.txt.
The original file no longer exists, which is expected.
root@3613fe3391dc:/app# ls -l Source/
total 0
root@3613fe3391dc:/app# ls -l Destination/
total 0
-rw-r--r-- 1 root root 0 Nov 21 18:10 mytext.txt
root@3613fe3391dc:/app# 
© www.soinside.com 2019 - 2024. All rights reserved.