Can a docker container persist data? It is often believed that a docker container cannot persist data. In this blog, we will see how we can use docker containers to hold the data within them.
We can run the docker container by:
docker container run
Let’s run a docker container:
docker container run --name mycontainer -it ubuntu:latest /bin/bash
Now let us write some data into some file inside the /tmp directory.
Now exit from that container.
Check to see if your container is running or not.
Now that we know the container is not running, let’s check if it is in a stopped state.
As we can see, the container is showing Exited(0). If we stop a container, it does not delete the configurations and contents within it. Now let us try to spin this container up again.
docker container start mycontainer
The stopped container is now up and running. Let’s see if the file we created is still there or not.
docker container exec -it mycontainer bash
The file is intact along with its content. This shows the persistence of data within a container. Until and unless the container is not explicitly deleted, the content or files within the container won’t be deleted.
To conclude, if we throughout the lifecycle of the container, the data is persisted. When a container is deleted, the data also gets deleted along with the container. If you need better persistence in a docker container, make use of volumes. Volumes will ensure that the data is persisted even after a container is deleted.
Hey, readers! Thank you for sticking up till the end. If you have any questions/feedback regarding this blog, I am reachable at email@example.com. You can find more of my blogs here.