Create (and persist) file from Java in Docker container

Multi tool use
Multi tool use


Create (and persist) file from Java in Docker container



I am developing a Java Spring Boot application meant to run in a Docker container. When I run the image, I need the app to check if a file exists in a path, and to create it if does not exist. When I stop the container and start it again, the file is missing again.
How can I achieve this? Do I need to mount a volume on the host?





Upon deleting the container everything you stored in it is going to be deleted with it. Basically what starting a container is: 1.) Start the container 2.) Copy the "image" in there. 3.) Mount any given volumes in there. In your case you should probably create a folder/file and mount it in your contanier using either the -v command for normal docker or the volumes: option for docker-compose.
– Ben
Jul 3 at 7:11


-v


volumes:


docker-compose





Also upon rereading: You only stopped the container, not actually deleted it? Can you confirm that? Stop the container, run docker container list and see if it is still there with status stopped. In that case no files should be deleted by docker. Maybe you started a new container over restarting the old one?
– Ben
Jul 3 at 7:13



docker container list


stopped





May be, I am not really comfortable with Docker so I am using just as a testing environment right now. I'll try to stop and restart it without deleting.
– afe
Jul 3 at 7:15





Okay, do so! Don't forget to use docker start to start a stopped container, not docker run (which creates a new container).
– Ben
Jul 3 at 7:17


docker start


docker run





Plus if you specified --rm when you started the container it will be deleted when stopped.
– ewramner
Jul 3 at 7:19




2 Answers
2



Let's say your Java app called app that saves a file in:


app


/var/lib/data



Then you should launch your container as follows:


docker volume create app-data

docker run -p 8080:8080 --mount source=app-data,target=/var/lib/data app



This will store all your data in a persistent volume named app-data


app-data



You can also bind a directory between the host machine and the container as follows:


docker run -p 8080:8080 --volume /var/lib/data:/var/lib/data app



This will store everything in /var/lib/data on the host rather than in the container. That way, the next time you start the container, it reads from there.


/var/lib/data



I typically go with the first option, as I have a name for the data I am storing, and it is not changing the "state" of the host machine (arguably still is, but very minimally). Binding a directory leaves room for error if any other process running on the host touches that directory.



See: https://docs.docker.com/storage/volumes/





This is something I don't feel like doing. I don't want those data to "leave" the container.
– afe
Jul 4 at 9:52



Ok as @Ben suggested it is working fine when I stop/start the container instead of running it.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

sW8EJg36sc5tzZwaS1wQLAVzug04q7ULZEDonva
bUV4dkCX,CJ rvgoZKYc3X5NF vqoW,2ddxvD bBtLCXF z8k P9 s,ccgifXVli,76w ajNUaOq8OkmG Xx2Bk mw6EyUM,U SgyN7dXYX

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications