Sharing data between Docker host and Docker containers

Posted: October 15, 2016 in Linux

Sharing data between Docker host and docker containers

We can map volumes on Docker host and docker containers during container creation

Let’s map /root folder on Docker host to container we are about to create:

 docker run --rm --hostname dockerA --name dockerA -it -v "$(pwd)"/create.sh:/create.sh ubuntu bash

–rm:deletes container after exiting

–hostname:container’s hostname

–name:friendly docker name

-it:interactive run (i) and attach terminal (t)

-v:map Docker host volume to docker container

In this example,i mapped ~/root.create.sh script to docker container “$(pwd)” -current host directory (-v “$(pwd)”/create.sh:/create.sh)

root@ubuntu:~# docker run --rm --hostname dockerA --name dockerA -it -v "$(pwd)"/create.sh:/create.sh ubuntu bash
root@dockerA:/# hostname
dockerA
root@dockerA:/# ls
bin boot create.sh dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@dockerA:/#

As we can see,script create.sh is copied to docker container root directory (/)

What does create.sh script do:

mkdir -p /docker

if [ "$HOSTNAME" = ubuntu ];then

for f in {1..3}
do
echo ubuntu > "/docker/docker-$f.txt"
done

elif [ "$HOSTNAME" = dockerA ];then
mkdir -p /folder_from_ubuntu
for f in {1..3} do echo dockerA > "/folder_from_ubuntu/docker-$f.txt"
done
else
mkdir -p /folder_from_ubuntu
for f in {1..3} do echo dockerB > "/folder_from_ubuntu/docker-$f.txt"
done
fi

First create /docker folder if it’s not exist,then it creates three files named /docker/docker-[1-3].txt then in these files writes output of $HOSTNAME variable,so if script runs on Docker host,”ubuntu” (docker host hotname),will be written to these files,and so on.

When run ~/create.sh script on Docker host,/docker folder will be created with 3 files (docker-[1-3].txt)

root@ubuntu:~# ./create.sh
root@ubuntu:~# ls /docker/
docker-1.txt docker-2.txt docker-3.txt
root@ubuntu:~# cat /docker/docker-1.txt
ubuntu

Now map ~ and /docker folder to docker container,(remember,docker run –rm switch automatically deletes container upon exit)

docker run --rm --hostname dockerA --name dockerA -it -v "$(pwd)"/create.sh:/create.sh -v /docker:/folder_from_ubuntu ubuntu bash

Now not only ~ folder from host is mapped to container,but also /docker folder  (-v /docker:/folder_from_ubuntu),instead “folder_from_ubuntu” we can put any name we want.

root@ubuntu:~# docker run --rm --hostname dockerA --name dockerA -it -v "$(pwd)"/create.sh:/create.sh -v /docker:/folder_from_ubuntu ubuntu bash
root@dockerA:/# hostname
dockerA
root@dockerA:/# ls
bin create.sh etc home lib64 mnt proc run srv tmp var
boot dev folder_from_ubuntu lib media opt root sbin sys usr
root@dockerA:/# ls /folder_from_ubuntu/
docker-1.txt docker-2.txt docker-3.txt
root@dockerA:/# cat /folder_from_ubuntu/docker-1.txt
ubuntu

As we can see,/docker folder content from Docker host (ubuntu), is replicated to docker container (dockerA),but what happens we we run create.sh script from container ?

root@dockerA:/# ./create.sh
root@dockerA:/# cat /folder_from_ubuntu/docker-1.txt
dockerA

Because script runs from dockerA containers,content shows dockerA.Files modified in docker container is reflected to Docker host:

root@ubuntu:~# cat /docker/docker-1.txt
dockerA

Also,file deleted on container is deleted from Docker host.

Leave docker container running (CTRL+P & CTRL+Q)

Create new file in /docker folder on Docker host:

"created on docker host" > /docker/docker_host.txt

Get back to container (dockerA)

root@ubuntu:/docker# docker exec -it dockerA bash
root@dockerA:/# cat /folder_from_ubuntu/docker_host.txt
created on docker host

Sharing data between containers

Create new docker container (dockerB) which will have mounted all volumes from dockerA

(–volumes-from dockerA)

docker run --rm --hostname dockerB --name dockerB -it --volumes-from dockerA ubuntu bash
root@dockerB:/# hostname
dockerB
root@dockerB:/# ls
bin create.sh etc home lib64 mnt proc run srv tmp var
boot dev folder_from_ubuntu lib media opt root sbin sys usr
root@dockerB:/# ls /folder_from_ubuntu/
docker-1.txt docker-2.txt docker-3.txt docker_host.txt

As we can see,all folder are replicated from dockerA

root@dockerB:/# ./create.sh
root@dockerB:/# cat /folder_from_ubuntu/docker-1.txt
dockerB

I run script on dockerB and again,changes are propagated to Docker host (ubuntu) and dockerA,also,files created on dockerB will be visible on dockerA and docker host (ubuntu)

root@dockerB:/# echo "created on dockerB" > /folder_from_ubuntu/dockerB_host.txt
root@ubuntu:~# docker exec -it dockerA bash
root@dockerA:/# cat /folder_from_ubuntu/dockerB_host.txt
created on dockerB

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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