3

My Dockerfile is this

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"]

and I build image with this command

docker build -t watcher/sl_user_test:1.1 .

so, my docker-compose is this

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

In my dotnet core app I get a file in the /in folder and I move it to /tmp/aaa code is this

string destination = $"/tmp/{Guid.NewGuid()}/git.zip";
new FileInfo(destination).Directory.Create();
File.Move("/in/git.zip", destination, true);

the problem is that this command copy file and doesn't move it, why? If I go inside the container I can do mv from bash and it works

Mauro Sala
  • 1,166
  • 2
  • 12
  • 33

2 Answers2

1

Since you are creating a new GUID in the path, the directory is guaranteed to not exist already. Thus File.Move will throw a DirectoryNotFoundException.

Create it before you move the file.

var tmpDir = $"/tmp/{Guid.NewGuid()}";
Directory.CreateDirectory(tmpDir);
File.Move("/in/git.zip", $"{tmpDir}/git.zip", true);

Assuming you are doing that, if the File.Move method just does a copy instead of a move, then it is likely that the original file is in use. From the docs (in the Remarks section):

If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • This is a good tips, but I use this function to verify if file is in use, and always isn't lock https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use – Mauro Sala Nov 22 '19 at 14:38
  • If I run the application directly (without docker) File.Move works – Mauro Sala Nov 22 '19 at 15:16
0

The problem is that I'm trying to move file from a volume to another. If you try this code in GoLang you receive this error

"rename /dir1/test.txt /dir2/test.txt: invalid cross-device link"

So, the solution are

  • write your own FileMove function (with copy and delete source)
  • use only one volume

So, in my case, from this

volumes:
  - /var/storage/in:/in:z
  - /var/storage/tmp:/tmp:z

I'll do this

volumes:
  - /var/storage/data/:/data:z

and in /data there are "in" and "tmp" folders

Mauro Sala
  • 1,166
  • 2
  • 12
  • 33