Problem with Docker Persistence
Lets try to understand two kinds of applications
- Which doesn’t use the local storage to store details
- eg: Web Server
- They use local storage to store necessary information
- eg: Database
We have to preserve the writable layers which are deleted when container is removed/terminated
To Preserve the writable layer. Writable layer needs to be present in the Docker Host even after container is deleted.
Preserving the writable layer after container is terminated can be acheived by using
- Bind mount
Refer here for complete info.
- Volume will become a mount in the container.
- Lifecycle of container will not impact Volume.
- How can we create volume
- Inside Dockerfile we have a instruction VOLUME
- Using Docker CLI:
- Create docker volumes by using docker volume
- For this section use examples mentioned here
- Dockerfile Example:
FROM ubuntu:18.04 LABEL AUTHOR="khaja" LABEL ORG="QT" RUN apt-get update && apt-get install openjdk-8-jdk -y && mkdir /myapp VOLUME /myapp ADD https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar /myapp/spring-petclinic.jar # COPY ./spring-petclinic.jar /spring-petclinic.jar EXPOSE 8080 CMD ["java", "-jar", "/myapp/spring-petclinic.jar"]
Terms to know
- Named Volume:
docker volume create myvol # use this docker container run -d -v myvol:/tools alpine sleep 1d
- Anonymous Volume:
- From Dockerfile
- From commandline if -v option has no name attached
docker container run -d -v /tools alpine sleep 1d
How to store volume Data outside of Docker Host
- Refer Here for network file system as docker volume