How to map application to docker volume

1.6k views Asked by At

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

There are 1 answers

8
Dominik Wuttke On BEST ANSWER

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'