Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

How do write a CSV file in a docker container volume with Java 8?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Building on the answer to your other question:

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...