docker volumes

# bind mount in container

docker run -it --name ctrName  -v ~/host/dir:/dataCtr ubuntu:latest /bin/bash
  usecase:- webserver mount directory

to debug run "docker inspect <containerId> and look for mounts to check if mounted or not

# docker volume
docker run -it --name ctrName2 -v volName:/dataCtrFolder ubuntu:latest /bin/bash

the volume will be mounted to  /dataCtrFolder  directory of the container "ctrName2"

docker volume

usecase: persisting data/share between containers eg. mysql

# mount volumes from another container eg. volume from master1 to slave1

docker run -it --name master1 -v backup:/backup -v logs:/logs ubuntu:latest /bin/bash

docker run -it --name slave1 --volumes-from master1 ubuntu:latest /bin/bash

docker volume inspect volName  # see json details


# persisting data
to export key-value env variables to container
#docker run -it -e DOC_ROOT=/var/www/html -e SHELL=/bin/bash ubuntu bash

if there are lots of env variables
 create file with valiables eg. env.list
MODE=test
DOC_ROOT=/var/www

docker run -it --env-file /path/env.list ubuntu bash

  # echo $MODE
        test
 # echo $DOC_ROOT
      /var/www

# To override the value in env.list use  -e option again
docker run -it --env-file /path/env.list -e MODE=dev ubuntu bash


No comments:

Post a Comment