This is again docker post in docker series which we has already shared with readers with some other valuable post. Today we will see How To Share Data between Docker Container.

Docker has their own network, in case we have proper protocols placed on Docker Container, we can easily communicate to another Docker container or Host machine, but we can’t demand it in case Docker container because its should light as much as possible and we can’t afford any additional protocol server only for sharing communicating between Docker containers, For this we have Volumes in Docker Container which helps to share data between Docker containers.


For this setup we are going to use Ubuntu 16.04 LTS latest as of now Dec 2017.

# lsb_release -d
Description:	Ubuntu 16.04.3 LTS

# uname  -r

# docker --version
Docker version 1.13.1, build 092cba3

Now we could see How to create volume and share them between Containers with their data. For this it is good to create volume first that we could share with multiple containers with same data.So these volume could attach to any new container at time when container create with Docker run command.

Creating Volume

For creating volume in Docker structure we used below command, creating Docker volume docker-data

# docker  volume create --name docker-data

Just after creating Docker volume, it will print volume name like above shown. Once it created it would work and can display with below commands.

# docker volume ls
local               docker-data

# docker volume inspect docker-data
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/docker-data/_data",
        "Name": "docker-data",
        "Options": {},
        "Scope": "local"

With above command outputs we can understand that Volume has been created in docker structure and could used for Docker containers further to share data with multiple Containers.

For accessing this volume on Docker Container, we need to create Docker container with some options while creating Docker container with Docker run command.

Creation of Docker Container with Volume

Through below way we can create Docker container that have access on same volume that we created above.

# docker run -v docker-data:/docker-data --hostname centos7-docker  --name centos -it centos /bin/bash

Login on Docker container, it has Docker-data volume directory
[root@centos7-docker /]# ls  
anaconda-post.log  bin  dev  docker-data  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

So, Now let’s create one file on same directory Volume from Docker container.

[root@centos7-docker /]# touch /docker-data/f1
[root@centos7-docker /]# cat > /docker-data/f1 
Docker Container volume first file

[root@centos7-docker /]# cat /docker-data/f1 
Docker Container volume first file

Now we could create another Docker Container which also has same share volume as we did above for this Container. This way this volume data would share to multiple container. As volume location is local to Host machine we can also access data directly form Host machine as well but it is never suggested to work on volume from Host machine, as it could corrupt Data present on Volumes shared on Docker containers.So let’s create another Docker container for see Shared volume on that side.

# docker run -v docker-data:/docker-data1 --hostname centos-docker  --name centos7 -it centos /bin/bash

 Docker Container created and already shared Volume to previous Container is shared to this Container
[root@centos-docker /]# ls
anaconda-post.log  bin  dev  docker-data1  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@centos-docker /]# ll docker-data1/
total 4
-rw-r--r-- 1 root root 35 Dec 12 13:50 f1
[root@centos-docker /]# cat docker-data1/f1
Docker Container volume first file

Creatin Docker Volume at time of Container creation

We can also create Docker volume at time docker container creation, This way we need not to create Volume before creating Docker container and this also shareable to another Docker container like we did above. Examples below

Below commands used to create New container and its Volume in one command
# docker run -v docker-volume:/data-volume --hostname centos-docker  --name centos-latest -it centos /bin/bash

Below commands shows volume is created and attached to container created above
[root@centos-docker /]# ls
anaconda-post.log  bin  data-volume  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Created a file for identification
[root@centos-docker /]# ll data-volume/
total 0
[root@centos-docker /]# cat > data-volume/f1
Second volume Mount Point: /var/lib/docker/volumes/docker-volume/_data
[root@centos-docker /]# # 

creating Another Container that would attached earlier created Volume
# docker run -v docker-volume:/data-volume --hostname centos-latest1  --name centos-latest1 -it centos /bin/bash
[root@centos-latest1 /]# cat data-volume/f1 
Second volume Mount Point: /var/lib/docker/volumes/docker-volume/_data

In above commands we saw that we could create volume while creating Container and can attach them to another container as well.

Although we could also discuss some other cases that could used to deployed to share data and implement Volume in Docker Container. But that would quit lengthy article. We would cover some other technique in another post.


With this brief descriptions, Now we know how to create volume in Docker container and Share data with multiple container. But we should take care to data integrity part while sharing data within multiple Container because Docker has no file locking mechanism to handle data corruption, Application layer need to care about this portion over Docker container Volumes.

Some other Docker Posts
1. Docker Introduction
2. How To Work On Docker Container
3. How to Move Docker Image
4. How To Commit Container changes
5. Port Forwarding for Docker Container through IPTables
6. How to set Docker Name and Hostname