Home > database >  Binding a local directory to java application container
Binding a local directory to java application container

Time:05-01

Hello I'm a beginner to docker.

I'm saving my output file in the current directory using the java code as follows :

File currDir = new File(".");
String path = currDir.getAbsolutePath();
String fileLocation = path.substring(0, path.length() - 1)   "SavedFile.xlsx";
FileOutputStream outputStream = new FileOutputStream(fileLocation);
workbook.write(outputStream);
System.out.println("Excel file created.........");
workbook.close();

I've built a DockerImage and and executed the container. I know that once the container finishes execution all the files are lost from it.

I tried to map my local directory to the container using the following command on my mac terminal

docker run --name mycontainer -v /users/myname/Desktop/OutputFolder : / myimage

or

docker run --name mycontainer -v /users/myname/Desktop/OutputFolder : / -it myimage

I'm getting the following message:

docker: invalid reference format.

The dockerfile associated with this as follows :

# we will use openjdk 17
FROM openjdk:17

# copy the packaged jar file into our docker image
COPY out/artifacts/FileOnDocker_jar/FileOnDocker.jar /demo.jar

# set the startup command to execute the jar
CMD ["java", "-jar", "/demo.jar"]

I tried logging into the container

myname@xyz DockerTest % docker exec -it mycontainer /bin/bash

bash-4.4# pwd
/

Due to this reason I used / as mapping the external directory to the container directory int he docker run commands above.

bash-4.4# ls -al
total 21156
drwxr-xr-x   1 root root     4096 Apr 30 11:47 .
drwxr-xr-x   1 root root     4096 Apr 30 11:47 ..
-rwxr-xr-x   1 root root        0 Apr 30 11:47 .dockerenv
lrwxrwxrwx   1 root root        7 Oct  9  2021 bin -> usr/bin
dr-xr-xr-x   2 root root     4096 Oct  9  2021 boot
-rw-r--r--   1 root root 21602227 Apr 30 08:07 demo.jar
drwxr-xr-x   5 root root      360 Apr 30 11:47 dev
drwxr-xr-x   1 root root     4096 Apr 30 11:47 etc
drwxr-xr-x   2 root root     4096 Oct  9  2021 home
lrwxrwxrwx   1 root root        7 Oct  9  2021 lib -> usr/lib
lrwxrwxrwx   1 root root        9 Oct  9  2021 lib64 -> usr/lib64
drwxr-xr-x   2 root root     4096 Oct  9  2021 media
drwxr-xr-x   2 root root     4096 Oct  9  2021 mnt
drwxr-xr-x   2 root root     4096 Oct  9  2021 opt
dr-xr-xr-x 188 root root        0 Apr 30 11:47 proc
dr-xr-x---   1 root root     4096 Apr 27 21:54 root
drwxr-xr-x   4 root root     4096 Apr 26 19:36 run
lrwxrwxrwx   1 root root        8 Oct  9  2021 sbin -> usr/sbin
drwxr-xr-x   2 root root     4096 Oct  9  2021 srv
dr-xr-xr-x  13 root root        0 Apr 30 11:47 sys
drwxrwxrwt   1 root root     4096 Apr 27 21:54 tmp
drwxr-xr-x   1 root root     4096 Apr 27 21:54 usr
drwxr-xr-x   1 root root     4096 Apr 26 19:36 var
bash-4.4# 

Can anyone help me where I'm making mistake?

Thanks in advance.

Adding the console code :

Console console = System.console();
        System.out.println("Please enter your username: ");
        String username = console.readLine();
        System.out.println("Please enter your password: ");
        char[] passwordChars = console.readPassword();
        String password = new String(passwordChars);

CodePudding user response:

The first thing is that you can't have spaces in the volume mapping option. I.e.

docker run --name mycontainer -v /users/myname/Desktop/OutputFolder : / myimage

should be

docker run --name mycontainer -v /users/myname/Desktop/OutputFolder:/ myimage

But that won't work because you're mapping your host directory onto the root of the image. When you do that, you 'hide' everything in the root directory and below it in the container, including your .jar file.

What you can do is map a single file onto the output file. As far as I can see, the file name of the output file in the container will be /SavedFile.xlsx. For this to work, the file must exists on the host before you start the container, so with the following 2 commands

touch /users/myname/Desktop/OutputFolder/SavedFile.xlsx
docker run --name mycontainer -v /users/myname/Desktop/OutputFolder/SavedFile.xlsx:/SavedFile.xlsx myimage

it should work and you should get your output in /users/myname/Desktop/OutputFolder/SavedFile.xlsx on the host.

CodePudding user response:

So to finally summarise Hans's answer:

  1. No need to spaces when mapping is being done at

docker run --name mycontainer -v /Users/myname/Desktop/OutputFolder/SavedFile.xlsx:/SavedFile.xlsx myimage

2: If any file is outputted from the java application then make sure that the same file with the same name must be created beforehand in the local directory so as to map it from the container. As a result the file in the local directory will have the required data post execution of the container.(eg: In my case I named it SavedFile.xlsx)

3: When you are using Console to accept username or password in java based applications inside the container use -it to enable the console to accept the username and password from keyboard else a nullpointer exception will thrown.

As a result the final command will be as follows :

docker run -it --name mycontainer -v /Users/myname/Desktop/OutputFolder/SavedFile.xlsx:/SavedFile.xlsx myimage

Any others suggestions/corrections are welcomed.

  • Related