如何将应用程序映射到Docker卷

huangapple go评论64阅读模式
英文:

How to map application to docker volume

问题

我是Docker的新手,我一直在尝试在tomcat镜像上运行部署了Java Web应用程序的容器。问题是我的应用程序必须读取并写入在我的机器上访问的文件,显然,当该应用程序的.war文件作为Docker容器部署在tomcat上时,它找不到该文件,因为不再识别该文件。我尝试找出如何解决这个问题,并了解到了Docker卷的概念。我考虑将我的文件存储在卷中,然后让我的应用程序从中检索数据,但我找不到方法来实现这一点。我一直在寻找一种方法,看看如何修改我的应用程序中的路径,使其指向位于Docker卷中的文件,但我找不到任何信息。也许我漏掉了什么,因为我一直在努力了解更多关于Docker卷的内容?请指出正确的做法,或者提供一个有类似操作示范的有用链接,如果我完全误解了这个方法,请告诉我。

英文:

I'm new to docker and I have been trying to run a container that deploys my Java web app on a tomcat image. The problem is my app has to read and write into a file that it accesses on my machine and clearly when the app's .war file is deployed on tomcat as a docker container it cannot find the file since the file is no longer recognized. I was trying to see how I can fix that and came across docker volumes. I have the idea of storing my file in a volume and then having my application retrieve data from it but I cannot find a way to do it. I've been looking for a way to see how I can modify the path in my app to point to a file that's in a docker volume but I cannot find anything. Perhaps I missed something as I've been trying to learn more about docker volumes? Please point out the right way to do this or maybe provide a helpful link that shows how something similar can be done, or if I am completely wrong about this approach please let me know.

答案1

得分: 0

这里是关于卷和挂载的官方文档:
https://docs.docker.com/storage/volumes/

以下是将mysql容器挂载到您的驱动器上的示例,这是一个不错的做法,可以在容器被删除时保留数据。
请查看“存储数据的位置”一节:
https://hub.docker.com/_/mysql/

以下是一个非常简短的Docker Compose示例,将容器内的文件夹挂载到外部文件夹:

volumes:
  - /var/mysql:/var/lib/mysql

*这只是完整docker-compose文件中的两行内容,但应演示了如何挂载数据。

冒号左边的文件夹是驱动器上数据所在的文件夹,
冒号左边的文件夹是容器内部引用的文件夹。

当您在文件夹“./datafolder”中寻找文件时,
您应该将容器内的“./”文件夹挂载到驱动器上包含“datafolder”的文件夹上。

英文:

Here is the official documentation on volumes and mounts
https://docs.docker.com/storage/volumes/

This is an example on how you would mount a mysql container to your drive, this is a good idea to keep the data even when the container is deleted.
Look at the point "Where to store data"

https://hub.docker.com/_/mysql/

This is a very short version for a docker compose to mount a folder inside the container to an external folder

volumes:
  - /var/mysql:/var/lib/mysql

*This are only 2 lines from a complete docker-compose file but should demonstrate how to mount a data.

The folder left of the ':' Is the folder where your data is located on your drive
the folder left of the ':' to which folder inside the container this refers.

When you are looking for a file in folder './datafolder'
you should mount your the folder ./ inside your container to the folder on your drive which contains 'datafolder'

huangapple
  • 本文由 发表于 2020年9月30日 06:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64128704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定