问题
I am struggling with docker and the file system. I would like to write a file in a docker volume from my Java application. The main goal is that another application running on the same machine can read the file.
I read the related question, but I did not find any answer solving this with a java application. Any idea on how to do this?
回答1:
Building on the answer to your other question:
- How to import a CSV inside a Docker container with Java 8?
Example
So you have two docker containers with a need to share a file system? Assuming your java application is containerized, use it to create a persistent data container:
$ docker create -v /data --name mydata mydockerimage
Run your containerized programs using this data container
$ docker run -it --rm --volumes-from mydata mydockerimage create "/data/myfile.csv"
$ docker run -it --rm --volumes-from mydata mydockerimage read "/data/myfile.csv"
It's possible to pull files out of the data container:
$ docker cp mydata:/data/myfile.csv myfile.csv
Finally you'll want to cleanup the data container eventually
$ docker rm -v mydata
Update
You have not indicated how you're building or using your java program. I have assumed it's an executable jar that can either write or read a CSV file:
java -jar myjar.jar create "/data/myfile.csv"
java -jar myjar.jar read "/data/myfile.csv"
For an example of how to build such a container see:
- How to build a docker container for a java app
来源:https://stackoverflow.com/questions/34441797/how-do-write-a-csv-file-in-a-docker-container-volume-with-java-8