Persist App Data in Docker

Learn how to persiste application data in Docker container


In the previous episode, we saw how to run dynamic application and interact with it by updating user’s information.

However, one of the issue currently is that when we restart or remove the container, then we lost the data we updated to our user.

Overview of Docker Volume

Docker Volume

Image: https://docs.docker.com/engine/storage/volumes/

  • We need Docker volume for data persistance.
  • Folder or directory in the host file system is mounted into the virtual file system of Docker. Example:

    $ docker run -d -p 8080:80 --volume /home/username/static-app:/usr/share/nginx/html nginx
    
  • There type of volume types:

    • Host Volume: You decide where on the host file system the reference is made. See above example.
    • Anonymous Volume: For each container, a folder is generated in the host file system that get mounted.

      $ docker run -v /var/lib/mysql/data mysql
      

      NOTE: Above command is just an example and might not work. It might be stored in the host system defined by Docker like in Linux it will be mounted on /var/lib/docker/volumes/random-hash/_data

    • Named Volume: You can reference the volume by name and it should be used in production.

      $ docker run -v app-db:/var/lib/mysql/data mysql
      

      NOTE: Above command is just an example and might not work. It might be stored in the host system defined by Docker like in Linux it will be mounted on /var/lib/docker/volumes/random-hash/_data

Help me to improve Gorkha Dev.