Persisting data in docker context I am new to docker and flask web framework, my docker context has a flask file, a mod_wsgi file, and the python script. As I am new to flask, there are a lot of small changes which I keep doing. For any change to the docker context to take into effect I have to:


Kill the container and prune it out of the system.
Make changes to the Dockerfile and then rebuild the container.
In some cases I have to even remove the image and rebuild it as the changes are not taken into effect if cache is retained.


Is there any way in which I can have the changes into the docker context taken into effect during run time. I tried to have the docker context shared as a volume as that is the preferred way to persist data in Docker containers and services, but that did not work, any changes still were not reflected.

Could you please let me know of any way I can achieve this. Any suggestion would be greatly appreciated.

Thanks and regards,
Shailesh
Mounted volumes can be used to preserve state between runs, apart from that Docker is stateless by design. To make a local directory available inside the Docker container, you can use the -v option, for example:

docker run -v /path/to/local/dir/:/mountpath/inside/docker ...

But if I understand your question correctly, you are asking for a faster feedback cycle while working on the Dockerfile. For that, I would recommend to start the container, and attach an interactive shell to try out your changes. Once you made some progress, update the Dockerfile.

$ docker ps
CONTAINER ID  IMAGE        COMMAND  CREATED           STATUS        PORTS  NAMES
f1217635695d  ubuntu:18.04 "bash"   23 seconds ago   Up 21 seconds         stoic_austin

$ docker exec -it f1217635695d bash
root@f1217635695d:/# ...


Also note that proper use of caching can speed up rebuilds of the Docker container after changes significantly.


  (from your question)
  1. Kill the container and prune it out of the system.


Pruning should not be necessary unless you run out of disk space. It should be sufficient to update the tag, for example:

docker build -t test .
docker run --rm -it test


test:latest will point to the last image id that was created. docker images will show that the previous images are not deleted, but as said, if you have enough disk space available, it is not a big concern.