Docker volume
-
defult container will have ephemeral storag. its have data only layer of docker image. on top of image or running conatiner storage will remove when container is removed
-
Probelms:
Data loss— any data written inside the container (database files, uploads, logs) disappears when the container is deleted or recreated.No sharing between containers— two containers can’t easily access the same data.No easy backup/migration— data is locked inside the container’s writable layer, tied to that specific container.Poor performance for write-heavy workloads— the container’s writable layer (using storage drivers like overlay2) is slower for I/O-heavy operations than a mounted volume.
All the above problems will Volumes solve this by storing data outside the container’s lifecycle, on the host or in Docker-managed storage, so data persists independently of the container.
- volume will help to create custom storage
docker volume create Create a volume
docker volume inspect Display detailed information on one or more volumes
docker volume ls List volumes
docker volume prune Remove unused local volumes
docker volume rm Remove one or more volumes
docker volume update Update a volume (cluster volumes only)
Types of docker volumes
- Named volumes
- Bind mounts
- tmpfs mounts
Named volume
- Docker create and manage storage location
/var/lib/dokcer/volumes/on your docker host - volume will be mount on specific location of my container
create volume
docker volume create mysql_data1
docker volume create mysql_logs
docker run --name some-mysql -v mysql_data1:/var/lib/mysql -v mysql_data:/var/logs -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
docker stop containerid
docker rm containerid
docker volume rm mysql_data1
docker volume rm mysql_logs
Bind mounts
- map data on specific location on you container local host
docker run --name some-mysql -v /root/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
docker run -it \
--name maven-builder \
-v "$PWD":/usr/src/mymaven \
-w /usr/src/mymaven \
maven:3.9-eclipse-temurin-17 \
mvn clean package -DskipTests && echo "hello" > sample.txt
Task
-
create volume and lunch oracle-xe and mount oracle data
/usr/lib/oracle/xe/oradata/XE/ -
create table and inser sample data
-
rm container and create new container with same volume and check data
-
user bind mount and run spring-petclinic package cmd
-
add another echo cmd to create sample file
