Docker Service Mount Host Machine Directory Not Work

justadeveloper2022
Sun, 20 Aug 2023 09:37:35 +0000
General

I work on a Spring Boot project that connects to remote machine and save files to the machine in a directory. Project works well when running project locally. The project code related to saving files:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.springframework.web.multipart.MultipartFile;

public static final Path ROOT = Paths.get(“\\IP_ADDRESS\Upload\files”);

private static void saveFileToFileSystem(MultipartFile file, String fileName) throws IOException {

Runtime.getRuntime().exec("net use \"\\\\"+IP_ADDRESS+"\" "+USER_PASSWORD+" /user:"+USER_NAME);

Files.copy(file.getInputStream(), ROOT.resolve(fileName));

Runtime.getRuntime().exec("net use \"\\\\"+IP_ADDRESS+"\" /delete");

}

The Dockerfile of the project is below:

FROM openjdk:22-slim-bullseye
ADD target/project.name.jar project.name.jar
ENTRYPOINT [“java”,“-jar”,“/project.name.jar”]

I create Docker service to deploy project on the remote machine, namely Docker service host machine as shown below in PowerShell. However, the service cannot access the host machine file system.

docker service create --name project-service
–replicas 2 --publish mode=host,target=8080,published=9100,protocol=tcp project_image:1.0

Then I try to mount the directory with running the code below in PowerShell.

docker service create --mount type=volume,source=“//e/Upload”,destination=“/home” --name project-service
–replicas 2 --publish mode=host,target=8080,published=9100,protocol=tcp project_image:1.0

But I get the error below:

invalid mount target, must be an absolute path: /home

How to solve the problem?

2 posts - 2 participants