lbednaszynski
lbednaszynski

Reputation: 698

Docker - cannot move file between volumes from java

I have two docker containers: producer and consumer.

Consumer container has two volumes:

VOLUME      ["/opt/queue/in", "/opt/queue/out"]

docker-compose.yml

consumer:
    image: consumer
producer:
    image: producer
    volumes_from:
    - consumer

Producer puts file in /opt/queue/in directory and consumer reads file from that dir and moves it to the /opt/queue/out. The problem is that consumer is written in Java and following Java code returns -1 (operation failed).

new File('/opt/queue/in/in_file').renameTo(new File('/opt/queue/in/in_file'));

When I try to move file from command line there is no error. File is moved correctly. Why this is happening? How can I diagnose what is the problem?

Upvotes: 6

Views: 1248

Answers (2)

Mugeesh Husain
Mugeesh Husain

Reputation: 428

As above mentioned, rename method will not work on docker mount, so You use this copyfile if you are using older version of java like java6. java8 have lots of way to move file

try { 
      FileUtils.copyFile(oldfile,newfile);
   } catch (Exception e) { e.printStackTrace(); }

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122414

The javadoc for File.renameTo specifically says that it may not be able to move a file between different volumes, and that you should use Files.move if you need to support this case in a platform independent way.

Upvotes: 10

Related Questions