Docker – Part 4 – Manage data in docker containers

Mount a host directory as data volume

You can mount a directory from host to a container

# Create a directory on the host
[root@dockervn ~]# mkdir -p /var/docker/disk01

# Run a container with mounting /var/docker/disk01 to /mnt
[root@dockervn ~]# docker run -i -t -v /var/docker/disk01:/mnt centos /bin/bash
[root@2985e50c258f /]# df -h
Filesystem Size     Used     Avail     Use%     Mounted on
/dev/mapper/docker-253:0-17174473-8dd986c417c6d     10G     241M     9.8G     3%     /
tmpfs 921M 0     921M 0%     /dev
tmpfs 921M 0     921M 0%     /sys/fs/cgroup
/dev/mapper/centos-root                  14G      1.9G 13G      14%     /mnt
shm 64 M 0 64M      0%     /dev/shm

Mount a data volume

A data volume is a specially designed directory in the container and bypass by the Union File System

  • Use –v option to mount data volume when creating a new container. By default, it is not deleted when the container is stopped.
  • Can be shared and reused among containers

# Launch a container, named container1 and mount a volume for container
[root@dockervn ~]# docker run -it -v /data –name container1 centos

# Check the volume data inside container
[root@712d3622cea6 /]# ls
bin data
dev etc home lib lib64 lost+found media opt proc root run sbin srv sys tmp usr var

# Check physical location of data volume
[root@dockervn ~]# docker inspect container1
This will give out a JSON output and you should look for Volumes attribute in the output.
“Mounts”: [{    “Name”: “5e2f738eb6941d784653f9604e2253540593155256b07968e”,  “Source”:          “/var/lib/docker/volumes/5e2f738eb6941d784653f9604e2253540593155256b07968e/_data”,  “Destination”: “/data”

Create a data volume container and mount it from multi containers

If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it’s best to create a named Data Volume Container, and then to mount the data from it.

Create a new container named containerDATA with a volume /dbdata
to share

[root@dockervn ~]# docker create -v /dbdata –name containerDATA centos /bin/bash

  • Note that this is a container data so it should be stopped

Create some containers and mount the /dbdata from containerDATA

[root@dockervn ~]# docker run -i -t –volumes-from containerDATA –name host1 centos /bin/bash
[root@66fb92d3e5d3 /]# ls
anaconda-post.log bin dbdata dev etc home media mnt opt root run sbin srv sys tmp usr var

[root@dockervn ~]# docker run -i -t –volumes-from containerDATA –name host2 centos /bin/bash
[root@2e6356af733e /]# ls
anaconda-post.log bin dbdata dev etc home media mnt opt root run sbin srv sys tmp usr var

[root@dockervn ~]# docker ps
2e6356af733e centos “/bin/bash” 6 minutes ago Up 6 minutes host2
66fb92d3e5d3 centos “/bin/bash” 7 minutes ago Up 7 minutes host1

Mount a shared-storage

Some Docker volume plugins allow you to provision and mount shared storage, such as iSCSI, NFS, or FC.

Part 1:
Part 2:
Part 3:
Part 4:

About Terri

System Administrator @Netpower Datacenter

Posted on 12.07.2016, in Linux, Technical Articles and tagged , , . Bookmark the permalink. 3 Comments.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: