DEV Community

Cover image for #017 Docker Data sharing between containers
Omar
Omar

Posted on

#017 Docker Data sharing between containers

Introduction

this is part 17 from the journey it's a long journey(360 day) so go please check previous parts , and if you need to walk in the journey with me please make sure to follow because I may post more than once in 1 Day but surely I will post daily at least one 😍.

And I will cover lot of tools as we move on.


Download app_017

app_015

if you follow part 9

cd location/DevOpsJourney/
git pull
cd app_017/
Enter fullscreen mode Exit fullscreen mode

replace location with where you put the DevOpsJourney

if your new go to part 9 and do same steps will download old lecture files and new one.


Dockerfile

In this short tutorial we are going to share data between 2 containers using volume

share

in this app_017 I add something to docker file (only for container1)

volume ["/app/share"]
Enter fullscreen mode Exit fullscreen mode

it's mean we are going to create a volume attached to it while building the image


Building

I am going to build my images

image1

docker image build -t app_017_1 container1/
Enter fullscreen mode Exit fullscreen mode

we can notice that he create the volume while building

image2

docker image build -t app_017_2 container2/
Enter fullscreen mode Exit fullscreen mode

running containers and share data

magic is here

on the left

docker run --rm --name app_017_1 -exec -it app_017_1 sh
Enter fullscreen mode Exit fullscreen mode

we need the --name here because we are going to use it to mention the volume in container2

inside the shell

echo "Hello i am inside container 1" > share/file.txt
Enter fullscreen mode Exit fullscreen mode

echo is an linux also windows program to write some text into a file it goes like

echo string > location/file.extension
Enter fullscreen mode Exit fullscreen mode

then to make sure that my file created

cat share/file.txt
Enter fullscreen mode Exit fullscreen mode

we can see our file successfully created.

let's move to the right split

docker run --rm --name app_017_2 --volumes-from app_017_1 -exec -it app_017_2 sh
Enter fullscreen mode Exit fullscreen mode

it's a long command but we cover all what those mean already in previous lectures what I care for now --volumes-from app_017_1 , app_017_1 is the --name of first container.

inside the shell let's make share that our shared file is here

ls
cat share/file.txt
Enter fullscreen mode Exit fullscreen mode

we can see that we are able to share the files between containers.

Top comments (0)