DevOps Classroom Series – 02/Feb/2020

Docker Storage Driver

  • Refer to storage drivers section in the article.

Docker Volumes

  • Volume adds persistence to the docker container
  • Volumes add disk sharing across containers and also between container and host
  • Volume life cylce is different from containers life cycle
  • Example Scenario:
    • Create a docker container with mysql running:
      • For the containers which generate/store data it is important to preserve it, so we have to use volumes
  • Application Categories:
    • Stateless Applications:
      • Application which donot store any state and rely on other applications to store/preserve the data
    • Stateful Application
      • Applications which store the data/state locally and for these applications the data has to preserved by us using volumes.
  • For official Docs Refer Here

Docker Volume Command line

  • Docker has created a sepearte command line for volume
  • Volume command line
docker volume create myvol
docker volume ls
docker volume inspect myvol
  • Creating the container and mounting the volume
docker container run -it --name cont1 --mount "source=myvol,target=/jars" openjdk:8 /bin/bash
  • Check for the mounts available inside container using df -h Preview
  • Now lets experiment with generating data inside the volume, remove the container and check in the volumes mount point if the data is still available
# Inside cont1
cd /jars
wget https://war-jar-files.s3-us-west-2.amazonaws.com/gameoflife.war
exit

# In Docker Host
docker container rm -f cont1

# Check for volumes
docker volume ls
docker volume inspect

# copy the mount point dir 
ls <mountpointdir>
# two files should be available (spc.jar and gol.war)
  • Now lets mount the myvol to two containers and run the application simultaneously
docker container run --name cont3 --mount "source=myvol,target=/app" -d -p 8080:8080 openjdk:8 java -jar /app/spring-petclinic.jar

docker container run --name cont4 --mount "source=myvol,target=/app" -d -p 8081:8080 openjdk:8 java -jar /app/spring-petclinic.jar

docker container ls

docker volume ls

# remove all the containers
docker container rm -f $(docker container ls -a -q)

  • Data from Docker Volume will be removed only when the volume is removed
docker volume rm myvol

Bind mount volumes

  • Now your docker host has a folder /app you want the same folder in the container
docker container run --name cont5 --mount "type=bind,source=/app,target=/jars" -d -p 8080:8080 openjdk:8 java -jar /jars/spring-petclinic.jar

Dockerfile

  • Inside the Dockerfile we have a instruction called as VOLUME.
  • When you use this for every container a new volume is created.
  • Lets write the Dockerfile
FROM openjdk:8

RUN mkdir /app && cd /app && wget https://war-jar-files.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
VOLUME /app
EXPOSE 8080
CMD ["java", "-jar", "/app/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
  • Create two container from the image and verify the volumes
docker volume ls
# inspect the required volume and you should see spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar in the mountpoint dir
docker stats

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin