I am new to docker and I have what I believe is a simple question. In the dockerfile I am creating a volume so that I can persist some information created in the container. My question is, does anyone know how to get the volume name and ID and write it to the persist.txt file? Can I do that in the dockerfile or is there a different way it should be done. Here is the code in the dockerfile:
Add Local Directory
RUN mkdir /persist
RUN echo “I’m a persistent thing!” > /persist/persist.txt
Thanks in advance! - Neal
You don’t crete a volume in a Dockerfile. You just define where the mount point should be after the container starts and the volume can be created. Since there is no volume yet, you can’t get any ID or any info.
Why do you want to save the volume name in a file?
Here is my latets tutorial in which I write about volumes in general as well, so you can learn more about how volumes work.
Don’t define a volume in a Dockerfile. Document it as a requirement so whoever creates a container from the image can mount a volume at a specific location in the container. There is no way to remove volumes defined in a Dockerfile in case someone don’t want to use it so they would end up with a bunch of anonymous volumes if they create a container from the same image multiple times and they won’t even know about that probably.
Thank you for your reply! So our container has some logic and will crunch data in a database to create a file that will be ingested into another application/container. So we want this to persist. I need to find the best way to accomplish this.
Sorry, I’m still not sure about your goal. Maybe its just too late for me where I’M writing from, but why would writing to the database and saving a file require knowing the name of a volume which could be seen only from the outside?
I appreciate your help. I am sure I don’t understand containers well enough but my understanding is that data inside a container will not persist when that container is destroyed. We want our files generated by the container to be able to be saved for later use. Do I not understand the process?
So my need is to be able to get back to that volume and pull the file out after the container stops or is destroyed.
If I could write it out to a different location that could be accessed later, that is fine as well.
You understand it perfectly, but for that you don’t need to know the name of a volume, only the mount point inside the container. Whether you predefine it for anonymous volumes or not is another question, I just wanted to note that I would not define it in a Dockerfile.
All you need to do is save a file in a specific folder in a container and make it a volume. Meaning that you mount a volume from the host to the container at that specific location. If you need the file in another container, you need to mount the same volume to that other container wherever that container expects it to be.
Thank you. So, if not in a docker file, should I do it in the python code? All of the examples I have looked at for mounting volumes involve docker commands.
Unless that python code manages the docker api, no. Have you checked the blogpost I shared? That shows some docker compose examples mounting volumes. Applications in the container should not even care about volumes.
I will look over the blog tomorrow morning. I am out of time here today. Thanks again!
Seems like a wording problem: a
Dockerfile is used to create an image, not to run it.
When you run a container, you can use for file persistency either a Docker volume (doc) or a bind mount (doc) of a host folder into the container.