如何使用Java 8在docker容器卷中写入CSV文件? [英] How do write a CSV file in a docker container volume with Java 8?
问题描述
我正在与docker和文件系统进行斗争。我想从我的Java应用程序在docker卷中编写一个文件。主要目标是在同一台机器上运行的另一个应用程序可以读取该文件。
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.
我读了相关的问题,但是我没有找到任何用java应用程序解决这个问题的答案。关于如何做到这一点的任何想法?
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?
推荐答案
在你的另一个问题的答案的基础上:
Building on the answer to your other question:
- How to import a CSV inside a Docker container with Java 8?
因此,您有两个需要共享文件系统的docker容器?假设您的java应用程序是容器化的,请使用它来创建持久数据容器:
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
更新
您尚未说明您是如何构建或使用Java程序的。我假设它是一个可写入或读取CSV文件的可执行jar:
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
这篇关于如何使用Java 8在docker容器卷中写入CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!